2014-04-01 82 views
1

您好我現在用的是下面的代碼,以在地圖上顯示用戶當前位置:反向地理編碼

function drawMap() { 
    var latlng = new google.maps.LatLng(currentLatitude, currentLongitude); 
    myLatLng = latlng; 
    var mapOptions = { 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     zoomControl: true, 
     zoomControlOptions: { 
      style: google.maps.ZoomControlStyle.SMALL, 
      position: google.maps.ControlPosition.LEFT_TOP 
     }, 
    }; 
    if (boolTripTrack === true) { 
     _map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 
    } 
} 


var suc = function(p) { 
     console.log("geolocation success", 4); 
     //Draws the map initially 
     if (_map === null) { 
      currentLatitude = p.coords.latitude; 
      currentLongitude = p.coords.longitude; 
      drawMap(); 
      //reverseGeocode(currentLatitude, currentLongitude); 
     } else { 
      myLatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude); 
     } 
     //Creates a new google maps marker object for using with the pins 
     if ((myLatLng.toString().localeCompare(oldLatLng.toString())) !== 0) { 
      //Create a new map marker 
      var Marker = new google.maps.Marker({ 
       position: myLatLng, 
       map: _map 
      }); 
      if (_llbounds === null) { 
       //Create the rectangle in geographical coordinates 
       _llbounds = new google.maps.LatLngBounds(new google.maps.LatLng(p.coords.latitude, p.coords.longitude)); //original 
      } else { 
       //Extends geographical coordinates rectangle to cover current position 
       _llbounds.extend(myLatLng); 
      } 
      //Sets the viewport to contain the given bounds & triggers the "zoom_changed" event 
      _map.fitBounds(_llbounds); 
     } 
     oldLatLng = myLatLng; 
    }; 
var fail = function() { 
     console.log("Geolocation failed. \nPlease enable GPS in Settings.", 1); 
    }; 
var getLocation = function() { 
     console.log("in getLocation", 4); 
    }; 

這工作得很好,但我需要執行時,按下按鈕,這樣反向地理編碼的地址被顯示。函數是我正在使用的是:

function reversegeocode(){ 
    var latlng = new google.maps.LatLng(?, ?); 
    var geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({'latLng': latlng}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     if (results[1]) { 
       alert(results[1].formatted_address); 
     } else { 
     alert('No results found'); 
     } 
    } else { 
     alert('Geocoder failed due to: ' + status); 
    } 
    }); 
} 

我不確定如何通過當前的經度和緯度從前面的函數。誰能幫我這個??

謝謝。

回答

0

這樣定義反向地理功能,帶參數latlng

function reversegeocode(lat, lng){ 
    var latlng = new google.maps.LatLng(lat, lng); 
    var geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({'latLng': latlng}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     if (results[1]) { 
       alert(results[1].formatted_address); 
     } else { 
     alert('No results found'); 
     } 
    } else { 
     alert('Geocoder failed due to: ' + status); 
    } 
    }); 
} 

然後通過傳遞變量調用函數:reverseGeocode(currentLatitude, currentLongitude);