使用android触摸屏幕时在Google地图上添加标记

时间:2016-12-21 03:54:55

标签: android google-maps

我想通过触摸屏幕在Google地图上添加标记。谷歌地图工作正常。但在触摸时,一切都没有发生。还有另一种方式吗?

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

  if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    return;
  }
  mMap.setMyLocationEnabled(true);
}

2 个答案:

答案 0 :(得分:2)

在mMap对象上设置单击侦听器; OnMapClickListener。在onMapClick事件中,将代码添加到标记中。

答案 1 :(得分:2)

试试这个

// Setting a click event handler for the map
    googleMap.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng latLng) {

            // Creating a marker
            MarkerOptions markerOptions = new MarkerOptions();

            // Setting the position for the marker
            markerOptions.position(latLng);

            // Setting the title for the marker.
            // This will be displayed on taping the marker
            markerOptions.title(latLng.latitude + " : " + latLng.longitude);

            // Clears the previously touched position
            googleMap.clear();

            // Animating to the touched position
            googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));

            // Placing a marker on the touched position
            googleMap.addMarker(markerOptions);
        }
    });