- 引入天地图 SDK
- 地图显示
- 我的位置
- 定位图标的修改
- 获取位置信息
- 显示效果
引入天地图 SDK
sourceSets {
main {
//指定so文件的查找目录是libs目录
jniLibs.srcDir 'libs'
}
}
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
地图显示
<com.tianditu.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
private void initMapView() {
mapView.setBuiltInZoomControls(true);
mMapController = mapView.getController();
GeoPoint point = new GeoPoint((int) (39.915 * 1E6),(int) (116.404 * 1E6));
mMapController.setCenter(point);
mMapController.setZoom(12);
}
我的位置
myLocationOverlay = new MyLocationOverlay(this,mapView);
myLocationOverlay.enableCompass();
myLocationOverlay.enableMyLocation();
mapView.addOverlay(myLocationOverlay);
mPoint = myLocationOverlay.getMyLocation();
mMapController.animateTo(mPoint);
定位图标修改
- 在项目路径中找到默认定位图标;
- 在关键类中找到设置该默认图标的位置;
- 继承关键类,重写相关代码,替换原有的定位图标为新的定位图标;
- 使用新的类替换 MyLocationOverlay 类。
@Override
protected void drawMyLocation(GL10 gl,MapView mapView,Location lastFix,GeoPoint myLocation,long when) {
Point point = new Point();
mapView.getProjection().toPixels(myLocation,point);
float accuracy = getAccuracy();
float accuracy = 500;
float distance = mapView.getProjection().metersToEquatorPixels(accuracy);
AndroidJni.OpenglFillRound(point.x,point.y,(int)distance,0,360,137,170,213,77);
UtilTextureDrawable drawable = new UtilTextureDrawable(mContext,R.drawable.ic_location,BOUND_TYPE_CENTER);
drawable.DrawTexture(gl,point,0.0F);
}
获取位置信息
/**
* 逆地理编码回调结果监听
*/
class OnGeoResultListener implements TGeoDecode.OnGeoResultListener {
@Override
public void onGeoDecodeResult(TGeoAddress tGeoAddress,int errorCode) {
if (TErrorCode.OK == errorCode) {
String str = "最近的 poi 名称:" + tGeoAddress.getPoiName() + "\n";
str += "查询点 Poi 点的方位:" + tGeoAddress.getPoiDirection() + "\n";
str += "查询点 Poi 点的距离:" + tGeoAddress.getPoiDistance() + "\n";
str += "查询点行政区名称:" + tGeoAddress.getCity() + "\n";
str += "查询点地理描述全称:" + tGeoAddress.getFullName() + "\n";
str += "查询点的地址:" + tGeoAddress.getAddress() + "\n";
str += "查询点的方位:" + tGeoAddress.getAddrDirection() + "\n";
str += "查询点的距离:" + tGeoAddress.getAddrDistance() + "\n";
str += "查询点道路名称:" + tGeoAddress.getRoadName() + "\n";
str += "查询点与最近道路的距离:" + tGeoAddress.getRoadDistance();
tvAddress.setText(tGeoAddress.getFullName());
System.out.println(str);
} else {
System.out.println("查询出错:" + errorCode);
}
}
}
TGeoDecode tGeoDecode = new TGeoDecode(new OnGeoResultListener());
tGeoDecode.search(mPoint);
显示效果