Isa*_*ron 13 google-maps geocoding google-maps-api-3
我正在构建一个用户为其列表提供地址的应用程序.要求一个简单的用户为他提供的每个地址提供纬度和经度当然是不切实际的!
我可以向Google Maps API提供地址吗?如果是这样,怎么样?
谢谢.
Dan*_*llo 27
当然是.使用Google Maps JavaScript API提供的地理编码服务可以非常轻松地完成这项工作.请考虑以下示例:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps Geocoding Demo 1</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 400px; height: 300px;"></div>
<script type="text/javascript">
var address = 'London, UK';
var map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.TERRAIN,
zoom: 6
});
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address
},
function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
new google.maps.Marker({
position: results[0].geometry.location,
map: map
});
map.setCenter(results[0].geometry.location);
}
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
截图:
您只需'London, UK'
将address
变量替换为支持Google地图中地理编码的任何位置即可.
归档时间: |
|
查看次数: |
17527 次 |
最近记录: |