var myloc;
function initialize() {
var mapOptions = {
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
var mylocOptions = {
draggable: true,
animation: google.maps.Animation.DROP,
title: "You are here..."
};
myloc = new google.maps.Marker(mylocOptions);
<% if !signed_in? || !current_user.loc %>
if (navigator.geolocation) navigator.geolocation.getCurrentPosition(function(pos) {
var me = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
myloc.setPosition(me);
myloc.setMap(map);
map.setCenter(me);
$.ajax({
data: { me: me.toString() },
type: 'POST',
url: '/set-location'
})
}, function(error) {
var address = prompt('Where are you looking?');
geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var me = results[0].geometry.location
myloc.setPosition(me);
myloc.setMap(map);
map.setCenter(me);
} else {
alert("Geocode was not successful for the following reason: " + status);
};
});
});
<% else %>
var me = new google.maps.LatLng(<%= current_user.loc %>);
myloc.setPosition(me);
myloc.setMap(map);
map.setCenter(me);
<% end %>
}
所以,我不是想提醒一下,但这就是我发现问题的方法。如果为真,myloc is undefined
。如果为false,则为myloc = Object object
。 navigator.geolocation
函数中是否有允许myloc
被识别的内容?
我想通过在初始化函数之外声明var myloc
,一切都很好吃。范围问题?谷歌地图诡计?
答案 0 :(得分:0)
Geolocating是一个异步过程。
执行getCurrentPosition
回调时,标记已经创建,因为浏览器会在请求用户位置时继续执行脚本。