Undefined need help

Hi, can ask some help I am having problem in processing the reverse geocode,…I want to convert the lat & lng into address but it seems like some address are undefined . can you help me please why is it undefined.


<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<meta http-equiv="content-language" content="en"/>



<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>


<script type="text/javascript">

var map;
var mycoordinates;
var mycoordinatesPath;
var initial;
var coordinateLatLng = [];
var datacoord = null;
var latlng;

var nextAddress = 0;
var r;
var address = [];
var delay = 100;
var progress;
var x = 0;
var mptype;

var latLngBounds = new google.maps.LatLngBounds();


function initialize() {
    geocoder = new google.maps.Geocoder();

    initial = new google.maps.LatLng(10.296954, 123.896785);

    var mapOptions = {
        zoom: 16,
        center: initial,
        mapTypeId: mptype

    };
    map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
}


calcRoute();

function calcRoute() {


    datacoord = null;

    $('.mytablesearch').css('display', 'none');
    $('#loctable tr').nextAll().remove();

    var data = [
        {lat:40.8689892,lng:-72.5175893},
        {lat:40.8198217,lng:-72.62898369999999},
        {lat:40.9882475,lng:-72.30024449999996},
        {lat:40.8689892,lng:-72.5175893},
        {lat:40.81503499999999,lng:-72.61768699999999},
        {lat:41.016989,lng:-71.99677659999998},
        {lat:40.9818,lng:-72.32487200000003},
        {lat:40.8689892,lng:-72.5175893},
        {lat:40.8689892,lng:-72.5175893},
        {lat:40.959321,lng:-72.296198},
        {lat:40.96343350000001,lng:-72.18480090000003},
        {lat:40.9737117,lng:-72.14368839999997},
        {lat:40.8119684,lng:-72.71858299999997},
        {lat:40.834894,lng:-72.69198140000003},
        {lat:40.8432396,lng:-72.6505957},
        {lat:40.96343350000001,lng:-72.18480090000003},
        {lat:40.96343350000001,lng:-72.18480090000003},
        {lat:40.884267,lng:-72.3895296},
        {lat:40.94361749999999,lng:-72.30447229999999},
        {lat:40.9253776,lng:-72.27813750000001}

    ];


    datacoord = data;
    theNext();

}




function getAddress(nextAddress) {

    latlng = new google.maps.LatLng(datacoord[nextAddress].lat, datacoord[nextAddress].lng);

    geocoder.geocode({latLng:latlng}, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {
                var add = results[0].formatted_address;

                address.push(add);

            } else {
                console.log("no result found");

            }
        } else {
            if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
                nextAddress--;
                delay++;

            } else {
                var reason = "Code " + status;
                var msg = 'address="' + latlng + '" error=' + reason + '(delay=' + delay + 'ms)<br>';
                console.log("MESSAGE=", msg);
            }


        }

        theNext();

    });


}


function theNext() {

    if (nextAddress < datacoord.length) {

        setTimeout('getAddress("' + nextAddress + '")', delay);
        nextAddress++;


    } else {
        $('.container-progressbar').css('display', 'none');
        display(datacoord);
        console.log("Done loading");
        map.fitBounds(latLngBounds);
    }
}


function display(datacoord) {

    var lno = 1;
    $('.mytablesearch').css('display', 'block');
    for (var n = 0; n < datacoord.length; n++) {
        $('#loctable tr:last').after('<tr><td>' + lno + '</td><td>' + datacoord[n].lat + '</td><td>' + datacoord[n].lng + '</td><td>' + address[n] + '</td></tr>');

        lno++;
    }
}



google.maps.event.addDomListener(window, 'load', initialize);
</script>



<style type="text/css">
    .mytablesearch{
        bottom: 17px;
        position: absolute;
        z-index:1000;
        height: 170px !important;
        overflow: auto;
        display:none;
        width: 100%;


    }

</style>

</head>
<body>


<div id="basic_map" data-role="page">
    <div data-role="content">

                   <div id="map_canvas" style="position:absolute; width:100%; height:100%;"></div>


                    <div class="mytablesearch">
                        <div class="table-responsive">
                            <table class="table table-striped" id="loctable">
                                <tr>
                                    <th>#</th>
                                    <th>latitude</th>
                                    <th>longitude</th>
                                    <th>Address</th>

                                </tr>

                            </table>
                        </div>
                    </div>

</body>
</html>


Thank you in advance.

Hi jemz,

First of all, I know I’ve mentioned this to you before, but it’d be really helpful if you could try to use more descriptive thread titles. If you’d put something like “Need help with reverse geocoding using Google Maps”, for example, then you’re more likely to generate interest from forum members who might know something about the topic.

Regarding your question, the problem here is that you’re hitting the quota limit by submitting requests too frequently. I know you’ve got some checks in place to handle that, but unfortunately there are a couple things preventing them from working properly:

Your getAddress function defines an argument called nextAddress, which overrides the global nextAddress within the function’s scope. When you decrement nextAddress, you’re altering this local variable rather than the global one, and so the current set of coordinates is never processed again. What you need to do here is use a different name for the argument:

function getAddress(nextIndex) {
    latlng = new google.maps.LatLng(datacoord[nextIndex].lat, datacoord[nextIndex].lng);

The other problem is that you’re only incrementing the delay by one millisecond, which isn’t going to make any difference. Google’s map API docs suggest a delay of 2 seconds after receiving the OVER_QUERY_LIMIT status. In my tests, an initial delay of 0.75 seconds between requests will allow you to process 9 sets of coordinates before hitting a rate limit, so you might want to play around with the delays to find an optimal setting.

Hi fretburner,

Thank you so much for the reply…I will try your solution and i will let you know :slight_smile:

Hello fretburner,

I just tested it and i think it’s working i can see now the address :)…Thank you so much