Baidu positioning SDK for the current latitude and longitude and location

Source: Internet
Author: User

When using the Locationmanager and location of Android to get the position, often there is the situation of access to null, and the operation is not very convenient, in this demo I used the Baidu map API Positioning SDK, Can get the current location latitude and longitude and detailed address information at once, also can get the surrounding POI information, at the same time can set the location of the notification point, when reaching a location, send notification information and other ways to inform the user. Jar package Download and official documents Please refer to: Baidu Positioning SDK, the premise is to register Baidu developer account.
The following is a look at the fundamentals of positioning, currently, the positioning SDK can be GPS, base station, WiFi signal to locate. As shown in the basic positioning process, when an application initiates a location request to the Location SDK, the Location SDK will generate a corresponding location based on the current GPS, base station, and WiFi information. The positioning SDK will then be positioned based on the positioning. If required, the Location SDK sends network requests to the location server. The location server calculates the corresponding coordinate location according to the location of the request, and then generates the positioning results to the location SDK based on the user's customized information.

To the official download jar file after adding to the project, the project directory is as follows:

Note To add Locsdk_2.4.jar to the same day project, right-click Jar file-build path-add to ...

On the Code
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" >

&LT;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 criteria
locationclientoption option = new Locationclientoption ();
Option.setopengps (TRUE); Whether to turn on GPS
Option.setcoortype ("Bd09ll"); Sets the coordinate type of the return value.
Option.setpriority (Locationclientoption.networkfirst); Set the targeting priority level
Option.setprodname ("Locationdemo"); Set the product line name. We strongly recommend that you use a customized product line name, so that we will provide you with more efficient and accurate location services later.
Option.setscanspan (Update_time); Sets the time interval for timed positioning. Unit milliseconds
locationclient.setlocoption (option);

Register 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.getlongitude ());
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 update number:");
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 value is greater than or equal to (MS), the positioning SDK uses the timed positioning mode internally.
* Once the requestlocation () is called, the Location SDK will be positioned once every set time.
* If the Location SDK finds no changes based on the positioning basis, the network request will not be initiated.
* Returns the result of the last position, and if a location change is found, the network request is positioned to obtain a new positioning result.
* When the timing is fixed, the requestlocation is called once, and the locating result is heard regularly.
*/
Locationclient.requestlocation ();
}
}
});

}

@Override
protected void OnDestroy () {
Super.ondestroy ();
if (locationclient! = null && locationclient.isstarted ()) {
Locationclient.stop ();
Locationclient = null;
}
}


}
To see the final implementation of the results, click on the start after the position monitoring state, according to set the listening interval to locate, if there is a change in position to update the location, while showing the number of detection location update, if the GPS is turned on, then obtain the satellite, GPS positioning:


Set the function of the location reminder I'm not here, but I'm interested in it. Reference to Development Guide

Baidu positioning SDK for the current latitude and longitude and location

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.