2016-12-21 29 views
0

在觸摸屏上添加標記我想通過觸摸屏幕在Google地圖上添加標記。 Google地圖工作正常。但在接觸時,沒有任何事情發生。有另一種方法嗎?在Google地圖上使用android

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

回答

1

試試這個

// 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); 
     } 
    }); 
+0

感謝您接受的答案。我很高興我可以幫助..;) – itzswan

+0

如果可以的話請請upvote答案。 – itzswan

2

在您的mMap對象上設置點擊偵聽器; OnMapClickListener。在onMapClick事件中,將代碼添加到標記中。

相關問題