jquery谷歌地图无法设置位置
jquery google maps not able to set location
我正在使用jquery谷歌地图插件。这是我的代码
$( document ).ready(function() {
var locations = [];
locations.push([{
'position': '57.7973333,12.0502107',
'bounds': true
}, 'Hello World!']);
$('#map_canvas').gmap().bind('init', function(ev, map) {
for (var i=0; i<locations.length; i+=1) {
var loc = locations[i];
/*
$('#map_canvas').gmap('addMarker', loc[0]).click(function() {
$('#map_canvas').gmap('openInfoWindow', {'content': loc[1]}, this);
});
*/
}
$('#map_canvas').gmap('option', 'zoom', 10);
if ( navigator.geolocation ) {
function success(pos) {
// Location found, show map with these coordinates
//drawMap(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
$('#map_canvas').gmap({
'center': new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude)
});
//alert(pos.coords.latitude);
//alert(pos.coords.longitude);
//$('#map_canvas').gmap({ 'center': '42.345573,-71.098326' });
}
function fail(error) {
$('#map_canvas').gmap({ 'center': '42.345573,-71.098326' });
}
// Find the users current position. Cache the location for 5 minutes, timeout after 6 seconds
navigator.geolocation.getCurrentPosition(success, fail, {maximumAge: 500000, enableHighAccuracy:true, timeout: 6000});
} else {
$('#map_canvas').gmap({ 'center': '42.345573,-71.098326' });
}
});
});
问题是,我想将位置(而不是标记)设置为用户所在的位置,但问题是代码不会移动位置。它将其保持在默认的经度/经度 0,0。我不知道出了什么问题。我在成功函数中放了一个警报语句,它会发出警报。
有谁知道发生了什么?
谢谢
Javascript经常使用异步进程。 这意味着:您(脚本编写者)发送一个请求,并附有回调。 Javascript需要任何时间,只要它准备好了,它就会调用该回调。
与此同时,它继续脚本的其余部分;它不会等待。
地理位置是这些异步服务之一。 您发送请求,脚本的其余部分将继续;如果地理位置找到该位置,则只有这样您才能使用该功能。
这意味着你总是需要一个默认值;你总是需要为地理位置失败编写默认代码。
下面是一个示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jquery google maps not able to set location</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<script src="http://jquery-ui-map.googlecode.com/svn/trunk/ui/jquery.ui.map.js"></script>
<script>
function initialize() {
// we send a request to get the position of the client.
// This can take some time, so we don't wait.
// We create the map and set a default location.
// If and when geolocation found the client's location, we change the center of the map
// else, we don't
// new map, with default location
$('#map_canvas').gmap({
center: '42.345573,-71.098326',
zoom: 10
});
if ( navigator.geolocation ) {
function success(pos) {
// set the center
$('#map_canvas').gmap('option', 'center', new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
}
function fail(error) {
// maybe you want to do something here
}
// start of the request to get the client's position
navigator.geolocation.getCurrentPosition(success, fail, {maximumAge: 500000, enableHighAccuracy:true, timeout: 6000});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<style>
#map_canvas {
height: 500px;
}
</style>
</head>
<body>
<div id="map_canvas"></div>
</body>
</html>
相关文章:
- 设置画布渲染器的x和y位置
- 是否可以使用flexbox显示模型设置元素的滚动位置
- Javascript或浏览器以不同的方式设置位置
- d3 sankey图-如何设置y位置
- 用three.js和CSG设置位置
- 有人知道在固定大小的弹出矩形上设置x/y位置的简单方法吗
- jquery谷歌地图无法设置位置
- 根据先前的参数设置位置 $routeChangeError 使用角度 JS
- Jquery Gritter设置位置
- 获取元素偏移量在设置位置之前返回相同的值:绝对值
- Usings传单js从变量中设置位置
- 使用svg.js设置位置动画的正确方法是什么
- 基于浏览器区域设置/位置的自动日期格式
- 当位于bootstrap选项卡内时,变形不会设置位置
- 当我在window.open中设置位置no时,地址栏的详细信息不会被隐藏
- 使用UI-Router和AngularJs设置位置更改后的消息
- 设置位置,如果不可见的图片不存在
- Draw2D设置位置
- 如何在Firefox、IE的SoundJS中正确设置位置
- jQuery.offset()设置位置:绝对.我需要位置:固定