登录百度开发者帐号后下载sdk导入自己的工程中。
代码如下:
1 package com.lixu.baidu_gps; 2 3 import com.baidu.location.BDLocation; 4 import com.baidu.location.BDLocationListener; 5 import com.baidu.location.LocationClient; 6 import com.baidu.location.LocationClientOption; 7 import com.baidu.mapapi.SDKInitializer; 8 import com.baidu.mapapi.map.BaiduMap; 9 import com.baidu.mapapi.map.MapStatusUpdate; 10 import com.baidu.mapapi.map.MapStatusUpdateFactory; 11 import com.baidu.mapapi.map.MapView; 12 import com.baidu.mapapi.map.MyLocationData; 13 import com.baidu.mapapi.model.LatLng; 14 import com.baidu.mapapi.search.geocode.GeoCodeOption; 15 import com.baidu.mapapi.search.geocode.GeoCodeResult; 16 import com.baidu.mapapi.search.geocode.GeoCoder; 17 import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener; 18 import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult; 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.view.View; 22 import android.view.View.OnClickListener; 23 import android.widget.Button; 24 import android.widget.EditText; 25 import android.widget.Toast; 26 27 public class MainActivity extends Activity { 28 private LocationClient mLocationClient = null; 29 private BaiduMap mBaiduMap = null; 30 private GeoCoder geocoder; 31 private EditText et1, et2; 32 33 @Override 34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 37 SDKInitializer.initialize(getApplicationContext()); 38 39 setContentView(R.layout.activity_main); 40 41 mLocationClient = new LocationClient(this); 42 // 获取百度地图的视图控件 43 MapView baidumapview = (MapView) findViewById(R.id.baidumv); 44 45 mBaiduMap = baidumapview.getMap(); 46 // 设置地图指针 47 mBaiduMap.setMyLocationEnabled(true); 48 49 // 处理百度地图开源项目的定位偏差 50 LocationClientOption option = new LocationClientOption(); 51 option.setCoorType("bd09ll"); 52 53 mLocationClient.setLocOption(option); 54 55 MyLocationListenner myListenner = new MyLocationListenner(); 56 mLocationClient.registerLocationListener(myListenner); 57 mLocationClient.start(); 58 59 geocoder = GeoCoder.newInstance(); 60 61 geocoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() { 62 63 @Override 64 public void onGetReverseGeoCodeResult(ReverseGeoCodeResult location) { 65 } 66 67 @Override 68 public void onGetGeoCodeResult(GeoCodeResult location) { 69 // 根据输入地址来获得经纬度 70 LatLng ll = location.getLocation(); 71 // 获取精度纬度完成定位 72 MyLocationData mMyLocationData = new MyLocationData.Builder().latitude(ll.latitude) 73 .longitude(ll.longitude).build(); 74 75 // 设置定位球的位置 76 mBaiduMap.setMyLocationData(mMyLocationData); 77 // 移动定位球到中心位置 78 79 MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(ll); 80 mBaiduMap.animateMapStatus(msu); 81 82 } 83 }); 84 85 et1 = (EditText) findViewById(R.id.et1); 86 87 et2 = (EditText) findViewById(R.id.et2); 88 89 Button btn = (Button) findViewById(R.id.sousuo); 90 91 btn.setOnClickListener(new OnClickListener() { 92 93 @Override 94 public void onClick(View v) { 95 if (et1.getText().toString().equals("") || et2.getText().toString().equals("")) { 96 Toast.makeText(getApplicationContext(), "请输入地址", 0).show(); 97 } else 98 geocoder.geocode( 99 new GeoCodeOption().city(et1.getText().toString()).address(et2.getText().toString()));100 101 }102 });103 104 }105 106 private class MyLocationListenner implements BDLocationListener {107 108 @Override109 public void onReceiveLocation(BDLocation location) {110 // 获取精度纬度完成定位111 MyLocationData mMyLocationData = new MyLocationData.Builder().latitude(location.getLatitude())112 .longitude(location.getLongitude()).build();113 114 // 设置定位球的位置115 mBaiduMap.setMyLocationData(mMyLocationData);116 // 移动定位球到中心位置117 LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());118 MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(ll);119 mBaiduMap.animateMapStatus(msu);120 121 }122 123 }124 125 // 程序退出时关闭126 @Override127 protected void onDestroy() {128 mLocationClient.stop();129 mBaiduMap.clear();130 geocoder.destroy();131 super.onDestroy();132 }133 134 }
xml文件和Mainfest文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.lixu.baidu_gps" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="19" 9 android:targetSdkVersion="19" />10 11 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >12 </uses-permission>13 <uses-permission android:name="android.permission.INTERNET" >14 </uses-permission>15 <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />16 <uses-permission android:name="android.permission.WAKE_LOCK" >17 </uses-permission>18 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />19 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />20 <!-- SDK1.5需要android.permission.GET_TASKS权限判断本程序是否为当前运行的应用? -->21 <uses-permission android:name="android.permission.GET_TASKS" />22 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >23 </uses-permission>24 <uses-permission android:name="android.permission.WRITE_SETTINGS" />25 <!-- 这个权限用于进行网络定位 -->26 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >27 </uses-permission>28 <!-- 这个权限用于访问GPS定位 -->29 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >30 </uses-permission>31 <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->32 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >33 </uses-permission>34 <!-- 获取运营商信息,用于支持提供运营商信息相关的接口 -->35 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >36 </uses-permission>37 <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 -->38 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >39 </uses-permission>40 <!-- 用于读取手机当前的状态 -->41 <uses-permission android:name="android.permission.READ_PHONE_STATE" >42 </uses-permission>43 <!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据 -->44 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >45 </uses-permission>46 <!-- 访问网络,网络定位需要上网 -->47 <uses-permission android:name="android.permission.INTERNET" />48 <!-- SD卡读取权限,用户写入离线定位数据 -->49 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >50 </uses-permission>51 <!-- 允许应用读取低级别的系统日志文件 -->52 <uses-permission android:name="android.permission.READ_LOGS" >53 </uses-permission>54 55 <application56 android:allowBackup="true"57 android:icon="@drawable/ic_launcher"58 android:label="@string/app_name"59 android:theme="@style/AppTheme" >60 <activity61 android:name=".MainActivity"62 android:label="@string/app_name" >63 <intent-filter>64 <action android:name="android.intent.action.MAIN" />65 66 <category android:name="android.intent.category.LAUNCHER" />67 </intent-filter>68 </activity>69 70 <meta-data71 android:name="com.baidu.lbsapi.API_KEY"72 android:value="TkWtCcaqum3XHoUSHwjHTVoA" />73 74 <service75 android:name="com.baidu.location.f"76 android:enabled="true"77 android:process=":remote" >78 </service>79 </application>80 81 </manifest>
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <LinearLayout 7 android:id="@+id/ll" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content"10 android:orientation="horizontal" >11 12 <EditText13 android:id="@+id/et1"14 android:layout_weight="1"15 android:layout_width="wrap_content"16 android:layout_height="wrap_content"17 android:hint="请输入城市名" />18 <EditText19 android:id="@+id/et2"20 android:layout_weight="1"21 android:layout_width="wrap_content"22 android:layout_height="wrap_content"23 android:hint="请输入街道名" />24 25 <Button26 android:id="@+id/sousuo"27 android:layout_width="wrap_content"28 android:layout_height="wrap_content"29 android:text="搜索" />30 </LinearLayout>31 32 <com.baidu.mapapi.map.MapView33 android:id="@+id/baidumv"34 android:layout_below="@id/ll"35 android:layout_width="match_parent"36 android:layout_height="match_parent" />37 38 </RelativeLayout>
运行效果图: