0

How to convert latitude and longitude value into address in javascript? I had tried geolocation, but its display only the 10 address. It will not display more then ten address. my code:

                        if (result.Status.code == G_GEO_SUCCESS) {

                            for ( var i = 0; i < result.Placemark.length; i++) {

                                var addres = result.Placemark[i].address;
                                // var t1=setTimeout(function(){x.value="2 seconds"},2000);
                                if (addres == "") {
                                    // alert('blank');
                                    document.getElementById("msg" + noofid).innerHTML = "Unable to find location";
                                } else {
                                    document.getElementById("msg" + noofid).innerHTML = result.Placemark[i].address;

                                }
                                //alert("address");
                            }

                        }
                        // ====== Decode the error status ======
                        else {
                            var reason = "Code " + result.Status.code;
                            if (reasons[result.Status.code]) {
                                reason = reasons[result.Status.code];
                            }
                            alert('Could not find "' + search + '" '
                                    + reason);
                        }
                    });

}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Dhandabani
  • 81
  • 1
  • 1
  • 7

1 Answers1

0

Hello since you have a specific issue regarding this api, and I came across your question I'll share with you some source from an old project of mine that had similar needs.

// get nearest coords based on given tracking
tb.prototype.getNearestStreetCoords = function(lat, lng, callback) {

    var parentClass = this;

    var nearestStreet = {
        latitude: lat,
        longitude: lng
    };

    if (parentClass.useNearest != true) callback(nearest); 

    var request = {
        origin: new google.maps.LatLng(lat, lng), 
        destination: new google.maps.LatLng(lat, lng),
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };

    directionsService = new google.maps.DirectionsService();

    directionsService.route(request, function(response, status) {

        if (status == google.maps.DirectionsStatus.OK) {       
            callback(response);
        } else {
            callback(false);
        }

    });     
}


// get nearest address based on lat & lng
tb.prototype.getAddress = function(lat, lng, callback) {

    geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({'latLng': new google.maps.LatLng(lat, lng)}, function(results, status) {

        var streetAddress = null;

        if (status == google.maps.GeocoderStatus.OK) {
            streetAddress = results[0].formatted_address;
        } 

        callback(streetAddress);
    }); 
}
0x_Anakin
  • 3,229
  • 5
  • 47
  • 86
  • Thank you Panagiotis , The thing is i have 50 latlong values, while i'm converting into address it display only 10 address.It will not show all the values... Thank you for spending your valuable time . – Dhandabani Sep 05 '13 at 11:04
  • Hmm do you get a reply from the service or is it unable to resolve specific lat&lng to address ? I had similar issue that's why I was using the getNearestSteetCoords function to give me the nearest street coords to the lat & lng I provided. If I remember correctly this ensures taht you have a valid point which has a registered address also. – 0x_Anakin Sep 05 '13 at 11:13
  • Ya i get the reply from server.but limited address only its replied, ok i will try this way .Thanku you so much. – Dhandabani Sep 05 '13 at 11:27
  • No problem. Let me now if it works for you – 0x_Anakin Sep 05 '13 at 11:35