使用高德地图自定义marker、infowindow

先上效果图

 

 

首先在oncreate的时候开启定位:

/**
     * 开启定位*/
    private void location() {
        //初始化定位
        AMapLocationClient mLocationClient;
        mLocationClient = new AMapLocationClient(getApplicationContext());
        // 设置定位回调监听
        mLocationClient.setLocationListener(this);
        // 初始化定位参数
        AMapLocationClientOption mLocationOption;
        mLocationOption = new AMapLocationClientOption();
        // 设置定位模式为Hight_Accuracy高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息(默认返回地址信息)
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(true);
        //设置是否强制刷新WIFI,默认为强制刷新
        mLocationOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false,不允许模拟位置
        mLocationOption.setMockEnable(false);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
    }

然后去重写AMapLocationListener接口的onLocationChanged方法:

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if(aMapLocation != null){
            if(aMapLocation.getErrorCode() == 0) {
                lai = aMapLocation.getLatitude();
                lon = aMapLocation.getLongitude();

                LatLng latLng2 = new LatLng(lai,lon);

                myInfoWindow = new MyInfoWindow(OtherPositionActivity.this);
                aMap.setInfoWindowAdapter(myInfoWindow);
                if(marker == null){
                    addMarker(latLng2,realname+"("+userId+")",imei,1);
                }else {
                    marker.destroy();
                    addMarker(latLng2,realname+"("+userId+")",imei,1);
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        iOtherPositionPrensenterCompl.searchAround(lai,lon,userId,realname,imei,50000);
                    }
                }).start();
            }else {
//                定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
                Log.e("地图错误","定位失败, 错误码:" + aMapLocation.getErrorCode() + ", 错误信息:" + aMapLocation.getErrorInfo());
            }
        }
    }

 

 使用自定义的MyInfoWindow:

public class MyInfoWindow implements AMap.InfoWindowAdapter {
    private Context context;
    private LatLng latLng;
    private TextView userText;
    private String userInfo;
    private TextView deviceText;
    private String deviceInfo;
    private View view;

    public MyInfoWindow(Context context) {
        this.context = context;
    }

    private void initData(Marker marker){
        latLng = marker.getPosition();
        deviceInfo = marker.getSnippet();
        userInfo = marker.getTitle();
    }

    private View initView(int type){
        if(type == 1) {
            view = LayoutInflater.from(context).inflate(R.layout.my_infowindow, null);
        }else if(type == 2){
            view = LayoutInflater.from(context).inflate(R.layout.my_infowindow_grey, null);
        }
        userText = view.findViewById(R.id.user_info);
        deviceText = view.findViewById(R.id.device_info);

        userText.setText(userInfo);
        deviceText.setText(deviceInfo);
        return view;
    }

    @Override
    public View getInfoWindow(Marker marker) {

        initData(marker);
        View view;
        if((int)marker.getObject() == 1){
             view = initView(1);
        }else if((int)marker.getObject() == 2){
            view = initView(2);
        }else {
            view = initView(1);
        }
        return view;
    }

    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }
}

这里主要重写getInfoWindow()方法,方法返回定义的infowindow,所以我们在设置marker的时候设定一个值,在这里取值出来做校验,marker.getObject(),根据此值选择对应的view。

 

接下来,自定义marker并且对marker设定值,以便根据此值设置不同的infowindow:

private void addMarker(LatLng latLng,String title,String snippet,int type){
        switch (type){
            case 1:
                Marker marker1;
                marker1 = aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
                        .position(latLng)
                        .title(title)
                        .snippet(snippet)
                        .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.blue_position)))
                );
                marker1.setObject(1);
                break;
            case 2:
                Marker marker2;
                marker2 = aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
                        .position(latLng)
                        .title(title)
                        .snippet(snippet)
                        .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.green_position)))
                );
                marker2.setObject(2);
                break;
            default:
                break;
        }

这里根据type值给map添加不同的marker,在MarkerOptions设定图标、标题、内容等,然后再给marker设置一个值marker2.setObject(),以便再初始化界面时根绝type选择不同的view。

marker在添加到地图的时候就有了点击事件,点击显示infowindow,如果想默认显示,可以调用marker.showInfoWindow()手动触发。

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值