Android Positioning & amp; Map & amp; navigation-Positioning Based on Baidu map, android city positioning

Source: Internet
Author: User

Android Positioning & map & navigation-Positioning Based on Baidu map, android city positioning

I. Problem Description

The LBS location service is an important feature in android applications and has become more and more widely used. Next we will gradually learn and implement lbs-related applications such as location, MAP, navigation, etc, first, let's look at how to implement the positioning function based on Baidu map.

Ii. Configure the environment

1. Registration key: Address http://developer.baidu.com/map/

2. Download and import the positioning SDK:

 

3. Compile the MyApplication class

Write the MyApplication class. For ease of use, we can encapsulate the Application component that implements the positioning method.

Encapsulate the following methods

1. Obtain the location information -- requestLocationInfo ()

2. Send location information via broadcast-sendBroadCast ()

3. Stop locating-stopLocationClient ()

Public class MyApplication extends Application {public LocationClient mLocationClient = null; public GeofenceClient mGeofenceClient; public MyLocationListenner myListener = new MyLocationListenner (); public static String TAG = "MyApplication "; private static MyApplication mInstance = null; @ Override public void onCreate () {mInstance = this; mLocationClient = new LocationClient (this);/*** project key, from Has to the official website to apply for http://lbsyun.baidu.com/apiconsole/key */mLocationClient. setAK ("your application Key"); mLocationClient. registerLocationListener (myListener); mGeofenceClient = new GeofenceClient (this); super. onCreate (); Log. d (TAG ,"... application onCreate... pid = "+ Process. myPid ();} public static MyApplication getInstance () {return mInstance;}/*** stop locating */public void stopLocationClient () {if (mLocationClient! = Null & mLocationClient. isStarted () {mLocationClient. stop () ;}/ *** Initiate location */public void requestLocationInfo () {setLocationOption (); if (mLocationClient! = Null &&! MLocationClient. isStarted () {mLocationClient. start ();} if (mLocationClient! = Null & mLocationClient. isStarted () {mLocationClient. requestLocation () ;}}/*** set parameters related to Baidu map */private void setLocationOption () {LocationClientOption option = new LocationClientOption (); option. setOpenGps (true); // enable gps option. setCoorType ("bd09ll"); // sets the coordinate type option. setServiceName ("com. baidu. location. service_v2.9 "); option. setPoiExtraInfo (true); option. setAddrType ("all"); option. setPoiNumber (10); option. disableCache (true); mLocationClient. setLocOption (option);}/*** listener function, which is formatted as a string when a location is updated, output to the screen */public class MyLocationListenner implements BDLocationListener {@ Override public void onReceiveLocation (BDLocation location) {if (location = null) {sendBroadCast ("positioning failed! "); Return;} sendBroadCast (location. getAddrStr ();} public void onReceivePoi (BDLocation poiLocation) {if (poiLocation = null) {sendBroadCast (" positioning failed! "); Return;} sendBroadCast (poiLocation. getAddrStr () ;}}/*** get send broadcast * @ param address */public void sendBroadCast (String address) {stopLocationClient (); Intent intent = new Intent (MainActivity. location_caa); intent. putExtra ("address", address); sendBroadcast (intent );}}
3. Main Program MainActivity
Public class MainActivity extends Activity {public static String TAG = "LocTestDemo"; private BroadcastReceiver broadcastReceiver; public static String location_caa = "location_caa"; private Button locBtn; private TextView locInfo; private MyApplication application; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); application = (MyApplication) super. getApplication (); initialize (); initializeViews (); initializeListeners ();} private void initialize () {registerBroadCastReceiver (); // register broadcast} private void initializeViews () {locBtn = (Button) findViewById (R. id. location); locInfo = (TextView) findViewById (R. id. location_info);} private void initializeListeners () {locBtn. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {locInfo. setText ("locating... "); // call the request locating information application. requestLocationInfo () ;}});}/*** register a broadcast, listen to the positioning result, and accept the broadcast to obtain the address information */private void registerBroadCastReceiver () {broadcastReceiver = new BroadcastReceiver () {public void onReceive (Context context, Intent intent) {String address = intent. getStringExtra ("address"); locInfo. setText (address) ;}}; IntentFilter intentToReceiveFilter = new IntentFilter (); intentToReceiveFilter. addAction (location_caa); registerReceiver (broadcastReceiver, intentToReceiveFilter);} @ Override protected void onDestroy () {super. onDestroy (); unregisterReceiver (broadcastReceiver );}}
Iv. AndroidManifest. xml configuration information
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.jereh.baidulocation"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <application        android:name="com.jereh.baidulocation.MyApplication"        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.jereh.baidulocation.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>        <service            android:name="com.baidu.location.f"            android:enabled="true"            android:process=":remote" >            <intent-filter>                <action android:name="com.baidu.location.service_v2.2" >                </action>            </intent-filter>        </service>    </application>    <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>    <uses-permission android:name="android.permission.VIBRATE" />    <uses-permission android:name="android.permission.WAKE_LOCK" />    <uses-permission android:name="android.permission.WRITE_SETTINGS" /></manifest>

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation:

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.