的标记Marker,上一章已经用到了Marker这个类了。创建一个系统默认的Marker很简单
private GoogleMap mMap;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("Hello world"));
也可以创建一个自定义的Marker
private static final LatLng MELBOURNE = new LatLng(-37.81319, 144.96298);
private Marker melbourne = mMap.addMarker(new MarkerOptions()
.position(MELBOURNE)
.title("Melbourne")
.snippet("Population: 4,137,400")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)));
自定义Marker时使用了BitmapDescriptorFactory创建一个自定义的Marker图标,在使用BitmapDescriptorFactory之前先看看先看看它的文档,使用这个类的任何方法之前,你必须做下列操作之一,以确保这个类被初始化:1:从MapFragment或者MapView中添加一个GoogleMap,你可以通过调用GoogleMap.getMap()方法,判断返回是否为空。2:调用 MapsInitializer.initialize(Context)方法,只要不抛出com.google.android.gms.common.GooglePlayServicesNotAvailableException GooglePlayServicesNotAvailableException这个异常就可以正常起始化。
还有Marker的点击事件,GoogleMap.setOnMarkerClickListener(); Marker的拖动事件,GoogleMap.setOnMarkerDragListener();(Marker默认是不能拖动的,创建Marker时设置melbourne.setDraggable(true)为可拖动)
点击 Marker时会默认弹出信息窗口Info Windows,你可以使用InfoWindowAdapter接口,调用GoogleMap.setInfoWindowAdapter()去自定义Info Windows,InfoWindowAdapter要实现两个方法 getInfoWindow(Marker)和getInfoContents(Marker)。api会先调用getInfoWindow(Marker)方法,如果返回null,再继续调用getInfoContents(Marker)方法,如果继续返回null,api将调用默认的info windows。info windows跟marker一样具有点击事件GoogleMap.setOnInfoWindowClickListener()。