FabioEnne
FabioEnne

Reputation: 742

Why Google Places API is not executing my request?

I'm trying to create a simple application where I insert an area of search, what to search and a radius of search. I can't understand why it's only working properly the second time I perform my query search, I know the problem is probably in my codeAddress() function:

function codeAddress(){
                var address = document.getElementById('Zona').value;
                geocoder.geocode( { 'address': address}, function(results, status){
                    if (status == google.maps.GeocoderStatus.OK){
                    map.setCenter(results[0].geometry.location);
                    coordo = results[0].geometry.location;
                    }else{
                        alert('Geocode was not successful for the following reason: ' + status);
                    }
                });
            }

for some kind of reason that I don't know it skips and it doesn't go inside the geocoder.geocode( { 'address': address}, function(results, status) but instead it goes inside if it is the second time I execute the function, not the first time.

Here is the application:

Jsfiddle If you enter a city name and click on the button one time it just find the location if you hit the button another time it work as expected, it drops the pins and create the table..anyone knows why? Thanks.

Upvotes: 1

Views: 70

Answers (2)

Harsh Agrawal
Harsh Agrawal

Reputation: 11

You can follow this plunk. I have resolved all the issue.

Upvotes: 0

SergioPetrarca
SergioPetrarca

Reputation: 248

I have looked at your code and, as you said, the function codeAddress jumps the first time. I looked for the operation of geocoding service on google site developers and I saw that the function codeAddress behaves the same way as yours.

To solve your problem, I thought you might be run before geocoding and after start function searcha.

To do this I thought I'd use the jQuery change:

$ ("input"). change (function () {
        //your code here
     })

Your function should be like this:

    $(document).ready(function(){
        $("#Zona").change(function codeAddress(){
            var address = document.getElementById('Zona').value;
            geocoder = new google.maps.Geocoder();
            geocoder.geocode( { 'address': address}, function(results, status){
                if (status == google.maps.GeocoderStatus.OK){
                    coordo=(results[0].geometry.location);
                }else{
                    alert('Geocode was not successful for the following reason: ' + status);
                }
            });
        });
    });

I modified your example with my changes, you can view it here: http://jsfiddle.net/ftxgm0cn/

Upvotes: 1

Related Questions