简体   繁体   English

如何使用位置名称在地图上添加标记

[英]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.

相关问题 在Android中使用Google Map V2在触摸的位置添加标记 - Add marker on touched location using google map v2 in android 如何清除谷歌地图中的位置标记 - How to clear location marker in Google Map 如何使用离子框架/添加带有多个标记的Google地图/ - How to add google map with multiple marker using ionic framework/ 如何使用osmdroid在地图中心添加标记 - How do I add a marker on the center of the map using osmdroid 如何在android的osm映射中添加更多标记 - how to add more marker in osm map in android Android开发-使用PNG作为地图,标记当前位置 - Android Development - Using a PNG as a map, marker for current location 如何在Android Studio中以字符串形式获取地图标记的位置? - How can I get the location, as a string, of a map marker in Android studio? 如何在WhatsApp中使用诸如位置共享之类的标记创建地图缩略图 - How to create a map thumbnail with marker like location sharing in whatsapp 每当用户单击地图时,如何在地图(OSM)上添加标记(航路点)? - How to add the marker(waypoints) on the map (OSM) whenever user click on map? 如何通过双击在地图上添加标记并获取坐标 [android] - How to add marker on map by double tap and get the coordinates [android]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM