Jos*_*ffy 4 javascript google-maps google-maps-api-3
编辑:它现在可以工作,但如果用户不允许或没有基于位置的服务,则不会加载.请参阅jsfiddle示例的已接受答案评论.
我已经查看了一些教程和问题,但我无法安静地了解发生了什么(或者在这种情况下,没有发生).当用户点击链接时,我正在加载我的地图.这会加载地图,其中包含用户在中心的当前位置以及用户位置的标记.但是,外面的任何标记if (navigation.location)似乎都没有加载.以下是我目前的代码:
function initialize() {
// Check if user support geo-location
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var point = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var userLat = position.coords.latitude;
var userLong = position.coords.longitude;
var mapOptions = {
zoom: 8,
center: point,
mapTypeId: google.maps.MapTypeId.HYBRID
}
// Initialize the Google Maps API v3
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
// Place a marker
new google.maps.Marker({
position: point,
map: map,
title: 'Your GPS Location'
});
});
} else {
var userLat = 53;
var userLong = 0;
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(userLat, userLong),
mapTypeId: google.maps.MapTypeId.HYBRID
}
// Place a marker
new google.maps.Marker({
position: point,
map: map,
title: 'Default Location'
});
// Initialize the Google Maps API v3
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
}
<?
for ($i = 0; $i < sizeof($userLocations); $i++) {
?>
var userLatLong = new google.maps.LatLng(<? echo $userLocations[$i]['lat']; ?>, <? echo $userLocations[$i]['long']; ?>);
new google.maps.Marker({
position: userLatLong,
map: map,
title:"<? echo $userLocations[$i]['displayName'] . ', ' . $userLocations[$i]['usertype']; ?>"
});
<?
}
?>
}
function loadMapScript() {
if (typeof(loaded) == "undefined") {
$("#showMap").css("display", "none");
$("#showMapLink").removeAttr("href");
$("#map").css("height", "600px");
$("#map").css("width", "600px");
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.googleapis.com/maps/api/js?key=MY_API_KEY&sensor=true&callback=initialize";
document.body.appendChild(script);
loaded = true;
} else {
alert("Map already loaded!");
}
}
Run Code Online (Sandbox Code Playgroud)
当用户单击链接时,将调用loadMapScript().php for循环遍历预先创建的包含所有信息的数组.
我猜我不完全理解它,就像我放的时候:
var userLatLong = new google.maps.LatLng(53, 0);
new google.maps.Marker({
position: userLatLong,
map: map,
title:"Title"
});
Run Code Online (Sandbox Code Playgroud)
进入控制台(谷歌浏览器),我收到错误:
Error: Invalid value for property <map>: [object HTMLDivElement]
Run Code Online (Sandbox Code Playgroud)
但是,我没有收到任何错误.任何帮助将非常感激!:)
navigator.geolocation.getCurrentPosition() 是异步的.
像这样重新组织您的代码:
var mapOptions = {
zoom: 8,
mapTypeId: google.maps.MapTypeId.HYBRID
}
function initialize() {
// Check if user support geo-location
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
makeMap(position.coords.latitude, position.coords.longitude, 'Your GPS Location');
});
} else {
makeMap(53, 0, 'DefaultLocation');
}
}
function makeMap(lat, lng, text) {
var point = new google.maps.LatLng(lat, lng);
mapOptions.center = point;
map = new google.maps.Map(document.getElementById("map"), mapOptions);
new google.maps.Marker({
position: point,
map: map,
title: text
});
<?php for ($i = 0; $i < sizeof($userLocations); $i++): ?>
var userLatLong = new google.maps.LatLng(<? echo $userLocations[$i]['lat']; ?>, <? echo $userLocations[$i]['long']; ?>);
new google.maps.Marker({
position: userLatLong,
map: map,
title:"<? echo $userLocations[$i]['displayName'] . ', ' . $userLocations[$i]['usertype']; ?>"
});
<?php endforeach ?>
}
Run Code Online (Sandbox Code Playgroud)
另外,考虑将$ userLocations引导到JavaScript变量中,如下所示:
var userLocations = <?php print json_encode($userLocations) ?>;
Run Code Online (Sandbox Code Playgroud)
然后在JavaScript中执行for循环,而不是混合语言.