700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 使用高德地图自定义marker infowindow

使用高德地图自定义marker infowindow

时间:2019-07-30 19:42:36

相关推荐

使用高德地图自定义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);//设置是否只定位一次,默认为falsemLocationOption.setOnceLocation(true);//设置是否强制刷新WIFI,默认为强制刷新mLocationOption.setWifiActiveScan(true);//设置是否允许模拟位置,默认为false,不允许模拟位置mLocationOption.setMockEnable(false);//设置定位间隔,单位毫秒,默认为2000msmLocationOption.setInterval(2000);//给定位客户端对象设置定位参数mLocationClient.setLocationOption(mLocationOption);//启动定位mLocationClient.startLocation();}

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

@Overridepublic 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() {@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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()手动触发。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。