Google javascript 地图 getCurrentPosition
Google javascript maps getCurrentPosition
我该如何使用:
navigator.geolocation.getCurrentPosition()
获取当前位置的坐标。
这是来自 google 站点的示例:
function initialize() {
var mapOptions = {
zoom: 6
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
// Try HTML5 geolocation
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var infowindow = new google.maps.InfoWindow({
map: map,
position: pos,
content: 'Location found using HTML5.'
});
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
}
我尝试将 var pos 部分替换为 myPos ,这是一个全局变量,但它没有用。
我的意思是我总是在 initialize() 函数后得到 myPos undefined .
在加载表单 (window) 时调用的初始化函数中获取纬度和经度 navigator.geolocation.getCurrentPosition() 的正确方法是什么?
.getCurrentPosition()
是一个异步函数,所以它需要一个回调,一旦它有这些坐标就执行,例如
navigator.geolocation.getCurrentPosition(function(position){
console.log(position);
});
这会给你这样的东西:
{
"timestamp": 1421093714138,
"coords":
{
"speed": null,
"heading": null,
"altitudeAccuracy": null,
"accuracy": 20,
"altitude": null,
"longitude": -122.4091036,
"latitude": 37.7837543
}
}
在您传递的回调中 .getCurrentPosition
您甚至可以更新变量,假设它们是事先声明的。我猜你的 myPos
变量未定义的原因是因为你连接到 google 映射 API 的方式有问题。如果您不使用 google 地图,而只想获取位置,您可以这样做:
var myPos;
navigator.geolocation.getCurrentPosition(function(position){
myPos = position;
});
哦,请确保您允许该网站访问您的位置。在 chrome 中,您会在页面顶部看到一个栏,上面写着“<网站 url > 想要使用您计算机的位置 [拒绝] [允许]”
编辑:
有两点不对。您只能在回调函数的范围内访问该变量——只有在该函数运行后才会定义 tmpPos
。正如我上面所说,.getCurrentPosition
是一个 asynchronous 函数。这意味着它会设置一个过程来获取您的地理位置,但同时会做其他事情(在您的情况下,它会继续并尝试将其他变量更新为它还没有的信息)。
此外,您正在调用自身内部的初始化函数,因此这将创建一个永不结束的无限循环函数。要解决此问题,请尝试:
function initialize(){
navigator.geolocation.getCurrentPosition(function(position){
// create the map here, because we only have access to position inside of this function
// even if we store in a global variable, it only gets updated once this callback runs
var currentPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
}
initialize();
我该如何使用:
navigator.geolocation.getCurrentPosition()
获取当前位置的坐标。
这是来自 google 站点的示例:
function initialize() {
var mapOptions = {
zoom: 6
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
// Try HTML5 geolocation
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var infowindow = new google.maps.InfoWindow({
map: map,
position: pos,
content: 'Location found using HTML5.'
});
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
}
我尝试将 var pos 部分替换为 myPos ,这是一个全局变量,但它没有用。
我的意思是我总是在 initialize() 函数后得到 myPos undefined .
在加载表单 (window) 时调用的初始化函数中获取纬度和经度 navigator.geolocation.getCurrentPosition() 的正确方法是什么?
.getCurrentPosition()
是一个异步函数,所以它需要一个回调,一旦它有这些坐标就执行,例如
navigator.geolocation.getCurrentPosition(function(position){
console.log(position);
});
这会给你这样的东西:
{
"timestamp": 1421093714138,
"coords":
{
"speed": null,
"heading": null,
"altitudeAccuracy": null,
"accuracy": 20,
"altitude": null,
"longitude": -122.4091036,
"latitude": 37.7837543
}
}
在您传递的回调中 .getCurrentPosition
您甚至可以更新变量,假设它们是事先声明的。我猜你的 myPos
变量未定义的原因是因为你连接到 google 映射 API 的方式有问题。如果您不使用 google 地图,而只想获取位置,您可以这样做:
var myPos;
navigator.geolocation.getCurrentPosition(function(position){
myPos = position;
});
哦,请确保您允许该网站访问您的位置。在 chrome 中,您会在页面顶部看到一个栏,上面写着“<网站 url > 想要使用您计算机的位置 [拒绝] [允许]”
编辑:
有两点不对。您只能在回调函数的范围内访问该变量——只有在该函数运行后才会定义 tmpPos
。正如我上面所说,.getCurrentPosition
是一个 asynchronous 函数。这意味着它会设置一个过程来获取您的地理位置,但同时会做其他事情(在您的情况下,它会继续并尝试将其他变量更新为它还没有的信息)。
此外,您正在调用自身内部的初始化函数,因此这将创建一个永不结束的无限循环函数。要解决此问题,请尝试:
function initialize(){
navigator.geolocation.getCurrentPosition(function(position){
// create the map here, because we only have access to position inside of this function
// even if we store in a global variable, it only gets updated once this callback runs
var currentPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
}
initialize();