如何获取用户点击地图的地图的经度和纬度

时间:2015-12-17 06:33:45

标签: android google-maps

我希望在按钮点击监听器中获取用户点击地图的latitudelongitude。但是当我这样做的时候。然后它获得当前位置的纬度和经度。但我希望地图上用户longitude所在的latitudeLongclick位置。

我的按钮点击监听器是:

saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
            LocationManager manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            double longi = location.getLongitude();
            double lati  = location.getLatitude();
            int mode = audioManager.getRingerMode();
            int val = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            String _name = locationName.getText().toString();

            if (_name.equals("")){
                locationName.setError("Enter a Name");
            }
            else {
                database.insertEntry(_name, lati, longi, mode, val);
                finish();
            }


        }
    });

3 个答案:

答案 0 :(得分:1)

您应implements OnMapClickListener为此并实施onMapClick(..)

@Override
public void onMapClick(LatLng point) {
//Get point as Location
}

参考Demo tutorial

答案 1 :(得分:0)

您可以按如下方式使用setOnMapClickListener

googleMap.setOnMapClickListener(new OnMapClickListener() {
   @Override
   public void onMapClick(LatLng latLng) {
                   //here you are getting latlong on map click 
   }
}

googleMap类对象

GoogleMap的位置

答案 2 :(得分:0)

您需要做的只是将全局变量设为location,并且只有在您实现OnMyLocationChangeListener这样的情况下才能在地图活动的位置监听器方法中设置纬度和经度

Location location = null;

    @Override
    public void onMyLocationChange(Location lastKnownLocation) {
        location  = lastKnownLocation
    }

然后

saveButton.setOnLongClickListener(new View.OnLongClickListener() {
 @Override
        public void onClick(View v) {
//Get the location variable which is defined earlier.
}
});