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

    }