0

我正在使用Google Map API V3构建移动地图。这与标准地图有点不同,因为我的标记类别可以打开/关闭。一切都很好,但我似乎无法获得地理位置的工作。我已经尝试了所有可以找到的组合和代码片段,并且至多可以注册位置请求,但无论我做什么,它都不会显示标记。我确信我做了一件非常简单的事情,但我对JavaScript很陌生,所以答案是回避我。未出现地理位置标记,Google地图API V3

这里是我当前代码的简短版本(包含所有内容的示例,但是删除了很多重复变量)。这个例子不包括地理定位功能,因为我已经尝试了很多不同的方式,我不确定哪一个我会放在这里。 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); 
      } 
     } 
    } 
} 
+0

不知道你的地理位置在此代码的上下文的意思。你可能是指地理编码:例如,采取街道地址并将其变成(lat,lng)? –

+0

您的地理位置代码在哪里。您粘贴的代码似乎是正确的? – Unknown

回答

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); 
    } 

    }