Android 是否可以调用onmaclick()而不是onMarkerClick()?
我有一个谷歌地图在我的应用程序,我处理点击它。一切都很好,但如果我在marker上单击一个标记或非常接近它,则调用marker click,而不是onMapClick,然后我没有单击位置的确切位置,因为marker.getPosition返回标记的中心点,这是不一样的Android 是否可以调用onmaclick()而不是onMarkerClick()?,android,google-maps,Android,Google Maps,我有一个谷歌地图在我的应用程序,我处理点击它。一切都很好,但如果我在marker上单击一个标记或非常接近它,则调用marker click,而不是onMapClick,然后我没有单击位置的确切位置,因为marker.getPosition返回标记的中心点,这是不一样的 是否可以禁用onMarkerClick并调用onmaclick,即使我单击了一个标记?对于禁用标记单击事件,返回true googleMap.setOnMarkerClickListener(new OnMarkerClickLi
是否可以禁用onMarkerClick并调用onmaclick,即使我单击了一个标记?对于禁用标记单击事件,返回true
googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
return true;
}
});
要获得点击地图,请在onMapReady上使用此侦听器
完整代码
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
Toast.makeText(MapsActivity.this, "Map clicked", Toast.LENGTH_SHORT).show();
}
});
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(MapsActivity.this, "Marker disabled", Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
为禁用标记单击事件返回true
googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
return true;
}
});
要获得点击地图,请在onMapReady上使用此侦听器
完整代码
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
Toast.makeText(MapsActivity.this, "Map clicked", Toast.LENGTH_SHORT).show();
}
});
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(MapsActivity.this, "Marker disabled", Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
这是可能的。这里有一个简单的解决方案 首先,您应该在活动或片段中声明GoogleMap.OnMarkerClickListener和GoogleMap.OnMapClickListener,如下所示
public class MapActivity extends FragmentActivity implements
GoogleMap.OnMarkerClickListener,
GoogleMap.OnMapClickListener
{
//..
}
然后在onCreate方法中注册该接口
@Override
protected void onCreate(Bundle savedInstanceState)
{
//..
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mMap.setOnMarkerClickListener(this);
mMap.setOnMapClickListener(this);
}
最后,您可以实现两个接口。您应该在onMarkerClick方法中调用onmaclick方法来实现这一点,即使您单击了这样的标记。不要忘记在onMarkerClick方法中返回true
@Override
public void onMapClick(LatLng latLng) {
Log.v("onMapClick", "Map Clicked..")
}
@Override
public boolean onMarkerClick(Marker marker) {
onMapClick(marker.getPosition());
return true;
}
这是可能的。这里有一个简单的解决方案 首先,您应该在活动或片段中声明GoogleMap.OnMarkerClickListener和GoogleMap.OnMapClickListener,如下所示
public class MapActivity extends FragmentActivity implements
GoogleMap.OnMarkerClickListener,
GoogleMap.OnMapClickListener
{
//..
}
然后在onCreate方法中注册该接口
@Override
protected void onCreate(Bundle savedInstanceState)
{
//..
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mMap.setOnMarkerClickListener(this);
mMap.setOnMapClickListener(this);
}
最后,您可以实现两个接口。您应该在onMarkerClick方法中调用onmaclick方法来实现这一点,即使您单击了这样的标记。不要忘记在onMarkerClick方法中返回true
@Override
public void onMapClick(LatLng latLng) {
Log.v("onMapClick", "Map Clicked..")
}
@Override
public boolean onMarkerClick(Marker marker) {
onMapClick(marker.getPosition());
return true;
}
发布您的代码?如果我理解正确,您仍然希望标记可以单击,但仅当您在其中心单击它时,而不是当您单击它真正靠近它时。我说的对吗?不,我希望地图即使在标记显示的地方也可以剪贴。现在标记会使用该事件。您不想再对标记单击始终做出反应了吗?您是否尝试过不声明MarkerClickListener?发布您的代码?如果我理解正确,您仍然希望标记可以单击,但仅当您单击它的中心时,而不是当您单击它真正靠近它时。我说的对吗?不,我希望地图即使在标记显示的地方也可以剪贴。现在标记消耗了该事件。你不想再对标记单击总是做出反应了吗?你尝试过没有声明MarkerClickListener吗?谢谢,但这正是我所做的,标记显示的位置不可剪裁,但我希望调用onMapClick。这是一个开源项目,类现在在这里没有设置MarkerClickListener,以便以默认方式处理单击,并在底部显示菜单。如果我添加您的解决方案,则不会发生任何事情,因为它已被使用,但仍然没有调用onmaclick。所以可能根本不可能?我这样做了,然后标记确实不可点击,但如果我点击它,仍然不会调用onMapClick。你能尝试在map click listener中添加标记单击侦听器吗谢谢,但这正是我所做的,标记显示的位置不可点击,但我希望调用onMapClick。这是一个开源项目类现在在这里,这里没有setOnMarkerClickListener,所以点击是以默认方式处理的,菜单显示在底部。如果我添加您的解决方案,则不会发生任何事情,因为它已被使用,但仍然没有调用onmaclick。所以可能根本不可能?我这样做了,然后标记确实不可单击,但如果我单击它,仍然不会调用onMapClick。您可以尝试在map click listener中添加该标记单击侦听器,但如果调用onMapClickmarker.getPosition;使用标记的中心点并不完美,因为即使在标记附近单击,也会调用onMarkerClick,而不完全是中心点;使用标记的中心点并不完美,因为即使在标记附近单击,也会调用onMarkerClick,而不完全是中心点。