地理位置标记不出现,谷歌地图API V3

Geolocation marker not appearing, Google Maps API V3

本文关键字:谷歌地图 API V3 地理位置      更新时间:2023-09-26

我正在使用Google map API V3构建一个移动地图。这与标准地图有点不同,因为我有标记的类别,你可以打开或关闭。一切都很好,但我似乎无法使地理定位工作。我已经尝试了我能找到的每种组合和代码片段,最多我可以让它注册位置请求,但无论我做什么,它都不会显示标记。我确信我做了一些非常简单的错误,但我是Javascript的新手,所以答案是逃避我。

这是我当前代码的缩短版本(包含所有内容的示例,但删除了许多重复的变量)。这个例子不包括地理定位功能,因为我已经尝试了很多不同的方法,我不确定我甚至会在这里放哪个。toggleMarkers()函数是我用来打开/关闭标记的,我猜这就是显示地理位置标记的问题所在。如果你能告诉我如何在这个代码中实现地理定位,我会非常感激!

var markers = [];
function initialize() {
//Setting Map
    var mapOptions = {
        zoom: 18,
        center: new google.maps.LatLng(45.73158,-122.636277),
        mapTypeId: 'satellite'
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
        map.setTilt(0);
//Marker Categories
    var markerBuildings = {
        category: 'buildings',
    }
    var markerParking = {
        category: 'parking',
    }
// Icons
    var iconBuilding = new google.maps.MarkerImage('images/building.png',
        new google.maps.Size(32, 37),
        new google.maps.Point(0,0),
        new google.maps.Point(16,32));
    var iconAmphitheater = new google.maps.MarkerImage('images/amphitheater.png',
        new google.maps.Size(32, 37),
        new google.maps.Point(0,0),
        new google.maps.Point(16,32));
//Coordinates
//Buildings
    var vmcb = new google.maps.Marker({
        position: new google.maps.LatLng(45.730513,-122.638106),
        map: map,
        url: 'http://www.google.com/',
        icon: iconBuilding,
        data: markerBuildings
    });
    markers.push(vmcb);     
    var vadm = new google.maps.Marker({                        
        position: new google.maps.LatLng(45.730899,-122.637742),
        map: map,
        url: 'http://www.google.com/',
        icon: iconBuilding,
        data: markerBuildings
    });
    markers.push(vadm);
}
function toggleMarkers(attr,val) {
    if (markers){
        for (i in markers) {
            if(markers[i].data[attr] == val){
                var visibility = (markers[i].getVisible() == true) ? false : true;
                markers[i].setVisible(visibility);
            }
        }
    }
}

如果你能告诉我如何在这个代码中实现地理定位,我会非常感激!

创建地图对象后调用getGeoLocation()(如下)

    function getGeoLocation() {
          // http://stackoverflow.com/questions/3397585/navigator-geolocation-getcurrentposition-sometimes-works-sometimes-doesnt
    var options = null;
    if (navigator.geolocation) {
        if (!browserChrome) {
            // By using the 'maximumAge' option, the position
            // object is guaranteed to be at most 15 seconds old.
            // FF and Safari seem to like these options; Chrome does not
            options={enableHighAccuracy: false, maximumAge: 15000, timeout: 30000};
          }
        else {
            // Request a position. We only accept cached positions, no matter what 
            // their age is. If the user agent does not have a cached position at
            // all, it will immediately invoke the error callback.
            // http://dev.w3.org/geo/api/spec-source.html
            // Chrome will not allow this submitted via the file:// protocol
            options={maximumAge:Infinity, timeout:0};
          }
        navigator.geolocation.getCurrentPosition(function(position) {
            centerIsSet = true
            canGeolocate = true;
            var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            map.setCenter(pos);
          },
             getGeoLocationErrorCallback(true),
            options);
    }
    else {
      // browser doesn't support geolocation
      getGeoLocationErrorCallback(false);
    }
    }