Baidu map Development Learning (2)-map positioning, Baidu Map

Source: Internet
Author: User

Baidu map Development Learning (2)-map positioning, Baidu Map

Positioning is an essential part of map development. As I keep learning about it, I will understand it myself.

I. Configuration

1. Baidu has its own jar package and so files, so remember to configure them in the corresponding folder and load them into the project. The loading method is available in learning. I will not elaborate on it here.

2. After importing the library file in Baidu's configuration method, declare the libs source file.

If you want to put all the jar package and so files into the libs file, you need to declare this. I have tried it, but the format is somewhat different. gradle will report an error. Change it to the following.

sourceSets {
main {
jniLibs.srcDir 'libs'
}

3. Add a service

The location service is enabled as a service and needs to be added to AndroidMainfest. xml.

<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>


4. Add Permissions
Previous study No. 1 has included the required permissions. Therefore, you can refer to the previous ones and will not attach them here.

 

2. Familiar with the main positioning classes used 1. LocationClient

This is a class for locating services. It is equivalent to defining a client with locating services and can only be started in the main thread.

It has two constructors, both of which need to be passed into the global context. We recommend that you use getApplicationContext to pass in.

The difference between the two constructors: whether the locationClientOption of the positioning parameter is defined before the creation process. If the first constructor is used, setLocOption must be used later.

The common method is as follows:

Void setLocOption(LocationClientOption locOption) Set LocationClientOption
Void start() Start positioning sdk
Void stop() Stop positioning sdk
Void registerLocationListener(BDLocationListener listener) Register the positioning listening Function
Void registerNotify(BDNotifyListener mNotify) Register location reminder listener
Void requestNotifyLocation() NA
Void removeNotifyEvent(BDNotifyListener mNotify) The location reminder listener for canceling registration
Int RequestOfflineLocation() The request is located offline and returned asynchronously. The result is obtained in the locationListener.

Generally, after constructing and setting parameters, you can call the start method to start the locating service.

2. LocationClientOption

Ability to configure and locate SDK Parameters

Common Methods:

Void setCoorType(java.lang.String coorType) Obtains the currently set coordinate type.
Void SetIgnoreCacheException(boolean cacheException) Set whether to capture exceptions. true: do not capture exceptions. false: capture exceptions. The default value is false.
Void setIgnoreKillProcess(boolean killProcess) Set whether to exit the Locating Process. true: Do not exit the process; false: exit the process. The default value is true.
Void setIsNeedAddress(boolean isNeed) Set whether address information is required. The default value is "no address ".
Void setIsNeedLocationPoiList(boolean isNeedLocationPoiList) Set whether to return location POI information. You can get data in BDLocation. getPoiList ().
Void setLocationMode(LocationClientOption.LocationMode mode) Set positioning Mode
Void setNeedDeviceDirect(boolean isNeedDeviceDirect) When locating the network, whether the device direction is true: required; false: not required.
Void setOpenGps(boolean openGps) Set whether to enable gps for locating
Void setProdName(java.lang.String prodName) Set the Prod Field Value
Void setScanSpan(int scanSpan) Sets the scan interval. The unit is millisecond. When <1000 (1 s), the scheduled location is invalid.

Common settings include positioning mode, positioning coordinate system, scanning interval, return address result, whether to enable GPS, and positioning mode.

Positioning mode (high accuracy by default ):

(1) high-precision positioning mode (Hight_Accuracy): uses both Network Positioning and GPS positioning to return the highest-precision positioning results first.

(2) Low Power positioning mode (Battery_Saving): Only Network Positioning (WiFi and base station) is used without GPS ).

(3) only use the device positioning mode (Device_Sensors): Network Positioning is not applicable, and GPS is only used for positioning. However, this mode does not support indoor environment positioning.

Positioning coordinate system (bd09ll is recommended, and Baidu map is more accurate with its own coordinates ...) :

(1) return to the longitude and latitude Coordinate System of the local communications administration: gcj02

(2) return to Baidu mocato Coordinate System: bd09

(3) return the coordinate system of Baidu longitude and latitude: bd09ll

I will also talk about the coordinate system in the next blog...

Scan interval (unit: ms): sets the scan interval. Unit: milliseconds. When <1000 (1 s), the scheduled location is invalid.

Return Positioning Result: Set whether address information is required. No address is provided by default.

Whether to enable GPS: Generally, GPS must be enabled for positioning.

3. BDLocationListener: Request callback Interface

After positioning parameters are set, you need to set listening events, mainly to overrideOnReceiveLocation(BDLocation location) function to process location results.

4. BDLocation contains attributes such as latitude and longitude, radius, and so on.

Common Methods:

java.lang.String getAddrStr() Obtain detailed address information
double getAltitude() Obtains the height information. Currently, this parameter is valid only when the GPS positioning result is obtained, in meters (not implemented yet)
java.lang.String getCity() Get City
java.lang.String getCountry() Obtain country
double getLatitude() Obtain latitude coordinates
double getLongitude() Obtain longitude coordinates
java.lang.String getProvince() Obtain Province
float getSpeed() Obtain the speed. Only the gps positioning result has the speed information, in kilometers/hour. The default value is 0.0f.
java.lang.String getStreet() Obtain street Information
java.lang.String getTime() Current location time returned by server
float getRadius() Obtains positioning accuracy. The default value is 0.0f.
float getDirection() Gps Positioning Result, traveling direction, Unit

Frequently obtained information: longitude and latitude, precision range, and traveling direction

5. MyLocationData Locating data packets

The previous class completes the positioning and listening of the map. If you want to display it in the map, you need to construct a positioning data packet to display the current location.

MyLocationData.Builder accuracy(float accuracy) Sets the precision of the positioning data, in meters.
MyLocationData build() Build and generate positioning data objects
MyLocationData.Builder direction(float direction) Set the direction of the positioning data
MyLocationData.Builder latitude(double lat) Set the positioning data latitude
MyLocationData.Builder longitude(double lng) Set the longitude of the positioning data
MyLocationData.Builder satellitesNum(int num) Set the number of satellites for positioning data
MyLocationData.Builder speed(float speed) Set the data locating speed

These methods are commonly used for precision, latitude and longitude, and direction. Corresponding data can be obtained using the BDLocation method.

6. Configure the positioning layer in MyLocationConfiguration

Constructor MyLocationConfiguration (MyLocationConfiguration. LocationMode mode, boolean enableDirection, BitmapDescriptor customMarker)

Display Mode ):

With the above several basic classes, you can simply use the positioning service.

You can also go to the official website to search for any category you need.

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

 

The core code is attached:

 
MLocClient = new LocationClient (this); // defines the client location
MLocClient. registerLocationListener (myListener); // sets the listener event
LocationClientOption option = new LocationClientOption (); // set the Parameter
Option. setOpenGps (true); // enable gps
Option. setCoorType ("bd09ll"); // sets the coordinate type.
Option. setScanSpan (1000); // facility scan Interval
MLocClient. setLocOption (option); // Add the parameter to the client
MLocClient. start (); // enable the client

Public class MyLocationListenner implements BDLocationListener {

@ Override
Public void onReceiveLocation (BDLocation location ){
// After map view is destroyed, it is not processed in the new receiving location
If (location = null | mMapView = null ){
Return;
}
MyLocationData locData = new MyLocationData. Builder ()
. Accuracy (location. getRadius ())
. Direction (100)
. Latitude (location. getLatitude ())
. Longpolling (location. getlongpolling ())
. Build ();
MBaiduMap. setMyLocationData (locData );
If (isFirstLoc ){
IsFirstLoc = false;
LatLng ll = new LatLng (location. getLatitude (),
Location. getlongpolling ());
// MToast = Toast. makeText (LocationDemo. this, "First time location:" + ll. latitude + "|" + ll. longpolling, Toast. LENGTH_SHORT );
// MToast. show ();
MapStatus. Builder builder = new MapStatus. Builder ();
Builder.tar get (ll). zoom (18.0f );
MBaiduMap. animateMapStatus (MapStatusUpdateFactory. newMapStatus (builder. build ()));
}
}

 

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.