Study on Baidu Map Development (II.)--Map location

Source: Internet
Author: User

Positioning is an important part of the development of the map, I am also constantly learning, I know how to write these.

First, the configuration

1. Baidu is positioned to have its own jar package and so files, so remember to configure them in the corresponding folder and loaded into the project. With the loading method in the learning of a inside there is, here is not detailed.

2. After importing the library file in the configuration method of Baidu, also make a declaration libs source file

If the jar package and so files are all put into the Libs file, you need such a declaration, I have tried, but the format is a bit biased, Build.gradle will error, change to the following so that you can.

sourcesets {
Main {
Jnilibs.srcdir ' Libs '
}

3. Add Service

Location services are opened as a service and need to be added to the androidmainfest.xml.

<service
Android:name= "COM.BAIDU.LOCATION.F"
Android:enabled= "true"
android:process= ": Remote" >
</service>


4. Add Permissions
Prior to learning one has already included the following required permissions, so you can refer to the previous, here is not attached.

second, familiar with the main use of the positioning of the class1.LocationClient

This is a class of location services, equivalent to defining a client with a location service and can only be started in the main thread

It has two constructors, all need to pass into the global context, it is recommended to use Getapplicationcontext to pass in

These two constructors differ: whether the locationclientoption that configure the positional parameters are defined before the creation process. If you adopt the first construction method, then you need to use setlocoption to supplement it later.

Common methods are as follows:

void setLocOption(LocationClientOption locOption) Set Locationclientoption
void start() Launch the Location SDK
void stop() Stop locating the SDK
void registerLocationListener(BDLocationListener listener) Registering the location listener function
void registerNotify(BDNotifyListener mNotify) Register Location Alert Listener
void requestNotifyLocation() NA
void removeNotifyEvent(BDNotifyListener mNotify) Cancellation of registered location Alert listener
Int requestofflinelocation () Offline location request, asynchronous return, results obtained in Locationlistener.

In general, after constructing and setting parameters, you can call the Start method and start the location service.

2.LocationClientOption

Ability to configure various parameters of the positioning SDK

Common methods:

void setCoorType(java.lang.String coorType) Get the current set of coordinate types
void SetIgnoreCacheException(boolean cacheException) Set whether exception snapping is true: do not catch exceptions, false: Catch exceptions, default to False
void setIgnoreKillProcess(boolean killProcess) Set whether to exit the location process true: Do not exit the process; false: Exit process, true by default
void setIsNeedAddress(boolean isNeed) Set whether address information is required, default to no address
void setIsNeedLocationPoiList(boolean isNeedLocationPoiList) Set whether you need to return location POI information, you can get the data in Bdlocation.getpoilist ()
void setLocationMode(LocationClientOption.LocationMode mode) Setting the positioning mode
void setNeedDeviceDirect(boolean isNeedDeviceDirect) When the network is located, whether device orientation is required true: required; False: Not required.
void setOpenGps(boolean openGps) Set whether GPS is turned on for positioning
void setProdName(java.lang.String prodName) Setting the Prod field value
void setScanSpan(int scanSpan) Set the scan interval in milliseconds when <1000 (1s), timed positioning is not valid

Common settings are positioning mode, positioning coordinate system, scan interval, return address results, whether to open GPS, positioning mode.

Positioning mode (default high precision):

(1) High-precision positioning mode (hight_accuracy): The use of network positioning and GPS positioning, priority to return to the highest precision positioning results.

(2) Low power positioning mode (battery_saving): Do not use GPS, only use network location (WiFi and base station).

(3) Only with device positioning mode (device_sensors): not suitable for network location, only use GPS to locate. However, the location of the indoor environment is not supported in this mode.

Positioning coordinate system (recommended to use BD09LL, Baidu map with its own coordinate comparison ... ):

(1) Return to geodetic latitude coordinate system: gcj02

(2) Back to Baidu Mercator coordinate system: BD09

(3) Return Baidu latitude and longitude coordinate system: BD09LL

Some understanding of coordinate system I will also say in the next blog ...

Scan interval (in ms): Set scan interval in milliseconds when <1000 (1s), timed positioning is not valid

Return to positioning Result: Set whether address information is required, default no address

Whether to turn on the GPS: positioning generally need to open

3. Bdlocationlistener Location Request Callback Interface

After setting the positioning parameters, it is necessary to set the listening events, mainly overridingonReceiveLocation(BDLocation location)函数,实现对于location结果的处理。

4.BDLocation Internal package of latitude and longitude, radius and other properties.

Common methods:

java.lang.String getAddrStr() Get Detailed address information
double getAltitude() Get height information, currently only valid for GPS positioning results, unit meters (not yet implemented)
java.lang.String getCity() Get the city
java.lang.String getCountry() Get Country
double getLatitude() Get latitude coordinates
double getLongitude() Get Longitude coordinates
java.lang.String getProvince() Get Province
float getSpeed() Get speed, only GPS location results with speed information, unit km/h, default value 0.0f
java.lang.String getStreet() Get Street Information
java.lang.String getTime() The current location time returned by the server
float getRadius() Get positioning accuracy, default value 0.0f
float getDirection() GPS positioning results, the direction of travel, Unit degree

Frequently obtained information: latitude, accuracy range, direction of travel

5.MyLocationData Positioning Packets

The previous class completes the map's positioning and listening responses, and if it is to be displayed in the map, it is necessary to construct the positioning packet to display the current location.

MyLocationData.Builder accuracy(float accuracy) Set accuracy information for positioning data, in meters per unit
MyLocationData build() Building a build Location data object
MyLocationData.Builder direction(float direction) Set orientation information for positioning data
MyLocationData.Builder latitude(double lat) Set latitude for positioning data
MyLocationData.Builder longitude(double lng) Set longitude for location data
MyLocationData.Builder satellitesNum(int num) Number of satellites setting location data
MyLocationData.Builder speed(float speed) Set the speed of positioning data

These methods are commonly used in precision, latitude and longitude, direction, the corresponding data acquisition can be Bdlocation method obtained.

6.MyLocationConfiguration Configuring the anchor layer

构造函数MyLocationConfiguration(MyLocationConfiguration.LocationMode mode, boolean enableDirection, BitmapDescriptor customMarker)

Display Mode (Locationmode):

With the above basic classes you can simply use location services.

What other types of search can be found on the official website

Http://wiki.lbsyun.baidu.com/cms/androidloc/doc/v6_0_3/doc/index.html

The core code is attached:

 mlocclient = new Locationclient (this);//define Location client 
Mlocclient.registerlocationlistener (MyListener);//Set Listener events
locationclientoption option = new Locationclientoption (),//Set parameter
Option.setopengps (TRUE);//Turn on GPS
Option.setcoortype ("Bd09ll"); Set the coordinate type
Option.setscanspan (1000);//facility Scan interval
mlocclient.setlocoption (option);//Add parameters to client
Mlocclient.start ();//Open client

 public class Mylocationlistenner implements Bdlocationlistener {

@Override
public void Onreceiveloc ation (bdlocation location) {
//Map view is not processing the newly received position after destruction
if (locations = = NULL | | mmapview = = NULL) { return;
}
Mylocationdata locdata = new Mylocationdata.builder ()
. Accuracy (Location.getra Dius ())
. Direction ()
. Latitude (Location.getlatitude ())
. Longitude (lo Cation.getlongitude ())
. Build ();
Mbaidumap.setmylocationdata (locdata);
if (Isfirstloc) {
Isfirstloc = false;
LATLNG ll = new Latlng (Location.getlatitude (),
Location.getlongitude ());
Mtoast = Toast.maketext (Locationdemo.this, "first position:" +ll.latitude + "|" + Ll.longitude, toast.length_short);
Mtoast.show ();
Mapstatus.builder Builder = new Mapstatus.builder ();
Builder.target (LL). Zoom (18.0f);
Mbaidumap.animatemapstatus (Mapstatusupdatefactory.newmapstatus (Builder.build ()));
}
}

Study on Baidu Map Development (II.)--Map 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.