关于百度地图定位和骑行路线规划API:
1、相关权限说明
2、定位
添加服务:
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
定位主要类:
LocationClient
BDLocationListener
使用——
初始化:
private void setInitLocationVariable(){
mLocationClient = new LocationClient(getApplicationContext());
mLocationResultListener = new LocationResultListenerImpl(mLocationClient);
mISendLocationResult = mLocationResultListener;
mLocationClient.registerLocationListener(mLocationResultListener);
LocationConfiguration.setInitLocationConfiguration(mLocationClient);
mLocationClient.start();
}
只有调用过start()才会开启定位功能。
其中的mISendLocationResult为个人需要写的接口,非百度地图提供。
其中的LocationResultListenerImpl实现BDLocationListener接口,获取定位的信息,具体如下:
public class LocationResultListenerImpl implements BDLocationListener,
ISendLocationResult {
private LocationClient mLocationClient = null;
public LocationResult locationResult = null;
public LocationResultListenerImpl(LocationClient mLocationClient){
this.mLocationClient = mLocationClient;
locationResult = new LocationResult();
}
@Override
public void onReceiveLocation(BDLocation bdLocation) {
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
sb.append(bdLocation.getTime());
sb.append("\n error code : ");
sb.append(bdLocation.getLocType());
sb.append("\n latitude : ");
sb.append(bdLocation.getLatitude());//获取到纬度
sb.append("\n lontitude : ");
sb.append(bdLocation.getLongitude());//获取到经度
sb.append("\n radius : ");
sb.append(bdLocation.getRadius());
sb.append("\n city :");
sb.append(bdLocation.getCity());
locationResult.direction = bdLocation.getDirection();
locationResult.directionDescription = bdLocation.getLocationDescribe();
locationResult.time = bdLocation.getTime();
locationResult.city = bdLocation.getCity();
locationResult.latitude = bdLocation.getLatitude();
locationResult.longitude = bdLocation.getLongitude();
locationResult.radius = bdLocation.getRadius();
locationResult.locType = bdLocation.getLocType();
Log.i("System.out.print",
"定位(onReceiveLocation)城市的值:currentCity ----------> "
+ bdLocation.getCity());
Log.i("System.out.print", "sb(1)" + sb.toString());
if (bdLocation.getLocType() == BDLocation.TypeGpsLocation){// GPS定位结果
sb.append("\n speed : ");
sb.append(bdLocation.getSpeed());// 单位:公里每小时
sb.append("\n satellite : ");
sb.append(bdLocation.getSatelliteNumber());
sb.append("\n height : ");
sb.append(bdLocation.getAltitude());// 单位:米
sb.append("\n direction : ");
sb.append(bdLocation.getDirection());// 单位:度
sb.append("\n addr : ");
sb.append(bdLocation.getAddrStr());
sb.append("\n describe : ");
sb.append("gps定位成功");
Log.i("System.out.print", "sb(2)" + sb.toString());
} else if (bdLocation.getLocType() == BDLocation.TypeNetWorkLocation){// 网络定位结果
sb.append("\n addr : ");
sb.append(bdLocation.getAddrStr());
//运营商信息
sb.append("\n operationers : ");
sb.append(bdLocation.getOperators());
sb.append("\n describe : ");
sb.append("网络定位成功");
Log.i("System.out.print", "sb(3)" + sb.toString());
} else if (bdLocation.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
sb.append("\n describe : ");
sb.append("离线定位成功,离线定位结果也是有效的");
Log.i("System.out.print", "sb(3)" + sb.toString());
} else if (bdLocation.getLocType() == BDLocation.TypeServerError) {
sb.append("\n describe : ");
sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到
[email protected],会有人追查原因");
Log.i("System.out.print", "sb(4)" + sb.toString());
} else if (bdLocation.getLocType() == BDLocation.TypeNetWorkException) {
sb.append("\ndescribe : ");
sb.append("网络不同导致定位失败,请检查网络是否通畅");
Log.i("System.out.print", "sb(5)" + sb.toString());
} else if (bdLocation.getLocType() == BDLocation.TypeCriteriaException) {
sb.append("\n describe : ");
sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
Log.i("System.out.print", "sb(6)" + sb.toString());
}
sb.append("\n locationdescribe : ");
sb.append(bdLocation.getLocationDescribe());// 位置语义化信息
List
list = bdLocation.getPoiList();// POI数据
StringBuffer stringBuffer = new StringBuffer();
if (list != null) {
stringBuffer.append("\n poilist size = : ");
stringBuffer.append(list.size());
for (Poi p : list) {
stringBuffer.append("\n poi= : ");
stringBuffer.append(p.getId() + " " + p.getName() + " " + p.getRank());
}
}
locationResult.locationPOIDescription = stringBuffer.toString();
Log.i("System.out.print", "sb(7)" + sb.toString());
Log.i("BaiduLocationApiDemo", stringBuffer.toString());
locationResult.isEndLocation = true;
String time = locationResult.time;
Double latitude = locationResult.latitude;
Double longitude = locationResult.longitude;
String city = locationResult.city;
float radius = locationResult.radius;
int locType = locationResult.locType;
String locationDescription = locationResult.locationPOIDescription;
}
@Override
public LocationResult returnLocationResult() {
if (locationResult == null){
return null;
}
return locationResult;
}
public MyLocationData getMapDataBehindLocation(){
if (locationResult != null){
MyLocationData myLocationData = new MyLocationData.Builder()
.accuracy(locationResult.radius)
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(locationResult.direction).latitude(locationResult.latitude)
.longitude(locationResult.longitude).build();
// mBaiduMap.setMyLocationData(locData);
locationResult.toString();
return myLocationData;
}
return null;
}
public class LocationResult{
public String time;
public Double latitude;
public Double longitude;
public String city;
public float radius;
public int locType;
public String locationPOIDescription;
/**
* 方向
*/
public float direction;
public String directionDescription;
public Boolean isEndLocation;
@Override
public String toString() {
return "LocationResult{" +
"time='" + time + '\'' +
", latitude=" + latitude +
", longitude=" + longitude +
", city='" + city + '\'' +
", radius=" + radius +
", locType=" + locType +
", locationPOIDescription='" + locationPOIDescription + '\'' +
", direction=" + direction +
", directionDescription='" + directionDescription + '\'' +
", isEndLocation=" + isEndLocation +
'}';
}
}
}
在使用定位的同时可能需要设置地图到当前位置:
// 开启定位图层
mBaidumap.setMyLocationEnabled(true);// 关闭时修改为false
//设置相关参数,LocationMode三个值分别表示罗盘,跟随和普通
private void setInitMyLocationConfiguration(){
LocationMode mLocationMode = LocationMode.COMPASS;// LocationMode.NORMAL //LocationMode.FOLLOWING
BitmapDescriptor mCurrentMarker;
mBaidumap
.setMyLocationConfigeration(new MyLocationConfiguration(
mLocationMode, true, null));// 默认图标
}
// 设置数据
MyLocationData locationData = mLocationResultListener.getMapDataBehindLocation();
mBaidumap.setMyLocationData(locationData);
根据某城市某地址获知经纬度信息
GeoCoder mGeoCoder = null;// 搜索,地理编码反编码
……
private LatLng latLngGeoCoder = null;
private void setSearchGeoCoder(){
// 初始化搜索模块,注册事件监听
mGeoCoder = GeoCoder.newInstance();
mGeoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
@Override
public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {// 根据城市和地址的回调,可获取到经纬度
if (geoCodeResult == null || geoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
// Toast.makeText(RoutePlanDemo.this, "抱歉,未能找到结果", Toast.LENGTH_LONG)
// .show();
return;
}
// mBaidumap.clear();
mBaidumap.addOverlay(new MarkerOptions().position(geoCodeResult.getLocation())
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.icon_marka)));
mBaidumap.setMapStatus(MapStatusUpdateFactory.newLatLng(geoCodeResult
.getLocation()));
latLngGeoCoder = geoCodeResult.getLocation();
// 测试:计算两点之间的距离
Double mDistance = DistanceUtil.getDistance(mLocationLatLng, latLngGeoCoder);
Log.i("TANG-YD", "_mDistance >>> " + mDistance);
String strInfo = String.format("纬度:%f 经度:%f",
geoCodeResult.getLocation().latitude, geoCodeResult.getLocation().longitude);
// Toast.makeText(RoutePlanDemo.this, strInfo, Toast.LENGTH_LONG).show();
}
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {// 根据经纬度搜索的回调
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(RoutePlanDemo.this, "抱歉,未能找到结果", Toast.LENGTH_LONG)
.show();
return;
}
// mBaidumap.clear();
mBaidumap.addOverlay(new MarkerOptions().position(result.getLocation())
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.icon_marka)));
mBaidumap.setMapStatus(MapStatusUpdateFactory.newLatLng(result
.getLocation()));
Toast.makeText(RoutePlanDemo.this, result.getAddress(),
Toast.LENGTH_LONG).show();
}
});
}
// 发起搜索某个城市某个地址的搜索
mGeoCoder.geocode(new GeoCodeOption().city(
mLocationResult.city).address("同方信息港"));// 发起搜索某个城市某个地址的搜索,信息在上面的回调函数中获取。
___________________________________________________________________________________________________________________________
下面是一个关于ListView局部更新的方法,需要注意是否有HeaderView和FooterView
private void updateSingleRow(ListView listView, long id) {
if (listView != null) {
int start = listView.getFirstVisiblePosition();
for (int i = start, j = listView.getLastVisiblePosition(); i <= j; i++)
if (id == ((Messages) listView.getItemAtPosition(i)).getId()) {
View view = listView.getChildAt(i - start);
getView(i, view, listView);
break;
}
}
}