My path to Android development-Baidu map Open Source Tool for locating information, android Open Source

Source: Internet
Author: User

My path to Android development-Baidu map Open Source Tool for locating information, android Open Source

Location technology is essential to mobile devices. Many apps use the location function.

Usually there are two positioning Methods: GPS Positioning and network positioning.

Android provides API support for both positioning methods. However, because google's network services are inaccessible in China, Android native positioning APIs are rarely used in China, instead, we use sdks from some third-party companies in China (such as Baidu and AMAP ). This learning is Baidu's LBS (you need to apply for an API Key in advance and download the corresponding package ).

Lbsyun.baidu.com

1. Add the jniLibs directory under the android tag of the Gradle file (Module: app), which stores the so file. (The jniLibs directory is displayed under the android option and the libs under the project option is actually one)

    sourceSets{        main{            jniLibs.srcDirs=['libs']        }    }

 

Then Sync synchronously updates gradle, And the jniLibs folder will be added. Then, copy the downloaded compressed package file to the current folder and Sync it to use the LBS service.

 

2. Declare permissions in AndroidManifest. xml, add the API Key to the application tag, and then register a service in Baidu SDK.

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    <uses-permission android:name="android.permission.INTERNET"/>    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>    <uses-permission android:name="android.permission.WAKE_LOCK"/>

  

<! -- Baidu map --> <service android: name = "com. baidu. location. f "android: enabled =" true "android: process =": remote "> </service> <meta-data android: name =" com. baidu. lbsapi. API_KEY "android: value =" UIrhfCXVUrf8ITGCFexZhqPFmutb72TQ "/>

After completing these operations, you can use the LBS service.

3. Basic LBS usage

① First, the LocationClient instance is required. The construction function of the LocationClient receives a Context parameter. Here, the getApplicationContext () method is called to obtain a global Context parameter and pass it in.

public class MainActivity extends AppCompatActivity {    public LocationClient mLocationClient;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                mLocationClient=new LocationClient(getApplicationContext());    }}

② Call the registerLocationListener () method of the LocationClient to register a location listener. When location information is obtained, the location listener is called back.

The location listener can call different information (longitude, latitude, positioning time, and positioning type lamp)

public class MainActivity extends AppCompatActivity {    public LocationClient mLocationClient;    public BDLocationListener myListener = new MyLocationListener();    public class MyLocationListener implements BDLocationListener{        @Override        public void onReceiveLocation(BDLocation bdLocation) {        }    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mLocationClient=new LocationClient(getApplicationContext());        mLocationClient.registerLocationListener(myListener);    }}

Customize the listener (you can obtain the longitude and latitude and positioning information as follows)

Public class MyLocationListener implements BDLocationListener {@ Override public void onid elocation (BDLocation bdLocation) {StringBuilder sb = new StringBuilder (); sb. append ("latitude :"). append (bdLocation. getLatitude ()). append ("\ n"); sb. append ("longitude :"). append (bdLocation. getlongpolling ()). append ("\ n"); sb. append ("Positioning Method:"); if (bdLocation. getLocType () = BDLocation. typeGpsLocation) {sb. append ("GPS");} else if (bdLocation. getLocType () = BDLocation. typeNetWorkLocation) {sb. append ("network ");}}}

③ Call the start () method of the LocationClient to start locating.

        mLocationClient.start();

④ Add textview to the layout to display the positioning information. The entire MainActivity code is as follows:

Public class MainActivity extends AppCompatActivity {public LocationClient mLocationClient; public BDLocationListener myListener = new MyLocationListener (); private TextView positontext; public class extends implements extends {@ Override public void merge) {StringBuilder sb = new StringBuilder (); sb. append ("latitude :"). append (bdLocation. getLatitude ()). append ("\ n"); sb. append ("longitude :"). append (bdLocation. getlongpolling ()). append ("\ n"); sb. append ("Positioning Method:"); if (bdLocation. getLocType () = BDLocation. typeGpsLocation) {sb. append ("GPS");} else if (bdLocation. getLocType () = BDLocation. typeNetWorkLocation) {sb. append ("network");} positontext. setText (sb) ;}@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); positontext = (TextView) findViewById (R. id. position_text); mLocationClient = new LocationClient (getApplicationContext (); mLocationClient. registerLocationListener (myListener); mLocationClient. start ();}}

Run the simulator to complete Simple locating.

 

 

4. Set the properties of the LocationClient

Before the start () method, create the Instance Object of LocationClientOption, set some attributes through this object, and call these settings through the setLocOption () method of LocationClient.

 

MLocationClient. registerLocationListener (myListener); LocationClientOption option = new LocationClientOption (); option. setLocationMode (LocationClientOption. locationMode. hight_Accuracy); // optional. The default value is high precision. The positioning mode is set to high precision and low power consumption. Only the device option is used. setCoorType ("bd09ll"); // optional. The default value is gcj02. The returned Positioning Result coordinate system is set to int span = 1000; option. setScanSpan (span); // optional. The default value is 0, that is, only one request is located. Setting the interval between initiating a location request must be greater than or equal to MS is the effective option. setIsNeedAddress (true); // optional, whether to set the location Address information, which is not required by default. If this parameter is set to true, you can use getCountry (), getProvice (), getCity (), and other methods in listener to obtain the region street information option. setOpenGps (true); // optional. The default value is false. Set whether to use the gps mLocationClient. setLocOption (option); mLocationClient. start ();

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.