2014-04-01 84 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);