是否可以调用onMapClick()而不是onMarkerClick()?

时间:2019-04-04 07:34:01

标签: android google-maps

我的应用程序中有一个google map,我可以处理它的点击。一切都很好,但是如果我单击标记或实际上靠近标记,就会调用onMarkerClick(),而不是onMapClick(),然后我就无法知道我所点击的地点的确切位置,因为marker.getPosition()返回标记的中心点。

即使单击标记,是否可以禁用onMarkerClick()并调用onMapClick()

2 个答案:

答案 0 :(得分:0)

返回true以禁用标记点击事件

googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
        return true;
    }
});

要在地图上点击,请在onMapReady上使用此侦听器

@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener()
{
    @Override
    public void onMapClick(LatLng arg0)
    {
        Log.v("onMapClick", "Yea worked!");
    }
});
}

完整代码

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

答案 1 :(得分:-1)

有可能。这是一个简单的解决方案。

首先,您应在活动或片段中声明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()方法中调用onMapClick()方法来实现这一点。不要忘记在onMarkerClick()方法中返回true。 >

@Override
public void onMapClick(LatLng latLng) {
   Log.v("onMapClick", "Map Clicked..")
}

@Override
public boolean onMarkerClick(Marker marker) {
    onMapClick(marker.getPosition());
    return true;
}