Javascript 地理定位谷歌地图jquery
我试图做一个脚本,定位用户的当前位置,并在重新加载页面后设置一个随机坐标。我可以初始化地图和随机坐标函数,但地理位置似乎没有找到我的位置,尽管它会在信息窗口中打印出消息Javascript 地理定位谷歌地图jquery,javascript,jquery,google-maps,Javascript,Jquery,Google Maps,我试图做一个脚本,定位用户的当前位置,并在重新加载页面后设置一个随机坐标。我可以初始化地图和随机坐标函数,但地理位置似乎没有找到我的位置,尽管它会在信息窗口中打印出消息 var map; var pos; function initialize() { //here is the starting for the map, where it will begin to show var latlng = new google.maps.LatLng(5
var map;
var pos;
function initialize() {
//here is the starting for the map, where it will begin to show
var latlng = new google.maps.LatLng(59.2982762, 17.9970823);
var myOptions = {
zoom: 13,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);
//geolocation
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var infowindow = new google.maps.InfoWindow({
map: map,
position: pos,
content: "You are here"
});
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
}
else {
handleNoGeolocation(false);
}
function handleNoGeolocation(errorflag) {
if (errorflag) {
alert('Geolocation not supported');
initialLocation = stockholm;
}
else {
alert('balblababa');
initialLocation = siberia;
};
var infowindow = new google.maps.InfoWindow(options);
map.setCenter(options, position);
}
//stop geolocation
var flagAreas = [
[59.2967322, 18.0009393],
[59.2980245, 17.9971503],
[59.2981078, 17.9980875],
[59.2982762, 17.9970823],
[59.2987638, 17.9917639],
[59.2987649, 17.9917824],
[59.2987847, 17.9917731],
[59.2988498, 17.991684],
[59.2988503, 17.9981593],
[59.3008233, 18.0041763],
[59.3014033, 18.0068793],
[59.3016619, 18.0137766]
];
var random = flagAreas.sort(function() {
return Math.random() - 0.5 })[0];
var marker = new google.maps.Marker({
position: new google.maps.LatLng(random[0], random[1]),
map: map,
});
}
window.onload = initialize;
</script>
var映射;
var-pos;
函数初始化(){
//这是地图的起点,它将从这里开始显示
var latlng=新的google.maps.latlng(59.2982762,17.9970823);
变量myOptions={
缩放:13,
中心:拉特林,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById('map-canvas'),myOptions);
//地理定位
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(函数(位置){
initialLocation=new google.maps.LatLng(position.coords.lation,position.coords.longitude);
var infowindow=new google.maps.infowindow({
地图:地图,
职位:pos,,
内容:“你在这里”
});
地图设置中心(pos);
},函数(){
手持导航(真);
});
}
否则{
手动定位(假);
}
函数handleNogeLocation(errorflag){
如果(错误标志){
警报(“不支持地理位置”);
初始位置=斯德哥尔摩;
}
否则{
警报(“巴尔布拉巴巴”);
初始位置=西伯利亚;
};
var infowindow=new google.maps.infowindow(选项);
地图设置中心(选项、位置);
}
//停止地理定位
var flagAreas=[
[59.2967322, 18.0009393],
[59.2980245, 17.9971503],
[59.2981078, 17.9980875],
[59.2982762, 17.9970823],
[59.2987638, 17.9917639],
[59.2987649, 17.9917824],
[59.2987847, 17.9917731],
[59.2988498, 17.991684],
[59.2988503, 17.9981593],
[59.3008233, 18.0041763],
[59.3014033, 18.0068793],
[59.3016619, 18.0137766]
];
var random=flagAreas.sort(函数(){
返回Math.random()-0.5}[0];
var marker=new google.maps.marker({
位置:new google.maps.LatLng(random[0],random[1]),
地图:地图,
});
}
window.onload=初始化;
感谢Suvi Vignarajah
这是工作代码
var map;
var pos;
function initialize() {
//here is the starting for the map, where it will begin to show
var latlng = new google.maps.LatLng(59.2982762, 17.9970823);
var myOptions = {
zoom: 13,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);
//geolocation
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var infowindow = new google.maps.InfoWindow({
map: map,
position: initialLocation,
content: "You are here"
});
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
}
else {
handleNoGeolocation(false);
}
function handleNoGeolocation(errorflag) {
if (errorflag) {
alert('Geolocation not supported');
initialLocation = stockholm;
}
else {
alert('balblababa');
initialLocation = siberia;
};
var infowindow = new google.maps.InfoWindow(options);
map.setCenter(options, position);
}
//stop geolocation
var flagAreas = [
[59.2967322, 18.0009393],
[59.2980245, 17.9971503],
[59.2981078, 17.9980875],
[59.2982762, 17.9970823],
[59.2987638, 17.9917639],
[59.2987649, 17.9917824],
[59.2987847, 17.9917731],
[59.2988498, 17.991684],
[59.2988503, 17.9981593],
[59.3008233, 18.0041763],
[59.3014033, 18.0068793],
[59.3016619, 18.0137766]
];
var random = flagAreas.sort(function() {
return Math.random() - 0.5 })[0];
var marker = new google.maps.Marker({
position: new google.maps.LatLng(random[0], random[1]),
map: map,
});
}
window.onload = initialize;
您的信息窗口
position
设置为错误的变量,请将其设置为initialLocation
。另外,不确定您是否知道,但是您声明了两次映射(全局和本地到initialize()
)-这没有问题,因为一切都发生在initialize()
的范围内,但不是很好的实践)。非常感谢!不错,别忘了map.setCenter(pos)代码>也应更改为map.setCenter(初始位置)代码>