地理位置标记未显示,Google Maps API V3

时间:2011-11-26 02:56:00

标签: javascript google-maps google-maps-api-3 geolocation google-maps-markers

我正在使用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);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

  
    

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

  

在创建地图对象

后调用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);
    }

    }