Baidu positioning SDK to get the current latitude and longitude, sdk latitude and longitude

Source: Internet
Author: User

Baidu positioning SDK to get the current latitude and longitude, sdk latitude and longitude

When you use the built-in LocationManager and Location of Android to obtain a location, the Location is often null, and the operation is not very convenient, in this Demo, I used the location SDK In the Baidu map API to get the longitude and latitude of the current location and detailed address information at one time, and the surrounding POI information. I can also set the location notification point, when a user arrives at a certain location, the user is notified by means of a notification. For jar package downloads and official documentation, refer to Baidu positioning SDK, provided that you need to register a Baidu developer account.
Next let's take a look at the basic principles of positioning. Currently, positioning sdks can be located through GPS, base stations, and Wi-Fi signals. Shows the basic positioning process. When an application initiates a positioning request to the positioning SDK, the positioning SDK generates a corresponding positioning basis based on the current GPS, base station, and Wi-Fi information. Then, the SDK locates Based on the positioning. If necessary, the locating SDK will send a network request to the locating server. The locating server will calculate the corresponding coordinates based on the request positioning, and then generate the positioning result and return it to the positioning SDK based on the user's custom information.

Download the jar file from the official website and add it to the project. The project directory is as follows:

Note: Add locSDK_2.4.jar to the current project, right-click the jar file-Build path-Add...

Code on
Layout file:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
 
<Button
Android: id = "@ + id/btn_start"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "20dp"
Android: text = "Start"/>
 
<TextView
Android: id = "@ + id/TV _loc_info"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textSize = "18sp"/>
 
</LinearLayout>

Configuration file:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. ericssonlabs"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
 
<Uses-sdk android: minSdkVersion = "8"/>
 
<Permission android: name = "android. permission. BAIDU_LOCATION_SERVICE">
</Permission>
 
<Uses-permission android: name = "android. permission. BAIDU_LOCATION_SERVICE">
</Uses-permission>
<Uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION">
</Uses-permission>
<Uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION">
</Uses-permission>
<Uses-permission android: name = "android. permission. ACCESS_WIFI_STATE">
</Uses-permission>
<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE">
</Uses-permission>
<Uses-permission android: name = "android. permission. CHANGE_WIFI_STATE">
</Uses-permission>
<Uses-permission android: name = "android. permission. READ_PHONE_STATE">
</Uses-permission>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE">
</Uses-permission>
<Uses-permission android: name = "android. permission. INTERNET"/>
<Uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS">
</Uses-permission>
<Uses-permission android: name = "android. permission. READ_LOGS">
</Uses-permission>
 
<Application
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">
<Activity
Android: name = ". LocationDemoActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
 
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
 
<Service
Android: name = "com. baidu. location. f"
Android: enabled = "true"
Android: permission = "android. permission. BAIDU_LOCATION_SERVICE"
Android: process = ": remote">
<Intent-filter>
<Action android: name = "com. baidu. location. service_v2.4"/>
</Intent-filter>
</Service>
</Application>
 
</Manifest>

Implementation Code:
[Java]
Public class LocationDemoActivity extends Activity {
Private TextView locationInfoTextView = null;
Private Button startButton = null;
Private LocationClient locationClient = null;
Private static final int UPDATE_TIME = 5000;
Private static int LOCATION_COUTNS = 0;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

LocationInfoTextView = (TextView) this. findViewById (R. id. TV _loc_info );
StartButton = (Button) this. findViewById (R. id. btn_start );


LocationClient = new LocationClient (this );
// Set positioning Conditions
LocationClientOption option = new LocationClientOption ();
Option. setOpenGps (true); // whether to enable GPS
Option. setCoorType ("bd09ll"); // set the coordinate type of the returned value.
Option. setPriority (LocationClientOption. NetWorkFirst); // sets the positioning priority.
Option. setProdName ("LocationDemo"); // set the product line name. We strongly recommend that you use a custom product line name to provide you with more efficient and accurate positioning services in the future.
Option. setScanSpan (UPDATE_TIME); // you can specify the time interval for scheduled positioning. Unit: milliseconds
LocationClient. setLocOption (option );

// Register the location listener
LocationClient. registerLocationListener (new BDLocationListener (){

@ Override
Public void onReceiveLocation (BDLocation location ){
// TODO Auto-generated method stub
If (location = null ){
Return;
}
StringBuffer sb = new StringBuffer (256 );
Sb. append ("Time :");
Sb. append (location. getTime ());
Sb. append ("\ nError code :");
Sb. append (location. getLocType ());
Sb. append ("\ nLatitude :");
Sb. append (location. getLatitude ());
Sb. append ("\ nLontitude :");
Sb. append (location. getlongpolling ());
Sb. append ("\ nRadius :");
Sb. append (location. getRadius ());
If (location. getLocType () = BDLocation. TypeGpsLocation ){
Sb. append ("\ nSpeed :");
Sb. append (location. getSpeed ());
Sb. append ("\ nSatellite :");
Sb. append (location. getSatelliteNumber ());
} Else if (location. getLocType () = BDLocation. TypeNetWorkLocation ){
Sb. append ("\ nAddress :");
Sb. append (location. getAddrStr ());
}
LOCATION_COUTNS ++;
Sb. append ("\ n check location updates :");
Sb. append (String. valueOf (LOCATION_COUTNS ));
LocationInfoTextView. setText (sb. toString ());
}

@ Override
Public void onReceivePoi (BDLocation location ){
}

});

StartButton. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
If (locationClient = null ){
Return;
}
If (locationClient. isStarted ()){
StartButton. setText ("Start ");
LocationClient. stop ();
} Else {
StartButton. setText ("Stop ");
LocationClient. start ();
/*
* When the set integer is greater than or equal to 1000 (MS), the positioning SDK uses the regular positioning mode internally.
* After requestLocation () is called, the SDK is located at a specified time.
* If the location of the SDK is not changed based on the location, no network request will be initiated,
* Returns the last location. If the location is changed, the network request is requested to locate the location to obtain a new location.
* If you call requestLocation once during scheduled locating, the system periodically monitors the locating result.
*/
LocationClient. requestLocation ();
}
}
});

}

@ Override
Protected void onDestroy (){
Super. onDestroy ();
If (locationClient! = Null & locationClient. isStarted ()){
LocationClient. stop ();
LocationClient = null;
}
}


}
To see the final implementation effect, click Start to enter the location listening status and locate it based on the configured listening interval. If the position changes, the location is updated and the number of detection location updates is displayed, if GPS is enabled, GPS positioning is performed after the satellite is obtained:


The location reminder setting function is not implemented here. If you are interested, refer to the Development Guide.

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.