一.定位服務(wù)
在地圖開發(fā)中我們都會用到地圖服務(wù)。首先是要獲取位置 ,我們可以把定位這一塊 寫到服務(wù)里 在想用的時候直接獲取。
public class LocationService extends Service implements BDLocationListener {
private LocationClient mLocationClient;
public static BDLocation mBdLocation;
private LocationClientOption.LocationMode tempMode = LocationClientOption.LocationMode.Hight_Accuracy;
private String tempcoor = "bd09ll"; //"gcj02";
public BDLocation getLastKnownLocation() {
return this.mLocationClient.getLastKnownLocation();
}
public boolean isStarted() {
return this.mLocationClient.isStarted();
}
public void registerLocationListener(BDLocationListener locationListener) {
this.mLocationClient.registerLocationListener(locationListener);
}
public void unRegisterLocationListener(BDLocationListener locationListener) {
this.mLocationClient.unRegisterLocationListener(locationListener);
}
public void disconnectLocationClient() {
if (mLocationClient != null && !mLocationClient.isStarted()) {
unRegisterLocationListener(this);
this.stop();
}
}
public void setLocOption(LocationClientOption clientOption) {
this.mLocationClient.setLocOption(clientOption);
}
public void start() {
this.mLocationClient.start();
}
public void stop() {
this.mLocationClient.stop();
}
@Override
public void onCreate() {
super.onCreate();
mLocationClient = new LocationClient(this);
LocationClientOption option = new LocationClientOption();
option.setLocationMode(tempMode);
option.setCoorType(tempcoor);
option.setScanSpan(span);//可選,默認0,即僅定位一次,設(shè)置發(fā)起定位請求的間隔需要大于等于1000ms才是有效的
option.setOpenGps(true);//可選,默認false,設(shè)置是否使用gps
option.setLocationNotify(true);//可選,默認false,設(shè)置是否當gps有效時按照1S1次頻率輸出GPS結(jié)果
option.setIgnoreKillProcess(true);//可選,默認true,定位SDK內(nèi)部是一個SERVICE,并放到了獨立進程,設(shè)置是否在stop的時候殺死這個進程,默認不殺死
option.setEnableSimulateGps(false);//可選,默認false,設(shè)置是否需要過濾gps仿真結(jié)果,默認需要
option.setIsNeedLocationDescribe(true);//可選,默認false,設(shè)置是否需要位置語義化結(jié)果,可以在BDLocation.getLocationDescribe里得到,結(jié)果類似于“在北京天安門附近”
option.setIsNeedLocationPoiList(true);//可選,默認false,設(shè)置是否需要POI結(jié)果,可以在BDLocation.getPoiList里得到
option.setIsNeedAddress(true);
setLocOption(option);
registerLocationListener(this);
start();
mBdLocation = mLocationClient.getLastKnownLocation();
}
public void onDestroy() {
super.onDestroy();
disconnectLocationClient();
}
@Override
public void onReceiveLocation(BDLocation location) {
mBdLocation = location;
}
}
二.反地理編碼
反地理編碼就是通過百度的坐標定位引擎,插敘出坐標對應(yīng)的物體所在的行政區(qū)劃、街道等信息在地圖上點擊的時候,或者拖動marker的時候得到的LatLng對象,就可以通過反地理編碼得到具體的地址了。
效果如下

開發(fā)步驟(不再介紹地圖定位)
1.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.baidu.mapapi.map.MapView
android:id="@+id/main_bdmap"
android:layout_width="match_parent"
android:layout_height="250dp"
android:onClick="true"></com.baidu.mapapi.map.MapView>
<ListView
android:id="@+id/main_pois"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></ListView>
</LinearLayout>
2.獲取地理編碼檢索(有兩種方法)
(1)通過創(chuàng)建 GeoCoder 回調(diào)獲取
/**
* 定位監(jiān)聽
*
* @param bdLocation
*/
@Override
public void onReceiveLocation(BDLocation bdLocation) {
//如果bdLocation為空或mapView銷毀后不再處理新數(shù)據(jù)接收的位置
if (bdLocation == null || mBaiduMap == null) {
return;
}
//定位數(shù)據(jù)
MyLocationData data = new MyLocationData.Builder()
//定位精度bdLocation.getRadius()
.accuracy(bdLocation.getRadius())
//此處設(shè)置開發(fā)者獲取到的方向信息,順時針0-360
.direction(bdLocation.getDirection())
//經(jīng)度
.latitude(bdLocation.getLatitude())
//緯度
.longitude(bdLocation.getLongitude())
//構(gòu)建
.build();
//設(shè)置定位數(shù)據(jù)
mBaiduMap.setMyLocationData(data);
//是否是第一次定位
if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLngZoom(ll, 18);
mBaiduMap.animateMapStatus(msu);
}
//創(chuàng)建GeoCoder實例對象
geoCoder = GeoCoder.newInstance();
//發(fā)起反地理編碼請求(經(jīng)緯度->地址信息)
ReverseGeoCodeOption reverseGeoCodeOption = new ReverseGeoCodeOption();
//設(shè)置反地理編碼位置坐標
reverseGeoCodeOption.location(new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude()));
geoCoder.reverseGeoCode(reverseGeoCodeOption);
//設(shè)置查詢結(jié)果監(jiān)聽者
geoCoder.setOnGetGeoCodeResultListener(this);
}
//地理編碼查詢結(jié)果回調(diào)函數(shù)
@Override
public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
}
//反地理編碼查詢結(jié)果回調(diào)函數(shù)
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
List<PoiInfo> poiInfos = reverseGeoCodeResult.getPoiList();
PoiAdapter poiAdapter = new PoiAdapter(MainActivity.this, poiInfos);
poisLL.setAdapter(poiAdapter);
}
(2)通過Web服務(wù)API獲取
首先要創(chuàng)建一個 服務(wù)端類型 的 百度地圖應(yīng)用
設(shè)置 ip白名單 設(shè)置為0.0.0.0/0 即可
只有IP白名單內(nèi)的服務(wù)器才能成功發(fā)起調(diào)用
格式: 202.198.16.3,202.198.0.0/16 填寫IP地址或IP前綴網(wǎng)段,英文半角逗號分隔
如果不想對IP做任何限制,請設(shè)置為0.0.0.0/0 (謹慎使用,AK如果泄露配額會被其用戶
消費,上線前可以用作Debug,線上正式ak請設(shè)置合理的IP白名單)
請求 接口
http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=39.983424,116.322987&output=json&pois=1&ak=您的ak
三。移動選址
移動選址 通過setOnMapStatusChangeListener 方法 獲取 地圖拖動 動作 結(jié)束 時 地圖圖層中心 點 的坐標
效果如下

開發(fā)步驟(不再介紹地圖定位)
1.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/main_top_RL"
android:layout_width="match_parent"
android:layout_height="250dp">
<com.baidu.mapapi.map.MapView
android:id="@+id/main_bdmap"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="true"></com.baidu.mapapi.map.MapView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@android:color/transparent"
android:src="@mipmap/baidumap_ico_poi_on" />
</RelativeLayout>
<ListView
android:id="@+id/main_pois"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></ListView>
</LinearLayout>
2.移動選址
首先要設(shè)置
//地圖狀態(tài)改變相關(guān)監(jiān)聽
mBaiduMap.setOnMapStatusChangeListener(this);
/**
* 定位監(jiān)聽
*
* @param bdLocation
*/
@Override
public void onReceiveLocation(BDLocation bdLocation) {
//如果bdLocation為空或mapView銷毀后不再處理新數(shù)據(jù)接收的位置
if (bdLocation == null || mBaiduMap == null) {
return;
}
//定位數(shù)據(jù)
MyLocationData data = new MyLocationData.Builder()
//定位精度bdLocation.getRadius()
.accuracy(bdLocation.getRadius())
//此處設(shè)置開發(fā)者獲取到的方向信息,順時針0-360
.direction(bdLocation.getDirection())
//經(jīng)度
.latitude(bdLocation.getLatitude())
//緯度
.longitude(bdLocation.getLongitude())
//構(gòu)建
.build();
//設(shè)置定位數(shù)據(jù)
mBaiduMap.setMyLocationData(data);
//是否是第一次定位
if (isFirstLoc) {
isFirstLoc = false;
LatLng ll = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLngZoom(ll, 18);
mBaiduMap.animateMapStatus(msu);
}
//創(chuàng)建GeoCoder實例對象
geoCoder = GeoCoder.newInstance();
//發(fā)起反地理編碼請求(經(jīng)緯度->地址信息)
ReverseGeoCodeOption reverseGeoCodeOption = new ReverseGeoCodeOption();
//設(shè)置反地理編碼位置坐標
reverseGeoCodeOption.location(new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude()));
geoCoder.reverseGeoCode(reverseGeoCodeOption);
//設(shè)置查詢結(jié)果監(jiān)聽者
geoCoder.setOnGetGeoCodeResultListener(this);
}
//地理編碼查詢結(jié)果回調(diào)函數(shù)
@Override
public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
}
//反地理編碼查詢結(jié)果回調(diào)函數(shù)
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
List<PoiInfo> poiInfos = reverseGeoCodeResult.getPoiList();
PoiAdapter poiAdapter = new PoiAdapter(MainActivity.this, poiInfos);
poisLL.setAdapter(poiAdapter);
}
/**
* 手勢操作地圖,設(shè)置地圖狀態(tài)等操作導致地圖狀態(tài)開始改變
*
* @param mapStatus 地圖狀態(tài)改變開始時的地圖狀態(tài)
*/
@Override
public void onMapStatusChangeStart(MapStatus mapStatus) {
}
/**
* 地圖狀態(tài)變化中
*
* @param mapStatus 當前地圖狀態(tài)
*/
@Override
public void onMapStatusChange(MapStatus mapStatus) {
}
/**
* 地圖狀態(tài)改變結(jié)束
*
* @param mapStatus 地圖狀態(tài)改變結(jié)束后的地圖狀態(tài)
*/
@Override
public void onMapStatusChangeFinish(MapStatus mapStatus) {
//地圖操作的中心點
LatLng cenpt = mapStatus.target;
geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(cenpt));
}