2013-05-29 36 views
-2

我已經試圖找到答案,但沒有。如何增加谷歌地圖API的距離?

我創建一個網站,用戶可以搜索並選擇與谷歌的地理編碼API的地方,然後我得到那個地方的界限,但隨後用戶應該能夠提高這些界限。

例如,用戶可以在5公里增加距離那個地方,所以我將能夠增加5公里界限。

這是結果,當一些地方的用戶seacrh:

"geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 42.15420480, 
       "lng" : -6.19020910 
      }, 
      "southwest" : { 
       "lat" : 32.403740, 
       "lng" : -31.2751580 
      } 
     }, 

現在我需要增加邊界與5公里。 考慮到我必須將km轉換爲度數,我該如何在JavaScript中做到這一點?

非常感謝。

編輯

我tryed這得益於@geocodezip但事情並不qorking。我試着說「here」在「目的地點給定的距離和從起點的方位」,但有些東西是不正確的。這是代碼:

//convert decimal to degree 
function getDD2DMS(dms, type){ 
    var sign = 1, Abs=0; 
    var days, minutes, secounds, direction; 

    if(dms < 0) { sign = -1; } 
    Abs = Math.abs(Math.round(dms * 1000000.)); 
    //Math.round is used to eliminate the small error caused by rounding in the computer: 
    //e.g. 0.2 is not the same as 0.20000000000284 
    //Error checks 
    if(type == 'lat' && Abs > (90 * 1000000)){ 
     //alert(' Degrees Latitude must be in the range of -90. to 90. '); 
     return false; 
    } else if(type == 'lng' && Abs > (180 * 1000000)){ 
     //alert(' Degrees Longitude must be in the range of -180 to 180. '); 
     return false; 
    } 
    days = Math.floor(Abs/1000000); 
    minutes = Math.floor(((Abs/1000000) - days) * 60); 
    secounds = (Math.floor(((((Abs/1000000) - days) * 60) - minutes) * 100000) *60/100000).toFixed(); 
    days = days * sign; 
    if(type == 'lat') direction = days<0 ? 'S' : 'N'; 
    if(type == 'lng') direction = days<0 ? 'W' : 'E'; 
    //else return value  
    return (days * sign) + 'º ' + minutes + "' " + secounds + "'' " + direction; 
} 

//calculate 
    var R = 6371; // km 
    var d = 10; //km 
    var brng = 0; //0 vertical, 90 horizontal 
    var lat1 = (42.15420480).toRad(); 
    var lon1 = (-6.19020910).toRad(); 
    var lat2 = Math.asin(Math.sin(lat1)*Math.cos(d/R) + Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng)); 
    var lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1), Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2)); 

    console.log('lat2 = ' + getDD2DMS(lat2, 'lat')); 
    console.log('lon2 = ' + getDD2DMS(lon2, 'lng')); 

結果與鏈接不一樣。 幫幫我。非常感謝你。

+0

問問題不是更好嗎,而不是負面評估問題?謝謝。的 – Pedro

+0

可能重複[如何從度轉化爲米的距離?(http://stackoverflow.com/questions/4102520/how-to-transform-a-distance-from-degrees-to-metres) – duncan

+0

@duncan謝謝,但這個想法並不是把度數轉換成度量,而是相反,公里或度量轉化爲度數,或者至少知道多少度數等於1km(或者更小)。 – Pedro

回答

0

我不完全明白你的問題,但你可以嘗試畫一個邊框,並通過使用範圍爲偏置發送搜索。

var bounds; 
google.maps.event.addListener(drawingManager, 'rectanglecomplete', function (rectangle) { 
    bounds = rectangle.getBounds(); 
    //search 
    google.maps.event.addListener(rectangle, 'bounds_changed', function() { 
      bounds = rectangle.getBounds(); 
      //search 
    } 
} 

//this is a copy of the search function in my app 
function codeAddress(type, geo) { 
var bounds = map.getBounds(); //get the current map bounds (should not be greater than the bounding box) 
geocoder.geocode({ 'address': geo, 'bounds': bounds }, function (results, status) { //geocode the lat/long of incoming with a bias towards the bounds 
    if (status == google.maps.GeocoderStatus.OK) { //if it worked 
     map.setCenter(results[0].geometry.location); //set the center of map to the results 
     testBounds(); //test to make sure we are indeed in the bounds (have to do this because gmaps only allows for a BIAS of bounds and is not strict) 
     if (mapInBounds == "yes") { //if it is inside bounds 
      if (searchCount > 0) { //check to see if this is the first time searched, if not 
       searchResult.setMap(null); //set old search result to not display on map 
      } else { //if it is the first time 
       searchCount++; //just interate 
      } 
      searchResult = new google.maps.Marker({ //create a new marker 
       map: map, //add to current map 
       position: results[0].geometry.location //set position to search results 
      }); 
      document.getElementById("search_results").innerHTML = "<div id=\"searchResult\">" + geo + " <a href=\"#\" onclick=\"searchResultDeleteMe();\"><img src=\"images/delete.png\"/></a></div><br\>"; //add list div 
     } else { //if location found was outside strict map bounds... 
      displayMessage("Could not find within bounds."); //say so 
     } 

    } else { //if the entire geocode did not work 
     alert(L6); //localization... 
    } 
}); 
}    //get the location of a lat/long or address 
function codeLatLng(latlng) { 
if (geocoder) { 
    geocoder.geocode({ 'latLng': latlng }, function (results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      if (results[1]) { 
       document.getElementById("rgItem").innerHTML = results[0].formatted_address; 
      } 
      else { 
       document.getElementById("rgItem").innerHTML = "Geocoder failed due to: " + status; 
       document.getElementById("rgItem").innerHTML = ""; 
      } 
     } 
    }); 
} 
}     //get th