谷歌地图 API v2: 如何使标记可点击?

如何让 Android Google Maps API v2中的标记变得可点击,这样它们要么会显示一个带有选项的菜单,要么只是启动一个新的活动?我相信我在我的应用程序的标记目前在一个“新”的方法。我没有给他们分配一个名称或者一个方法,以便能够将它与所需的其余代码链接起来。

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

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

185914 次浏览

Google Android Maps Api v2中的所有标记都是可点击的。不需要为标记设置任何其他属性。 你需要做的是注册标记点击回调到你的 googleMap 并处理回调中的点击:

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
}
}
}

这是一个良好的 关于标记定制的谷歌指南

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

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();
}
}

另一种解决方案: 按照标题获取标记

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
}
}
}

在向地图中添加标记时使用 setTag(position)

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

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;
}
});

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

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));
}
}

避免使用活动实现 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);

当使用数据模型创建新标记时,将这两者都添加到制造者映射

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);
}
});
Step 1
public class TopAttractions extends Fragment implements OnMapReadyCallback,
GoogleMap.OnMarkerClickListener


Step 2
gMap.setOnMarkerClickListener(this);


Step 3
@Override
public boolean onMarkerClick(Marker marker) {
if(marker.getTitle().equals("sharm el-shek"))
Toast.makeText(getActivity().getApplicationContext(), "Hamdy", Toast.LENGTH_SHORT).show();
return false;
}

我在 onMapReady(GoogleMap googleMap)方法中添加了一个 mMap.setOnMarkerClickListener(this);。因此,每当您单击一个标记时,它都会在祝酒方法中显示文本名称。

public class DemoMapActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener,OnMapReadyCallback, GoogleMap.OnMarkerClickListener {


private GoogleMap mMap;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_places);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}


@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
double lat=0.34924212701428;
double lng=32.616554024713;
String venue = "Capital Shoppers City";
LatLng location = new LatLng(lat, lng);
mMap.addMarker(new MarkerOptions().position(location).title(venue)).setTag(0);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLng(location);
CameraUpdate zoom = CameraUpdateFactory.zoomTo(16);
mMap.moveCamera(cameraUpdate);
mMap.animateCamera(zoom);
mMap.setOnMarkerClickListener(this);
}


@Override
public boolean onMarkerClick(final Marker marker) {
// Retrieve the data from the marker.
Integer clickCount = (Integer) marker.getTag();


// Check if a click count was set, then display the click count.
if (clickCount != null) {
clickCount = clickCount + 1;
marker.setTag(clickCount);
Toast.makeText(this,
marker.getTitle() +
" has been clicked ",
Toast.LENGTH_SHORT).show();
}
// Return false to indicate that we have not consumed the event and that we wish
// for the default behavior to occur (which is for the camera to move such that the
// marker is centered and for the marker's info window to open, if it has one).
return false;
}

}

你可参考此连结 记号笔

下面的科特林代码可以帮助你

创建标记

        for (i in arrayList.indices) {
val marker = googleMap!!.addMarker(
MarkerOptions().position(
LatLng(
arrayList[i].location_latitude!!.toDoubleOrNull()!!,
arrayList[i].location_latitude!!.toDoubleOrNull()!!
)
).title(arrayList[i].business_name)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_marker))
)
marker.tag = i
}

点击标记

        googleMap!!.setOnMarkerClickListener { marker ->
Log.d(TAG, "Clicked on  ${marker.tag}")
true
}

关闭标记点击

关闭地图上标记的可点击功能我的回答。

我在搜索“ 机器人地图标记关闭点击”的时候发现了这个问题

map.setOnMarkerClickListener { true }