我希望在按钮点击监听器中获取用户点击地图的latitude
和longitude
。但是当我这样做的时候。然后它获得当前位置的纬度和经度。但我希望地图上用户longitude
所在的latitude
和Longclick
位置。
我的按钮点击监听器是:
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();
}
}
});
答案 0 :(得分:1)
您应implements OnMapClickListener
为此并实施onMapClick(..)
@Override
public void onMapClick(LatLng point) {
//Get point as Location
}
答案 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.
}
});