简体   繁体   中英

Google Maps problem with Geocoder

The point is to point out the markers from a JSON file, but if the current json, dont have any lat and lng it should calculate it, but it dont work

//For example when
item.county = 'Orust'
//and 
item.region = 'Bohuslän'

Why is that?

if(lat == null)
            {
                geocoder.geocode({ 'address': item.county+', '+item.region}, function(result, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if(result[0])
                    {
                        latlng = result[0].geometry.location
                    }
                }
                });

Have I missed something?

First check to see if things are working as expected inside the callback. To do that, change this:

latlng = result[0].geometry.location;

To this:

console.log(result[0].geometry.location);

(You can also just add the console.log above the latlng assignment rather than changing it. Actually, that's probably better.)

If the resulting message in your JavaScript console looks like a latlng object, then the problem is likely a scope problem--in other words, latlng outside of the callback is not the same as latlng inside the callback.

If the resulting message in your JavaScript console does not look like a latlng object, then the problem is inside the callback itself. Check to see if you are getting any results at all by putting this where you put the last console.log() above:

console.log(result);

If these console.log commands never fire and nothing appears in your JavaScript console, then the status returned from the geocoder.geocoder() is not OK and you need to add some error handling in the callback to see what's going on.

Request to geocoder runs asynchronously. You cannot access to vriable which is assigned inside the callback from the outside code. Actually you could but it will be undefined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM