Android uses Baidu Positioning SDK method and error handling

Source: Internet
Author: User

Before the location of my project using the base station method, the use of Google provided API, but the day before noon suddenly did not return data, to search the internet to know that Google's interface does not provide services, based on the urgency of the time with Baidu's existing SDK, But in the process of using the first time to get the position is always null, after many experiments finally succeeded. Of course, if you need a precise location, you can add the position offset algorithm. My application to this request is not high, did not do, a search a lot, not much to say.

The following passage from Baidu map api> positioning SDK

Baidu map Positioning SDK free to open, no need to apply for key. Before using the Baidu Positioning SDK, I would like to first read the Baidu positioning map API. If used, it shall be deemed to be a full acceptance of the terms of use and shall agree to be bound by the terms of this agreement.
At present, the Baidu Map positioning SDK only supports Android and Symbian volume platform, other products are open.

The following example is part of the code that uses the Android platform. For this platform Baidu's open staff has written a complete demo, the project into the eclipse after the general no error, if the error, eclipse will also give hints. In general, you can change the name of the Propertie.properties file to Default.properties, if there are errors, show the project activity error AH what, is the SDK version of the problem is wrong, You can see <USES-SDK android:minsdkversion= "8"/> Find this minimum value through his manifest file. For example, I downloaded 2.6 of his minimum version is 5, my eclipse default version is 8, you can right-click the project, select the bottom of the properties item, and then in the Pop-up dialog box to the right of the list to select Android, on the left, select API Level 5, That's 2.0, then OK. There's a problem with the wood.

Here's a look at the steps to use the API:
You can also view the Baidu Positioning SDK's own development guide

1, the first step is to match the environment:
① first Baidu demo in the Libs folder copied to their own project. (Don't forget to build the jar package path)
② 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>

To the application node of the manifest
Next copy of Baidu Status SDK required permission

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

③ next can be assured in the code to use the Baidu Positioning SDK.
Here is a reminder from Baidu. Note that fifth, because we often do not locate the location for the first time:

1. You need to ensure that the program is compiled and passed. If you have questions about your own code, you can download the official Advanced sample to view the standard source code.

2. Make sure the network is unobstructed, whether it is connecting to WiFi or using the 2g/3g signal.

3. The call to locate the SDK must be in the main thread.

4, you must set the parameters before the positioning SDK start, such as whether to use GPS, scanning interval settings and so on. We strongly recommend that you set up your own prodname and keep it safe so that we are able to provide you with better location services.

5, positioning the SDK start immediately after execution, in this case it is difficult to locate success, because the location SDK has just started to boot has not acquired the location information. At this point getlocation is generally null. If you are trying to get a location that is successful, you can add a listerner to determine if Strdata is empty, then start the location again.

6, the positioning coverage of about 98%. This means that there are 2% possible servers without data, so the location will fail. Just go somewhere else, or try a few more times to successfully locate.

7, please use the real machine. The location test cannot be performed on the virtual machine.

Here's my case code:
The specific idea is: to open and get the location of the code separately to do, you can start the program when it started. Because according to the above, it can sometimes take two minutes to get to the location, which is absolutely not possible for us to show the information. If the main line measuring modules two minutes the consequences imaginable.
The name is: I do not set the time interval public void Setscanspan (int)//Set the time interval for timed positioning. Unit MS, if not set or if the integer value set is less than (MS), the first positioning mode is used. Each time Requestlocatin () is called, the Location SDK initiates a location. The request location corresponds to the listening result one by one, and if the integer value is set to be 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-based SDK finds no change based on the position, the network request is not initiated, the result of the last location is returned, and if the location change is found, the network request is positioned to obtain the new positioning result. When the timing is fixed, the requestlocation is called once, and the locating result is heard regularly. After setting the timing positioning, it can be eagerly replaced by a single position, and the time interval of less than (MS) will need to be reset. After the Locationclient object stops, it will no longer be positioned. If the timed positioning mode is set, the Requestlocation () is called multiple times, and the additional location requests are positioned at intervals, but not more than 1 seconds at a time.

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.SItude;
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);//Turn on GPS
         Option.setcoortype ("Bd09ll"); Set the coordinate type to BD09LL
        option.setpriority ( Locationclientoption.networkfirst); Set Network priority
        Option.setprodname ("demo");//Set product line name
         mlocationclient.setlocoption (option);
        Mlocationclient.registerlocationlistener (listener);
        Mlocationclient.start ();//will open with the acquisition location, you can try to get the position in the later use
   }

   /**
     * Stop, reduce resource consumption
     */
     public void Stoplistener () {
        if (mlocationclient! = null & & Mlocationclient.isstarted ()) {
            Mlocationclient.stop ();
            mlocationclient = null;
       }
   }

/**
* Update location and save to Situde
*/
public void Updatelistener () {
if (mlocationclient! = null && mlocationclient.isstarted ()) {
Mlocationclient.requestlocation ();
LOGGER.I ("Update the location");
}
}

/**
* Get 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.longitude = Location.getlongitude ( );
       }

@Override
public void Onreceivepoi (Bdlocation arg0) {
Return
}

}
}

public class Locationinfo {
/** latitude and longitude information structure body */
public static class Situde {
/** Latitude */
public double latitude;
/** Longitude */
public double longitude;
}
}

When used, the locationclient is opened at the beginning, i.e. the Startlocation () method is called. Mine is called in OnCreate in the service service and is then used in code like this:

Locationinfo.situde station = Location.getlocation ();
if (station.latitude = = 0.0 && Station.longitude = = 0.0) {
Location.updatelistener ();
Station = Location.getlocation ();
}
if (station.latitude = = 0.0 && Station.longitude = = 0.0) {
Return "not located in your current location, please try again";
}
Location.stoplistener ();

Using the method above, you can get the location information for the first time.

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.