Get local location information in real time using the Baidumap SDK in Android studio _android

Source: Internet
Author: User
Tags sha1

Configuring the BAIDUMAP Environment

1. In the Baidu API to create a new one of their app package name and app name to be aware of their Android Studio and the package name and app name consistent;

2. Baidu map also needs to fill in a SHA1 digital signature:

A, input keytool-list-v-keystore Debug.keystore, you will get three fingerprint certificates, select the SHA1 type of certificate (the key password is Android), the SHA1 value obtained is the same as the value obtained in Ecplise, which is used as debug.

b, Input keytool-list-v-keystore Xxx.keystore, (this place is xxx.keystore when you generate the app signature keystore), KeyStore Password: Generates the password entered during the Xxx.keystore period to sign the app, and the SHA1 is available at this time.

(The above is mainly to get appkey, no Appkey read the value will be problematic)

3. Download the SDK package you need (http://lbsyun.baidu.com/sdk/download?selected=location) as needed, because only the positioning so I download the positioning function of the package;

4. Unzip the download good package, in order to make the app compatibility better, I libs all the files and folders under the app in Libs;

5. Configure the manifest file, first add the appkey you obtained in application:

<meta-data
android:name= "Com.baidu.lbsapi.API_KEY"

And then immediately need to register a dedicated service for BAIDUMAP (no need to implement):

<service
android:name= "com.baidu.location.f"
android:enabled= "true"

Finally, configure the permissions to locate the range of permissions that are required (which is also available on official documents):

<!--This permission is used for network positioning--> <uses-permission android:name= "Android.permission.ACCESS_COARSE_LOCATION"/> <! --this permission is used to access GPS positioning--> <uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION"/> <!--
For access to WiFi network information, WiFi information is used for network positioning--> <uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/> <!--obtain carrier information to support provider-related interface--> <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" /> <!--This permission is used to obtain WiFi access, WiFi information is used for network positioning--> <uses-permission android:name= "android.permission.CHANGE_ Wifi_state "/> <!--used to read the current status of the phone--> <uses-permission android:name=" Android.permission.READ_PHONE_STATE "/ > <!--write to the extended storage, write data to the expansion card to write offline location data--> <uses-permission android:name= "android.permission.WRITE_EXTERNAL_ STORAGE "/> <!--access network, network positioning needs to surf the internet--> <uses-permission android:name=" Android.permission.INTERNET "/> <! --SD card read access, user write off-line location data--> <uses-permission android:name= "Android.permission.MOUNT_UNMOUnt_filesystems "/>  

This environment is configured to complete.

Implement functional Code

1. Write a textview in layout to store your location:

<textview
android:id= "@+id/mytext"
android:layout_width= "Match_parent"
Wrap_content "

2. A locating callback interface Bdlocationlistener and class locationclient are needed in the positioning:

In ①bdlocationlistener, a positional callback function onreceivelocation (bdlocation location) returns a bdlocation location result;

The ②locationclient class is instantiated and implements a positioning listener function (Registerlocationlistener (Bdlocationlistener listener));

Define TextView controls and other method classes:

Private TextView TextView = null;
Private button button = null;
Private locationclient locationclient = null;

Implement Bdlocationlistener (some of the judgments in it can be considered and added to yourself or what location information is needed to try it, just get the current address):

The public class Mybdlocationlistener implements bdlocationlistener{@Override the public
void Onreceivelocation ( Bdlocation location) {

Get all the controls and instances individually:

TextView = (TextView) Findviewbyid (r.id.mytext);
Button = (button) Findviewbyid (R.ID.MYBTN);
Locationclient = new Locationclient (Getapplicationcontext ());
Locationclient.registerlocationlistener (MyListener);
Initlocation ()//Initialize locationgclient
button.setonclicklistener (New View.onclicklistener () {
@Override Public
void OnClick (view view) {
if (locationclient.isstarted ()) {
locationclient.stop ();
}
Locationclient.start ();
}

Initialize Locationgclient:

private void Initlocation () {locationclientoption option = new Locationclientoption (); Option.setlocationmode ( LocationClientOption.LocationMode.Hight_Accuracy);//optional, default high precision, set positioning mode, High-precision, low-power, only equipment Option.setcoortype ("bd09ll");
Optionally, the default GCJ02, set the returned positioning result coordinate system int span=1000; Option.setscanspan (span);//optional, default 0, that is, positioning only once, setting the interval required to initiate a location request is greater than or equal to 1000ms is valid option.setisneedaddress (true);/optional, Set whether address information is required, default does not require OPTION.SETOPENGPS (true);//optional, default false, set whether to use GPS option.setlocationnotify (TRUE);//optional, default false, Set whether the GPS result Option.setisneedlocationdescribe (true) if the GPS is in effect, and/or optional, default false, and whether the location semantic result is required, can be obtained in bdlocation.getlocationdescribe, the result is similar to "near Beijing Tian an door" option.setisneedlocationpoilist (true);/optional, default false, Set whether the POI result is required, you can get Option.setignorekillprocess (false) in bdlocation.getpoilist,//optional, default True, locate the inside of the SDK is a service, and put it in the standalone process, set whether to kill the process at stop, default does not kill option. Setignorecacheexception (false);//optional, default false, set whether to collect crash information, default collection Option.setenablesimulategps (FALSE),//optional, default false,
Set whether to filter GPS simulation results, the default need locationclient.setlocoption (option);  }

Now all steps are complete, you can try to run it!

In the Android studio using the Baidumap SDK real-time access to local location information about the relevant knowledge to introduce you here, I hope to help you!

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.