尝试使用谷歌API v3从地址获取位置
trying to get location from address using google api v3
我的html中有这个javascript,我想在这里做的是获得给定位置的地图表示
<script type="text/javascript">
var geocoder;
var map;
function getmaploc() {
geocoder = new google.maps.Geocoder();
var myOptions = {
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
geocoder.geocode( 'cairo', 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);
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
});
}
</script>
<body onload="getmaploc()">
<div id="map_canvas" style="width: 320px; height: 480px;"></div>
</body>
我不知道这有什么问题??
有多个错误,
- 省略的大括号
- 首先初始化地图
- geocode() 需要一个 GeocoderRequest-object 作为参数
固定代码:
var geocoder;
var map;
function getmaploc() {
geocoder = new google.maps.Geocoder();
var myOptions = {
zoom : 8,
mapTypeId : google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
geocoder.geocode({
address : 'cairo'
}, function(results, status) {
console.log(results);
if(status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
new google.maps.Marker({
map : map,
position : results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
相关文章:
- 获取docker容器中运行的节点应用程序的IP地址
- 如何在asp.net中获取地图上的数据库地址
- 从express请求对象获取客户端MAC地址
- 从URL地址获取数据
- 从服务提供商 IP 地址获取设备 IP 地址
- 我们如何使用 jQuery 通过 IP 地址获取国家/地区名称
- mapquest 中的内置函数,用于从地址获取纬度
- 从地址获取嵌入的街景
- 从没有谷歌地理位置的地址获取纬度/经度
- 尝试使用谷歌API v3从地址获取位置
- 谷歌地图(javascript API):从地址获取GPS坐标
- 使用JavaScript/Node.js从ip地址获取两个字母的大陆代码
- JavaScript从电子邮件地址获取信息
- 如何从web地址获取当前会话并在javascript中使用它?
- 从地址获取经纬度并写入文件
- JavaScript如何从ip地址获取地理位置
- 使用javaScript(Ajax)从其地址获取页面详细信息
- 从另一个域通过字符串中的ip地址获取变量
- Leaflet.Geosearch:从地址获取lon/lat
- 从URL地址获取AJAX工具提示的数据