Tom*_*Tom 2 javascript google-maps google-maps-api-3 google-maps-markers
我正在使用Google Maps API并且目前已成功根据确切位置添加标记,但我还希望能够在国家/地区添加标记,而无需拥有该国家/地区的坐标.
这可能吗?
dav*_*rad 12
确实很有可能.使用GeoCoder:https://developers.google.com/maps/documentation/javascript/geocoding
只需了解其名称,GeoCoder就可以返回一个国家/地区的纬度/经度值.
假设你已经拥有map:
geocoder = new google.maps.Geocoder();
function getCountry(country) {
geocoder.geocode( { 'address': country }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
getCountry('USA');
getCountry('Brazil');
getCountry('Denmark');
Run Code Online (Sandbox Code Playgroud)
将在您的地图上放置标记在美国,巴西和丹麦的直接中心.

| 归档时间: |
|
| 查看次数: |
8523 次 |
| 最近记录: |