【问题标题】:Catch a click on a specific marker in Google Maps在 Google 地图中点击特定标记
【发布时间】:2018-06-21 03:54:53
【问题描述】:

告诉我,我怎样才能正确捕捉到对 Google 地图地图上某个标记的点击然后设置操作?假设有三个变量标记一;二;三;我这样做了,但仅适用于第三四次点击标记:

    public void onMapReady(GoogleMap googleMap) {
...
One = mMap.addMarker(new MarkerOptions()
        .position(lat));
One.setTag(0);
// Set a listener for marker click.
mMap.setOnMarkerClickListener(this);

}

public boolean onMarkerClick(final Marker marker) {

ExampleBottomSheetDialog bottomSheet = new ExampleBottomSheetDialog();
bottomSheet.show(getSupportFragmentManager(), "exampleBottomSheet");

// Check if a click count was set, then display the click count.
if (marker.equals(One)) {
    Toast.makeText(getApplicationContext(), "Balalaika: ",
            Toast.LENGTH_SHORT).show();
}

return false;

}

【问题讨论】:

标签: java


【解决方案1】:

你可以implements方法GoogleMap.OnMarkerClickListener

@Override
public boolean onMarkerClick(Marker marker) {
    Log.e(TAG, "onMarkerClick: " + marker.getTitle() + " " + marker.getSnippet());


    return true;
}

【讨论】:

    【解决方案2】:

    我自定义我的 InfoWindows 并将我的标记点击事件写入

     mMap.setOnInfoWindowClickListener
    

    这段代码对我来说很好,也许它可以帮助你。

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    
        //Initialize Google Play Services
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                buildGoogleApiClient();
                mMap.setMyLocationEnabled(false);
                mMap.getUiSettings().setMyLocationButtonEnabled(false);
            }
        } else {
            buildGoogleApiClient();
            mMap.setMyLocationEnabled(false);
            mMap.getUiSettings().setMyLocationButtonEnabled(false);
        }
        addMarketToMap(mMap);
    
        mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
    
            @Override
            public View getInfoWindow(Marker marker) {
    
                View infoWindow = getLayoutInflater().inflate(R.layout.custom_info_contents, (FrameLayout) findViewById(R.id.map), false);
    
                TextView title = infoWindow.findViewById(R.id.title);
                title.setText(marker.getTitle());
    
           //customize infowindows
    
                return infoWindow;
            }
    
            @Override
            public View getInfoContents(Marker marker) {
    
                return null;
            }
        });
    
        mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
      //your marker Onclick event
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 2014-03-17
      • 2019-04-26
      • 2012-09-27
      相关资源
      最近更新 更多