最简单的高德地图2D地图的使用,仅包含了添加Marker,拖动Marker后转换经纬度到详细地址并显示。
混淆时注意加上:
-keep class com.amap.api.maps2d.**{*;}
-keep class com.amap.api.mapcore2d.**{*;}
-dontwarn com.amap.api.**
-dontwarn com.a.a.**
-dontwarn com.autonavi.**
public class SetAddressActivity extends BaseEasyActivity implements AMap.OnMarkerClickListener, AMap.OnMarkerDragListener, GeocodeSearch.OnGeocodeSearchListener {
@Bind(R.id.mapView)
MapView mapView;
private AMap aMap;
private LatLng mLatLng;
private Marker mMarker;
private GeocodeSearch geoCoderSearch;
private String mAddressName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setBackTitle("设置地址");
//保存地址更新的方法放在这里
saveLocation();
//获取地址的经纬度信息
double lon = mIntent.getDoubleExtra(Constants.LON, 0);
double lat = mIntent.getDoubleExtra(Constants.LAT, 0);
mLatLng = new LatLng(lat, lon);
mapView.onCreate(savedInstanceState);
//初始化高德地图
init();
}
private void saveLocation() {
toolbarRight.setText("保存");
RxView.clicks(toolbarRight)
.throttleFirst(500, TimeUnit.MILLISECONDS)
.filter(new Func1<Void, Boolean>() {
@Override
public Boolean call(Void aVoid) {
return mMarker != null && !TextUtils.isEmpty(mAddressName);
}
})
.subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
//保存
HashMap<String, String> option = new HashMap<>();
LatLng latLng = mMarker.getPosition();
KLog.e(latLng.toString());
option.put(Constants.LON, Double.toString(latLng.longitude));
option.put(Constants.LAT, Double.toString(latLng.latitude));
option.put(Constants.LOCATION_ADDRESS, mAddressName);
RetrofitHelper.getInstance().saveShop(new RxSubscriber<String>(mContext) {
@Override
public void onNext(String s) {
showToastMsg(s);
Observable.timer(1, TimeUnit.SECONDS)
.subscribe(new Action1<Long>() {
@Override
public void call(Long aLong) {
finishActivity();
}
});
}
}, option);
}
});
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onDestroy() {
mapView.onDestroy();
super.onDestroy();
}
@Override
protected int getLayoutId() {
return R.layout.shopinfo_setaddress;
}
private void init() {
if (aMap == null) {
aMap = mapView.getMap();
setUpMarker();//添加marker
setUpSearch();//添加坐标转地址功能
}
}
private void setUpSearch() {
geoCoderSearch = new GeocodeSearch(this);
geoCoderSearch.setOnGeocodeSearchListener(this);
}
private void setUpMarker() {
aMap.setOnMarkerClickListener(this);//marker点击监听
//marker拖动监听,在MarkerOptions中设置了draggable(true)以后,没有设置这个似乎无法拖动
aMap.setOnMarkerDragListener(this);
addMarkersToMap();// 往地图上添加marker
}
/**
* 在地图上添加marker
*/
private void addMarkersToMap() {
MarkerOptions markerOption = new MarkerOptions().icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))//Marker颜色
.position(mLatLng)//Marker坐标
.draggable(true);//可拖动
aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mLatLng, 15));//移动屏幕中间
mMarker = aMap.addMarker(markerOption);
}
@Override
protected void initViewAndEvent() {
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
public boolean onMarkerClick(Marker marker) {
//点击Marker弹出转换后的详细地址
//坐标地址转换
reGeoSearch(marker.getPosition());
return true;
}
/**
* 坐标转换地址
*
* @param latLng
*/
private void reGeoSearch(LatLng latLng) {
if (latLng == null) {
return;
}
//根据LatLng创建一个Point
LatLonPoint latLonPoint = new LatLonPoint(latLng.latitude, latLng.longitude);
if (geoCoderSearch != null) {
// 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系
RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200, GeocodeSearch.AMAP);
geoCoderSearch.getFromLocationAsyn(query);
}
}
@Override
public void onMarkerDragStart(Marker marker) {
}
@Override
public void onMarkerDrag(Marker marker) {
}
@Override
public void onMarkerDragEnd(Marker marker) {
//Marker拖动结束后弹出信息
// mMarker = marker;
reGeoSearch(marker.getPosition());
}
@Override
public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
//地址坐标转换回调
if (rCode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getRegeocodeAddress() != null
&& result.getRegeocodeAddress().getFormatAddress() != null) {
mAddressName = result.getRegeocodeAddress().getFormatAddress();
//获取详细地址并展示
showToastMsg(mAddressName);
} else {
showToastMsg("对不起,没有搜索到相关数据!");
}
} else {
showToastMsg("定位失败,请重新选择");
}
}
@Override
public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
}
}
网友评论