Google Maps API v2:如何让标记点击?

如何让Google Maps API v2中的标记变得可点击,以便他们调出带有选项的菜单或者只是启动一项新的活动? 我相信我在我的应用程序当前在“newb”方法中做了标记。 我没有给他们分配一个名字或方法,以便能够将其与所需的其他代码链接起来。

googleMap.addMarker(new MarkerOptions() .position(latLng) .title("My Spot") .snippet("This is my spot!") .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))); 

如果您回答这个问题,请包含一个标记的示例代码,该代码被引入一个唯一的名称,然后设置为可点击打开一个新的活动。

Google Android Maps Api v2中的所有标记都可以点击。 您不需要为标记设置任何附加属性。 你需要做的是注册标记点击callback您的谷歌地图和处理单击callback:

 public class MarkerDemoActivity extends android.support.v4.app.FragmentActivity implements OnMarkerClickListener { private Marker myMarker; private void setUpMap() { ....... googleMap.setOnMarkerClickListener(this); myMarker = googleMap.addMarker(new MarkerOptions() .position(latLng) .title("My Spot") .snippet("This is my spot!") .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))); ...... } @Override public boolean onMarkerClick(final Marker marker) { if (marker.equals(myMarker)) { //handle click here } } } 

setTag(position)同时添加标记映射。

 Marker marker = map.addMarker(new MarkerOptions() .position(new LatLng(latitude, longitude))); marker.setTag(position); 

setOnMarkerClickListener监听器上的setOnMarkerClickListener getTag()

 map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { int position = (int)(marker.getTag()); //Using position get Value from arraylist return false; } }); 

另一个解决scheme:你通过标题得到标记

 public class MarkerDemoActivity extends android.support.v4.app.FragmentActivity implements OnMarkerClickListener { private Marker myMarker; private void setUpMap() { ....... googleMap.setOnMarkerClickListener(this); myMarker = googleMap.addMarker(new MarkerOptions() .position(latLng) .title("My Spot") .snippet("This is my spot!") .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))); ...... } @Override public boolean onMarkerClick(final Marker marker) { String name= marker.getTitle(); if (name.equalsIgnoreCase("My Spot")) { //write your code here } } 

}

这是我用4个可点击标记的地图活动的整个代码。 点击一个标记显示一个信息窗口,点击信息窗口后,您将进入另一个活动:英语,德语,西class牙语或意大利语。 如果你想使用OnMarkerClickListener,尽pipeOnInfoWindowClickListener,你只需要交换这一行:

 mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() 

对此:

 mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() 

这一行:

 public void onInfoWindowClick(Marker arg0) 

对此:

 public boolean onMarkerClick(Marker arg0) 

并在方法“onMarkerClick”结束时:

 return true; 

我认为这可能对某人有帮助;)

 package pl.pollub.translator; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.widget.Toast; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); Toast.makeText(this, "Choose a language.", Toast.LENGTH_LONG).show(); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() { @Override public void onInfoWindowClick(Marker arg0) { if(arg0 != null && arg0.getTitle().equals("English")){ Intent intent1 = new Intent(MapsActivity.this, English.class); startActivity(intent1);} if(arg0 != null && arg0.getTitle().equals("German")){ Intent intent2 = new Intent(MapsActivity.this, German.class); startActivity(intent2);} if(arg0 != null && arg0.getTitle().equals("Italian")){ Intent intent3 = new Intent(MapsActivity.this, Italian.class); startActivity(intent3);} if(arg0 != null && arg0.getTitle().equals("Spanish")){ Intent intent4 = new Intent(MapsActivity.this, Spanish.class); startActivity(intent4);} } }); LatLng greatBritain = new LatLng(51.30, -0.07); LatLng germany = new LatLng(52.3107, 13.2430); LatLng italy = new LatLng(41.53, 12.29); LatLng spain = new LatLng(40.25, -3.41); mMap.addMarker(new MarkerOptions() .position(greatBritain) .title("English") .snippet("Click on me:)")); mMap.addMarker(new MarkerOptions() .position(germany) .title("German") .snippet("Click on me:)")); mMap.addMarker(new MarkerOptions() .position(italy) .title("Italian") .snippet("Click on me:)")); mMap.addMarker(new MarkerOptions() .position(spain) .title("Spanish") .snippet("Click on me:)")); mMap.moveCamera(CameraUpdateFactory.newLatLng(greatBritain)); mMap.moveCamera(CameraUpdateFactory.newLatLng(germany)); mMap.moveCamera(CameraUpdateFactory.newLatLng(italy)); mMap.moveCamera(CameraUpdateFactory.newLatLng(spain)); } } 

避免使用Activity实现OnMarkerClickListener,使用本地的OnMarkerClickListener

 // Not a good idea class MapActivity extends Activity implements OnMarkerClickListener { } 

您将需要一张地图来查找链接到标记的原始数据模型

 private Map<Marker, Map<String, Object>> markers = new HashMap<>(); 

你将需要一个数据模型

 private Map<String, Object> dataModel = new HashMap<>(); 

把一些数据放在数据模型中

 dataModel.put("title", "My Spot"); dataModel.put("snipet", "This is my spot!"); dataModel.put("latitude", 20.0f); dataModel.put("longitude", 100.0f); 

使用数据模型创build新标记时,将两者都添加到制图器映射中

 Marker marker = googleMap.addMarker(markerOptions); markers.put(marker, dataModel); 

对于单击标记事件,请使用本地OnMarkerClickListener:

 @Override public void onMapReady(GoogleMap googleMap) { // grab for laters this.googleMap = googleMap; googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { Map dataModel = (Map)markers.get(marker); String title = (String)dataModel.get("title"); markerOnClick(title); return false; } }); mapView.onResume(); showMarkers(); ZoomAsync zoomAsync = new ZoomAsync(); zoomAsync.execute(); } 

用于显示信息窗口从标记图检索原始数据模型:

 @Override public void onMapReady(GoogleMap googleMap) { this.googleMap = googleMap; googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() { @Override public void onInfoWindowClick(Marker marker) { Map dataModel = (Map)markers.get(marker); String title = (String)dataModel.get("title"); infoWindowOnClick(title); } }); 

我已经编辑了上面给出的例子…

 public class YourActivity extends implements OnMarkerClickListener { ...... private void setMarker() { ....... googleMap.setOnMarkerClickListener(this); myMarker = googleMap.addMarker(new MarkerOptions() .position(latLng) .title("My Spot") .snippet("This is my spot!") .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))); ...... } @Override public boolean onMarkerClick(Marker marker) { Toast.makeText(this,marker.getTitle(),Toast.LENGTH_LONG).show(); } }