最近项目中需要用到地图定位,并且实现提示框提示你的当前位置,然后我找了系统定位的小蓝点,好像没有和
InfoWindow使用的属性,然后我就使用了marker和InfoWindow的使用,并且实现实时定位!
首先看下效果
界面有点low,主要是先实现需求,然后我们可以改这个样式
接下来就是具体的实现代码,当然是在你集成了地图的sdk的前提下再来搞这个!
这个方法是实时获取最新的具体信息,然后我们就可以把marker放在这个方法中进行添加,需要注意的就是要及时的销毁marker,要不,每次刷新都要创建一个,就不好了。
if(marker != null){
//如果marker存在,我们就销毁他,然后在下面重新创建,
//这样就不会存在重复添加marker了
marker.destroy();
}
//添加Marker,他的属性还有很多,需要的可以上官网查看
marker = aMap.addMarker(new MarkerOptions().position(centerLatLng).period(2000).title(province).snippet(adress));
//显示标记
marker.showInfoWindow();
//创建
getInfoWindow(marker);
@Override
public View getInfoWindow(Marker marker) {
//创建我们需要的标记样式
View infoWindow = getLayoutInflater().inflate(
R.layout.hk_activity_mapshowfragment_infowindow, null);
//然后进行赋值
render(marker, infoWindow);
return infoWindow;
}
/**
* 自定义infowinfow窗口,将自定义的infoWindow和Marker关联起来
*/
public void render(Marker marker, View view) {
String title = marker.getTitle();
TextView titleUi = ((TextView) view.findViewById(R.id.title));
titleUi.setText(title);
String snippet = marker.getSnippet();
TextView snippetUi = ((TextView) view.findViewById(R.id.snippet));
snippetUi.setText(snippet);
}
这个就是我的样式,可根据自己的具体需求改变
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#ff0000"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="180dp"
android:layout_height="40dp"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="@+id/snippet"
android:layout_width="180dp"
android:layout_height="30dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:ellipsize="end"
android:singleLine="true"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
这里需要注意的就是需要给他们设置点击事件
aMap.setOnMarkerClickListener(this);// 设置点击marker事件监听器
aMap.setInfoWindowAdapter(this);// 设置自定义InfoWindow样式
还有就是这个地方this你可以强转一下,也可以
implements AMap.InfoWindowAdapter,AMap.OnMarkerClickListener
实现就行。
这样的话就可以实现正常使用了,
小白总结,希望可以解决大家的问题