java - 如何从地图中的标记获取纬度和经度
问题描述
我使用此函数getLocation从设备获取纬度和经度。但我正在寻找的是一种使用标记的方法,将其定位在地图mapView中并获取它的纬度和经度。有例子吗?
一种方法是在地图中心做一个标记,然后移动地图直到标记位于地图的所需位置,然后读取标记的经度和纬度
public void getLocation() {
try {
locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
// getting GPS status
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
// First get location from Network Provider
if (isNetworkEnabled) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
Log.d("Network", "Network");
if (locationManager != null) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
//return TODO;
}
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitud = location.getLatitude();
longitud = location.getLongitude();
}
}
}
//get the location by gps
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitud = location.getLatitude();
longitud = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
解决方案
另一种选择是在用户触摸 mapView 时监听事件
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(point.latitude, point.longitude))
.title("Location touched");
}
}
});
这将每次创建一个标记,如果您想替换它,只需在添加另一个标记之前清除其他标记。
推荐阅读
- javascript - 我收到 403 错误,需要知道如何修复它
- c++ - 字符大小 C/C++
- django - Django:如何在引导模板上手动呈现表单的每个字段?
- visual-studio - 用于构建 MFC 应用程序的 VC++ 替代方案
- json - 如何在 groovy 中转义 Json 字符串?
- substrate - 如何列出 Polkadot 网络中的所有平行链?
- windows - 如何在本地计算机上创建第二个 git 远程?
- sikuli-x - 如何使用循环保存不同的文件名?
- javascript - 如果未定义函数,请定义函数
- azure - 使用 Github Actions 将 Next.js 部署到 Azure 应用服务的问题