Baidu map positioning: obtains the longitude and latitude of the current location.

Source: Internet
Author: User

Baidu map positioning: obtains the longitude and latitude of the current location.

Note:

1. initialize the BaiduMap SDK before the display interface, that is:

SDKInitializer. initialize (Context );

SetContentView (R. layout. main );

2. When the location mode is set to LocationMode. Hight_Accuracy, you may not be able to obtain accurate latitude and longitude in the room. The default value is 4.9E-324.

The solution is to change the mode to Battery_Saving, or to the outdoor

3. You can set the location mode based on the network connection status of the current device and whether GPS is enabled.

// Obtain network connection information

ConnectivityManager cManager = (ConnectivityManager) getSystemService (Context. CONNECTIVITY_SERVICE );

NetworkInfo info = cManager. getActiveNetworkInfo ();

// Obtain the GPS connection information
LocationManager lManager = (LocationManager) getSystemService (Context. LOCATION_SERVICE );
If (lManager. isProviderEnabled (LocationManager. GPS_PROVIDER )){
Toast. makeText (this, "GPS enabled", 0). show ();
}

 

4. Use latitude and longitude to operate in the public void oncancelocation (BDLocation loc) {} Method

 

Step 1: Import Baidu positioning SDK package

 

Part 2: add access permissions and services to the AndroidManifest. xml configuration file. The Code is as follows:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <manifest xmlns: android = "http://schemas.android.com/apk/res/android" 3 package = "com. qf. location "4 android: versionCode =" 1 "5 android: versionName =" 1.0 "> 6 7 <uses-sdk 8 android: minSdkVersion =" 8 "9 android: targetSdkVersion = "18"/> 10 11 <! -- Added content 1 (access permission) --> 12 <uses-permission android: name = "android. permission. GET_ACCOUNTS "/> 13 <uses-permission android: name =" android. permission. USE_CREDENTIALS "/> 14 <uses-permission android: name =" android. permission. MANAGE_ACCOUNTS "/> 15 <uses-permission android: name =" android. permission. AUTHENTICATE_ACCOUNTS "/> 16 <uses-permission android: name =" android. permission. ACCESS_NETWORK_STATE "/> 17 <uses-per Mission android: name = "android. permission. INTERNET "/> 18 <uses-permission android: name =" com. android. launcher. permission. READ_SETTINGS "/> 19 <uses-permission android: name =" android. permission. CHANGE_WIFI_STATE "/> 20 <uses-permission android: name =" android. permission. ACCESS_WIFI_STATE "/> 21 <uses-permission android: name =" android. permission. READ_PHONE_STATE "/> 22 <uses-permission android: name =" android. p Ermission. WRITE_EXTERNAL_STORAGE "/> 23 <uses-permission android: name =" android. permission. BROADCAST_STICKY "/> 24 <uses-permission android: name =" android. permission. WRITE_SETTINGS "/> 25 <uses-permission android: name =" android. permission. READ_PHONE_STATE "/> 26 <! -- This permission is used for network positioning --> 27 <uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION"> 28 </uses-permission> 29 <! -- This permission is used to access GPS positioning --> 30 <uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"> 31 </uses-permission> 32 <! -- Used to access Wi-Fi network information. wifi information is used to locate the network --> 33 <uses-permission android: name = "android. permission. ACCESS_WIFI_STATE "> 34 </uses-permission> 35 <! -- Obtains carrier information to support interfaces related to carrier information --> 36 <uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE "> 37 </uses-permission> 38 <! -- This permission is used to obtain the permission for wifi, And wifi information is used to locate the network --> 39 <uses-permission android: name = "android. permission. CHANGE_WIFI_STATE "> 40 </uses-permission> 41 <! -- Used to read the current status of the mobile phone --> 42 <uses-permission android: name = "android. permission. READ_PHONE_STATE"> 43 </uses-permission> 44 <! -- Write extended storage and write data to the expansion card for writing offline positioning data --> 45 <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE "> 46 </uses-permission> 47 <! -- Access the network and access the INTERNET for Network Positioning --> 48 <uses-permission android: name = "android. permission. INTERNET"/> 49 <! -- SD card read permission, users write offline positioning data --> 50 <uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"> 51 </uses-permission> 52 <! -- Allow applications to read low-level system log files --> 53 <uses-permission android: name = "android. permission. READ_LOGS "> 54 </uses-permission> 55 56 <application57 android: allowBackup =" true "58 android: icon =" @ drawable/ic_launcher "59 android: label = "@ string/app_name" 60 android: theme = "@ style/AppTheme"> 61 <activity62 android: name = "com. qf. location. mainActivity "63 android: label =" @ string/app_name "> 64 <intent-filter> 65 <action android: nam E = "android. intent. action. MAIN "/> 66 67 <category android: name =" android. intent. category. LAUNCHER "/> 68 </intent-filter> 69 </activity> 70 71 <! -- Added content 2 --> 72 <meta-data73 android: name = "com. baidu. lbsapi. API_KEY "74 android: value =" Dt7Q30nVQMKbM3NT6GHnRnGq "/> 75 76 <service77 android: name =" com. baidu. location. f "78 android: enabled =" true "79 android: process =": remote "> 80 </service> 81 </application> 82 83 </manifest>View Code

Step 3: Apply for a Baidu map development key: set the value attribute of the <meta_data> label displayed in the previous step.

Step 4: write the code to get the longitude and latitude of the current location: the code is as follows:

1 private LocationClient locClient; 2 3 @ Override 4 protected void onCreate (Bundle savedInstanceState) {5 super. onCreate (savedInstanceState); 6 7 // initialize BaiduMap SDK 8 SDKInitializer before the display interface. initialize (getApplicationContext (); 9 setContentView (R. layout. activity_main); 10 11 12 getLocation (); 13} 14 15 16 // obtain the longitude and latitude 17 public void getLocation () of the current mobile phone location () {18 // instantiate location client 19 locClient = new LocationClient (getApplicationContext (); 20 // create a Location option object 21 LocationClientOption locOption = new LocationClientOption (); 22 23 // set the option attribute 24 locOption. setCoorType ("bd09II"); // bd09II indicates that the returned result is Baidu's latitude and longitude 25 locOption. setIsNeedAddress (true); // The returned information contains the current address 26 locOption. setNeedDeviceDirect (true); // The returned content contains 27 locOption directions of the phone head. setScanSpan (5000); // initiates a location request 28 locOption every 5 seconds. setLocationMode (LocationMode. hight_Accuracy); // high precision mode, network and GPS29 30 31 // set the encapsulated parameters to the location client 32 locClient. setLocOption (locOption); 33 34 // register a location listener for the location client 35 locClient. registerLocationListener (new BDLocationListener () {36 37 @ Override38 public void onReceiveLocation (BDLocation loc) {39 double latitude = loc. getLatitude (); 40 double longpolling = loc. getlongpolling (); 41 Toast. makeText (getApplicationContext (), latitude + ":" + longpolling, 0 ). show (); 42 43/* ========================================== the interface defined here = ================================= */44 45} 46 }); 47 48 // start position client 49 locClient. start (); 50 51}View Code

 

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.