[英]How to add marker on the map using the location name
@Override
public void onMapReady(GoogleMap googleMap){
map = googleMap;
LatLng tinching = new LatLng(22.3290803, 114.1478785);
map.addMarker(new MarkerOptions().position(tinching).title("Sham Shui Po"));
map.moveCamera(CameraUpdateFactory.newLatLng(tinching));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(tinching,15));
}
Hi all, How can I add a marker on the map using the location name(such as London) instead of input the Latitude and Longitude? 大家好,如何使用位置名称(例如伦敦)在地图上添加标记,而不是输入纬度和经度?
First you will need to use Geocoder ( https://developer.android.com/reference/android/location/Geocoder.html ) to get lat and long of city by name. 首先,您需要使用Geocoder( https://developer.android.com/reference/android/location/Geocoder.html )来获取名称所在的城市。
if(Geocoder.isPresent()){
try {
String cityName= "NameOfTheLocation";
Geocoder geocoder = new Geocoder(this);
List<Address> addresses= geocoder.getFromLocationName(cityName, 5); // get the found Address Objects
List<LatLng> latlng = new ArrayList<LatLng>(addresses.size()); // A list to save the coordinates if they are available
for(Address a : addresses){
if(a.hasLatitude() && a.hasLongitude()){
latlng .add(new LatLng(a.getLatitude(), a.getLongitude()));
}
}
} catch (IOException e) {
// handle the exception
}}
Then use your latlng to add marker 然后使用您的latlng添加标记
map.addMarker(new MarkerOptions().position(latlng).title("Sham Shui Po"));
map.moveCamera(CameraUpdateFactory.newLatLng(latlng));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng,15));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.