Android uses Baidu positioning SDK methods and error handling

Source: Internet
Author: User

Previously, I used the base station method to locate the location in my project, using the API provided by Google, but I suddenly did not return data at noon the day before yesterday. I found out after searching the Internet, google's access port does not provide services and uses Baidu's existing SDK Based on Time Constraints. However, the first time a location is obtained during use, it is always null. After many experiments, it is finally successful. Of course, you can add the location offset algorithm to the exact location. My applications do not have high requirements for this, so I will not talk about it as soon as I search for it.

The following section is from Baidu map API> positioning SDK

The Baidu map positioning SDK is free of charge and does not need to apply for a key. Before using the Baidu positioning SDK, you want to read the Baidu positioning map API first. If used, it shall be deemed as a full acceptance of the terms of use and agree to be bound by the terms of this Agreement.
Currently, Baidu map positioning SDK only supports Android and Symbian platforms, and other products are being opened.

The following example shows some code for using the Android platform. Baidu open personnel on this platform have already written a complete demo. After the project is imported into eclipse, there is generally no error. If an error is reported, eclipse will also give a prompt. Generally, you can set the propertie. change the properties file name to default. properties is fine. If there are still errors, the project activity is displayed incorrectly or something, that is, the SDK version is incorrect. You can check <uses-sdk android through its manifest file: minSdkVersion = "8"/> Find the minimum value. For example, if the minimum version of 2.6 is 5 and the default version of eclipse is 8, right-click the project, select the bottom properties item, select Android in the list on the right of the pop-up dialog box, select API level 5 on the left, that is, 2.0, and then confirm. You have a problem.

The following describes how to use this API:
You can also view Baidu positioning SDK's own development guide

1. The first step is to match the environment:
① Copy the libs folder in Baidu demo to your project. (Do not forget to build the path of the jar package)
② Then copy

<Service android: name = "com. baidu. location. f" android: enabled = "true"
Android: process = ": remote" android: permission = "android. permission. BAIDU_LOCATION_SERVICE">
<Intent-filter>
<Action android: name = "com. baidu. location. service_v2.6"> </action>
</Intent-filter>
</Service>

 

Application node to manifest
Next, copy the permission required by Baidu status SDK.

<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>

③ You can safely use the Baidu positioning SDK in the code.
The following is a reminder from Baidu. Note the fifth article, because we often cannot locate the position for the first time:

1. Make sure that the program is compiled. If you have any questions about the code you have written, you can download the official advanced example to view the standard source code.

2. Ensure that the network is smooth, whether connected to wifi or using 2G/3G signals.

3. Locate that the SDK call must be in the main thread.

4. You must set parameters before positioning the SDK to start, for example, whether to use GPS or scan Interval Settings. We strongly recommend that you set up your own prodName and keep it secure so that we can provide you with better positioning services.

5. Locate the SDK start and execute it immediately. In this case, it is difficult to locate the SDK, because the positioning information has not been obtained at the beginning of SDK start. Getlocation is generally null. If the location is to be obtained successfully, you can add a judgment in listerner. If the strData is empty, then initiate a location.

6. The positioning coverage rate is about 98%. That is to say, 2% of the servers may not have data, so the positioning will fail. You only need to go somewhere else, or try multiple times to locate the problem.

7. Use a real machine. Positioning tests cannot be performed on virtual machines.

The following is my example code:
The specific idea is: Separate the code for enabling and obtaining the location, and start when the program is started. According to the above, sometimes the location can be obtained in two minutes, which is absolutely impossible for us to display information. If the main thread is stuck for two minutes, the consequences can be imagined.
I have not set the time interval public void setScanSpan (int) // set the time interval for timed location. Unit: ms. If not set or the set integer is less than 1000 (ms), the first positioning mode is used. Each time requestLocatin () is called, the SDK initiates a location. The Request Location corresponds to the listening result one by one; if the set integer is greater than or equal to 1000 (MS), the positioning SDK internally uses the timed location mode. 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 is initiated and the last location is returned. If the location is changed, the network request is located, get the new positioning result. When you call requestLocation at a scheduled time, the system periodically listens to the location result. After the timing positioning, you can be eager to change to a positioning, You need to reset the time interval is less than 1000 (MS. After the locationClient object is stopped, it will not be located. If you call requestLocation () multiple times after setting the scheduled location mode, the location is located at intervals, and the additional location request is also located, however, the frequency does not exceed 1 second.

Import android. content. Context;

Import com. baidu. location. BDLocation;
Import com. baidu. location. BDLocationListener;
Import com. baidu. location. LocationClient;
Import com. baidu. location. LocationClientOption;
Import com. palmdeal. entity. LocationInfo. srepository;
Import com. palmdeal. util. Logger;

Public class LocationProvider {
Private static LocationClient mLocationClient = null;

Private static SItude station = new SItude ();
Private static MyBDListener listener = new MyBDListener ();

Context context;

Public LocationProvider (Context context ){
Super ();
This. context = context;
}

Public void startLocation (){
MLocationClient = new LocationClient (context );
LocationClientOption option = new LocationClientOption ();
Option. setOpenGps (true); // enable gps
Option. setCoorType ("bd09ll"); // set the coordinate type to bd09ll.
Option. setPriority (LocationClientOption. NetWorkFirst); // sets the network priority.
Option. setProdName ("demo"); // set the product line name
MLocationClient. setLocOption (option );
MLocationClient. registerLocationListener (listener );
MLocationClient. start (); // separate the Enable and obtain location, so that you can obtain the location as much as possible in subsequent use.
}

/**
* Stop to reduce resource consumption
*/
Public void stopListener (){
If (mLocationClient! = Null & mLocationClient. isStarted ()){
MLocationClient. stop ();
MLocationClient = null;
}
}

/**
* Update the location and save it to ssung.
*/
Public void updateListener (){
If (mLocationClient! = Null & mLocationClient. isStarted ()){
MLocationClient. requestLocation ();
Logger. I ("update the location ");
}
}

/**
* Obtain latitude and longitude information
*
* @ Return
*/
Public SItude getLocation (){
Return station;
}

Private static class MyBDListener implements BDLocationListener {

@ Override
Public void onReceiveLocation (BDLocation location ){
If (location. getCity () = null ){
Int type = mLocationClient. requestLocation ();
Logger. e ("first request false" + type );
}
Station. latitude = location. getLatitude ();
Station. longpolling = location. getlongpolling ();
}

@ Override
Public void onReceivePoi (BDLocation arg0 ){
// Return
}

}
}

Public class LocationInfo {
/** Longitude and latitude struct */
Public static class s0000 {
/** Latitude */
Public double latitude;
/** Longitude */
Public double longpolling;
}
}

In use, the LocationClient is enabled at the beginning, that is, the startLocation () method is called. I called it in onCreate in the service, and then used it in the Code as follows:

LocationInfo. s0000station = location. getLocation ();
If (station. latitude = 0.0 & station. longpolling = 0.0 ){
Location. updateListener ();
Station = location. getLocation ();
}
If (station. latitude = 0.0 & station. longpolling = 0.0 ){
Return "the location is not set to your current location. Please try again ";
}
Location. stopListener ();

After the above method is used, the location information can be obtained for the first time.

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.