-2

I already googled trying to find the answer, but nothing.

I'm creating a site where user can search and choose a place with google geocoding api, then i get the bounds of that place, but then user should be able to increase those bounds.

For example, user could increase distance from that place in 5km, so i would be able to increase bounds in 5km.

This is the result when user seacrh for some place:

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

Now i need to increase the bounds with 5km. How can i do that in javascript considering that i have to convert km into degrees?

Thank you very much.

EDIT

I tryed this thanks to @geocodezip but something is not qorking. I try as it says here at "Destination point given distance and bearing from start point", but something is not working right. This is the code:

//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'));

The result is not the same as the link. Help me. Thank you very much.

Pedro
  • 77
  • 8
  • Wouldn't it be better to tell me what is wrong with the question, instead of assessing the question negatively? Thank you. – Pedro May 29 '13 at 13:56
  • possible duplicate of [How to transform a distance from degrees to metres?](http://stackoverflow.com/questions/4102520/how-to-transform-a-distance-from-degrees-to-metres) – duncan May 29 '13 at 14:04
  • @duncan thank you, but the idea is not to convert degrees to metters, but the opposite, km or metters to degrees, or at least to know how many degrees are iqual to 1km (or metters). – Pedro May 29 '13 at 14:15
  • The [number of km/degree of longitude](http://en.wikipedia.org/wiki/Longitude#Length_of_a_degree_of_longitude) varies with distance from the equator. [For Latitude it is fairly constant](http://en.wikipedia.org/wiki/Latitude#The_length_of_a_degree_of_latitude) – geocodezip May 29 '13 at 15:51
  • @geocodezip thank you. I saw the links you post, but is too much for me!! lol Is there an example, a formula in javascript to use? Thank you. – Pedro May 29 '13 at 18:05
  • The usual reference (and the first result to searching for "latitude longitude javascript") is [here](http://www.movable-type.co.uk/scripts/latlong.html) – geocodezip May 29 '13 at 18:23
  • Thank you @geocodezip i edited the question with some code, please give a look. The result is not the same if i try it al the link u gave me. Thank you. – Pedro May 29 '13 at 21:51

1 Answers1

0

I do not fully understand your question, but you could try to draw a bounding box and send a search through using the bounds as a bias.

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
yoyo
  • 79
  • 3
  • 11