Baidu map SDK V3.2 and Baidu positioning SDK V4.2 complete the positioning function, sdkv4.2

Source: Internet
Author: User

Baidu map SDK V3.2 and Baidu positioning SDK V4.2 complete the positioning function, sdkv4.2

Baidu map SDK V3.2 and Baidu positioning SDK V4.2 complete Positioning

1. To complete the positioning function, download Baidu map SDK (baidumapapi_v3_2_0.jar; libBaiduMapSDK_v3_2_0_15.so) and Baidu positioning SDK (locSDK_4.2.jar; liblocSDK4d. so), you need to download the library and jar package such as above on the official website. Right-click the jar package and add it to build path.


2. Add the required key, service, and permissions to the manifest. xml file. If no service is added. The locating service cannot be started, causing the callback oncancelocation to fail.



Let's look at the Code:

The main code in acticity is as follows:


/**
* Client to be located
*/
Private LocationClient mLocationClient;
/**
* Listener to be located
*/
Public MyLocationListener mMyLocationListener;
/**
* Current positioning Mode
*/
Private LocationMode mCurrentMode = LocationMode. NORMAL;
/***
* Is it the first time to locate
*/
Private volatile boolean isFristLocation = true;


/**
* The latest longitude and latitude
*/
Private double mCurrentLantitude;
Private double mcurrentlongpolling;
/**
* Current precision
*/
Private float mCurrentAccracy;


// Initialize positioning information
Private void initMyLocation ()
{
// Locate Initialization
MLocationClient = new LocationClient (this );

If (mLocationClient = null ){
Log. I (TAG, "mLocationClient is null ");
Return;
}

MMyLocationListener = new MyLocationListener ();
MLocationClient. registerLocationListener (mMyLocationListener );
// Configure Positioning
LocationClientOption option = new LocationClientOption ();
Option. setOpenGps (true); // enable gps
Option. setCoorType ("bd09ll"); // sets the coordinate type.
Option. setScanSpan (5000); // 5S positioned once
MLocationClient. setLocOption (option );
}


/**
* Real-time location callback listening
*/
Public class MyLocationListener implements BDLocationListener
{
@ Override
Public void onReceiveLocation (BDLocation location)
{

Log. I (TAG, "oncancelocation ");

// After map view is destroyed, it is not processed in the new receiving location
If (location = null | mMapView = null)
Return;
// Construct the positioning data
MyLocationData locData = new MyLocationData. Builder ()
. Accuracy (location. getRadius ())
// Set the direction information obtained by the developer, Which is 0-360 clockwise.
. Direction (mXDirection). latitude (location. getLatitude ())
. Longpolling (location. getlongpolling (). build ();
MCurrentAccracy = location. getRadius ();
// Set the positioning data
MBaiduMap. setMyLocationData (locData );
MCurrentLantitude = location. getLatitude ();
Mcurrentlongparts = location. getlongparts ();
// Set the custom icon
BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory
. FromResource (R. drawable. navi_map_gps_locked); // The icon resource of the current position displayed.
MyLocationConfiguration config = new MyLocationConfiguration (
MCurrentMode, true, mCurrentMarker );
MBaiduMap. setMyLocationConfigeration (config );

Log. I (TAG, "CURRENT LAT->" + mCurrentLantitude + "; LON->" + mcurrentlong.pdf );

// When positioning for the first time, move the map location to the current location
If (isFristLocation)
{
IsFristLocation = false;
LatLng ll = new LatLng (location. getLatitude (),
Location. getlongpolling ());

MapStatus mMapStatus = new MapStatus.Builder().tar get (ll)
. Zoom (18). build ();
// Define the MapStatusUpdate object to describe the changes in the MAP status

MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory
. NewMapStatus (mMapStatus );
// Change the MAP status
MBaiduMap. setMapStatus (mMapStatusUpdate );

// MapStatusUpdate u = MapStatusUpdateFactory. newLatLng (ll );
// MBaiduMap. animateMapStatus (u );
}
}
}


@ Override
Protected void onStart ()
{
// Enable Layer Positioning
MBaiduMap. setMyLocationEnabled (true );
If (! MLocationClient. isStarted ())
{
MLocationClient. start ();
Int I = mLocationClient. requestLocation ();
Log. I (TAG, "mLocationClient started -->" + I );
}

Super. onStart ();
}


@ Override
Protected void onStop ()
{
// Close layer Positioning
MBaiduMap. setMyLocationEnabled (false );
MLocationClient. stop ();

Super. onStop ();
}



The manifest. xml file code is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. example. getsmsinfo"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">


<Uses-sdk
Android: minSdkVersion = "8"
Android: targetSdkVersion = "17"/>

<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
<Uses-permission android: name = "android. permission. READ_EXTERNAL_STORAGE"/>
<Uses-permission android: name = "android. permission. READ_SMS"/>
<Uses-permission android: name = "android. permission. RECEIVE_SMS"/>

<Uses-permission android: name = "android. permission. GET_ACCOUNTS"/>
<Uses-permission android: name = "android. permission. USE_CREDENTIALS"/>
<Uses-permission android: name = "android. permission. MANAGE_ACCOUNTS"/>
<Uses-permission android: name = "android. permission. AUTHENTICATE_ACCOUNTS"/>
<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/>
<Uses-permission android: name = "android. permission. INTERNET"/>
<Uses-permission android: name = "com. android. launcher. permission. READ_SETTINGS"/>
<Uses-permission android: name = "android. permission. CHANGE_WIFI_STATE"/>
<Uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"/>
<Uses-permission android: name = "android. permission. READ_PHONE_STATE"/>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
<Uses-permission android: name = "android. permission. BROADCAST_STICKY"/>
<Uses-permission android: name = "android. permission. WRITE_SETTINGS"/>
<Uses-permission android: name = "android. permission. READ_PHONE_STATE"/>
<Uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/>
<Uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION"/>
 


<Application
Android: allowBackup = "true"
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name"
Android: theme = "@ style/AppTheme">

<Meta-data
Android: name = "com. baidu. lbsapi. API_KEY"
Android: value = "the KEY you applied for"/>

<Service <! -- Sevice must be added here. Otherwise, the oncancelocation callback fails. -->
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_v4.2"> --> 4.2 corresponds to your locSDK version.
</Action>
</Intent-filter>
</Service>

<Activity
Android: name = "com. example. getsmsinfo. MainActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>


<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>


</Application>


</Manifest>








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.