实训第三周(2)

本次主要实现了点击位置消息显示位置地图及信息的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;
    }
}

最终效果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值