本次主要实现了点击位置消息显示位置地图及信息的showLocActivity和相应的布局文件。
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStatusBarColor(R.color.app_blue_color);
setContentView(R.layout.activity_show_loc);
ButterKnife.bind(this);
setTitleBar("位置信息", true, false);
initMap(savedInstanceState);
showMsgLocation();
}
1.initMap和上次写的selectLocActivity中的相同,下面为showMsgLocation;
private void showMsgLocation(){
mIMMessage = (IMMessage) getIntent().getSerializableExtra("IMMessage");
LocationAttachment attachment = (LocationAttachment) mIMMessage.getAttachment();
if (attachment == null){
ToastUtils.showMessage(this,"附件获取失败,请重试~");
finish();
return;
}
double latitude = attachment.getLatitude();
double longitude = attachment.getLongitude();
if (latitude < 0.0 || longitude < 0.0){
ToastUtils.showMessage(this,"地理坐标失效,无法显示!");
}else {
LatLng latLng = new LatLng(latitude,longitude);
// 显示标记
MarkerOptions options = new MarkerOptions();
BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(R.mipmap.ic_map_center);
options.icon(descriptor);
options.position(latLng);
mAMap.addMarker(options);
// 移动地图视角
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, mZoomLevel);
mAMap.animateCamera(cameraUpdate);
}
String address = attachment.getAddress();
if (!TextUtils.isEmpty(address)){
mTvAddress.setText(address);
}else {
mTvAddress.setText("地址描述获取失败……");
}
}
2.返回当前位置:
@OnClick(R.id.iv_my_location)
public void location(){
mLocationClient.startLocation();
}
@Override
public void onLocationChanged(AMapLocation location) {
if (location != null) {
if (location.getErrorCode() == 0) {
LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());
MarkerOptions options = new MarkerOptions();
BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(R.mipmap.ic_my_loc);
options.icon(descriptor);
options.position(latLng);
mAMap.addMarker(options);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, mZoomLevel);
mAMap.animateCamera(cameraUpdate);
}
}
}
3.过程中用到的LocationPoint
/**
* 分享位置,单个位置点信息
* Created by
*/
public class LocationPoint {
private String mId;
private String mName;
private String mAddress;
private boolean mSelected;
private LatLonPoint mPoint;
public String getId() {
return mId;
}
public void setId(String id) {
mId = id;
}
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public String getAddress() {
return mAddress;
}
public void setAddress(String address) {
mAddress = address;
}
public boolean isSelected() {
return mSelected;
}
public void setSelected(boolean selected) {
mSelected = selected;
}
public LatLonPoint getPoint() {
return mPoint;
}
public void setPoint(LatLonPoint point) {
mPoint = point;
}
}
最终效果: