我如何使用GMaps API来显示默认为用户的HTML5地理位置的可拖动引脚?
How can I use GMaps API to display a draggable pin that defaults to the user's HTML5 geolocation?
标题说明了一切,真的。我试图使用Google Maps JavaScript API来创建一个带有可拖动引脚的地图,该引脚默认为由HTML5地理定位API定义的用户当前地理位置,但我使用的代码根本不会输出任何东西。它可能是一些非常简单的东西(我是web开发的新手,善良!),但我无法弄清楚它是什么。
这是我使用的代码:
<input type="text" name="ilat" style="display: none;">
<input type="text" name="ilong" style="display: none;">
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<div style="overflow:hidden;height:500px;width:600px;">
<div id="gmap_canvas" style="height:500px;width:600px;"></div>
<style>
#gmap_canvas img {
max-width: none!important;
background: none!important
}
</style><a class="google-map-code" id="get-map-data" /></div>
<script type="text/javascript">
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
var locmap = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
} else {
var locmap = new google.maps.LatLng(51.50532252465797, -0.14202490290529113);
}
}
function init_map() {
var myOptions = {
zoom: 16,
center: locmap,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("gmap_canvas"), myOptions);
marker = new google.maps.Marker({
draggable: true,
map: map,
position: locmap)
});
infowindow = new google.maps.InfoWindow({
content: "<b>Last known location</b> "
});
google.maps.event.addListener(marker, "click", function() {
infowindow.open(map, marker);
});
infowindow.open(map, marker);
}
google.maps.event.addDomListener(window, 'load', init_map);
google.maps.event.addListener(marker, 'dragend', function(event) {
document.getElementById("ilat").value = this.getPosition().lat();
document.getElementById("ilong").value = this.getPosition().lng();
});
</script>
有人能告诉我我做错了什么吗?谢谢!
你的代码恐怕有点乱!有一些语法错误在它,所以你应该总是检查JavaScript控制台(汉堡->更多工具-> Chrome上的JavaScript控制台)在这些情况下寻求帮助。
语法错误之后,主要的问题是变量范围都错了。请参考这个整洁的版本,并在这里查看示例Google代码。
var locmap = new google.maps.LatLng(51.50532252465797, -0.14202490290529113);
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(locmap) {
locmap = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
},
function() {});
}
}
function init_map() {
getLocation();
var myOptions = {
zoom: 16,
center: locmap,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("gmap_canvas"), myOptions);
marker = new google.maps.Marker({
draggable: true,
map: map,
position: locmap
});
infowindow = new google.maps.InfoWindow({
content: "<b>Last known location</b> "
});
google.maps.event.addListener(marker, "click", function() {
infowindow.open(map, marker);
});
infowindow.open(map, marker);
google.maps.event.addListener(marker, 'dragend', function(event) {
document.getElementById("ilat").value = this.getPosition().lat();
document.getElementById("ilong").value = this.getPosition().lng();
});
}
google.maps.event.addDomListener(window, 'load', init_map);
<input type="text" name="ilat" style="display: none;">
<input type="text" name="ilong" style="display: none;">
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<div style="overflow:hidden;height:500px;width:600px;">
<div id="gmap_canvas" style="height:500px;width:600px;"></div>
<style>
#gmap_canvas img {
max-width: none!important;
background: none!important
}
</style><a class="google-map-code" id="get-map-data" />
</div>
相关文章:
- 安卓平台上的QWebView HTML5地理位置
- Possibile使用Manifest v2下的HTML5地理位置API将lat/long转换为国家/地区
- 使用html5地理位置api创建地理围栏
- HTML5地理位置,询问位置
- HTML5地理位置非异步
- 使用下拉框中的选择更改HTML5中的地理位置地图
- jquery,在html5中使用带有地理位置的deferred
- 如何使用html/html5中的用户输入获取完整的地理位置详细信息
- 在 HTML5 Web Worker 中使用地理位置
- 如何使用HTML5地理位置javascript初始化带有地理位置(用户的当前位置)的Google Maps API
- 各种浏览器中的HTML5地理位置API
- HTML5 服务器地理位置(不是客户端位置)
- 测试是否已授予 html5 地理位置权限
- iOS HTML5 地理位置 - 当浏览器无权使用地理位置时没有错误
- 即使设置了所有选项,HTML5地理位置也不起作用
- 为什么这个依赖于HTML5地理位置的javascript代码出错
- 使用html5中的地理位置在谷歌地图中显示坐标列表
- HTML5地理位置地址表单填充器未捕获语法错误:意外的标记<
- HTML5地理位置监视位置正在重新加载完整的地图,而不仅仅是位置
- 如何检索本地变量(地理位置HTML5和谷歌地图API)