admin管理员组

文章数量:1397102

I am newbie in Maps API, i am trying get the distance (in Km) from 2 adress. What is wrong with this code?

var mygc = new google.maps.Geocoder();

var locationOrigem;
var locationDestino;

var latOrigem  = 0;
var longOrigem = 0;

var latDestino  = 0;
var longDestino = 0;


mygc.geocode({'address' : 'Presidente Vargas 897, Centro RJ'}, function(results, status){

    locationOrigem = results[0].geometry.location;
    latOrigem   = results[0].geometry.location.lat();
    longOrigem  = results[0].geometry.location.lng();

});

mygc.geocode({'address' : 'Abelardo Bueno 3000, Barra da Tijuca RJ'}, function(results, status){

    locationDestino = results[0].geometry.location;
    latDestino  = results[0].geometry.location.lat();
    longDestino = results[0].geometry.location.lng();

});

alert(google.maps.geometry.sphericalputeDistanceBetween(locationOrigem, locationDestino));

By a unknown reason, var locationOrigem and locationDestino are "undefined" outside of anonymous functions ??? Why???

I am newbie in Maps API, i am trying get the distance (in Km) from 2 adress. What is wrong with this code?

var mygc = new google.maps.Geocoder();

var locationOrigem;
var locationDestino;

var latOrigem  = 0;
var longOrigem = 0;

var latDestino  = 0;
var longDestino = 0;


mygc.geocode({'address' : 'Presidente Vargas 897, Centro RJ'}, function(results, status){

    locationOrigem = results[0].geometry.location;
    latOrigem   = results[0].geometry.location.lat();
    longOrigem  = results[0].geometry.location.lng();

});

mygc.geocode({'address' : 'Abelardo Bueno 3000, Barra da Tijuca RJ'}, function(results, status){

    locationDestino = results[0].geometry.location;
    latDestino  = results[0].geometry.location.lat();
    longDestino = results[0].geometry.location.lng();

});

alert(google.maps.geometry.spherical.puteDistanceBetween(locationOrigem, locationDestino));

By a unknown reason, var locationOrigem and locationDestino are "undefined" outside of anonymous functions ??? Why???

Share Improve this question edited Jul 19, 2011 at 22:12 celsowm asked Jul 19, 2011 at 21:00 celsowmcelsowm 46410 gold badges41 silver badges64 bronze badges 1
  • See my updated post. Does it work for you now? – Ruslan Polutsygan Commented Jul 20, 2011 at 12:55
Add a ment  | 

1 Answer 1

Reset to default 4

Have you included this script?

<script type="text/javascript" src="http://maps.google./maps/api/js?libraries=geometry&sensor=false&language=en"></script>

What happen when you run your code? Are you sure that your addresses were geocoded correctly? Are you sure that problem is in puteDistanceBetween. Try to dump all variables.

UPDATE:

Requests to the Geocoder are executed asynchronously. So request for distance calculation were run before you had got geocoded data. That is why your variables were undefined. You need to use callbacks to ensure that request is finished. Try this code

var mygc = new google.maps.Geocoder();
var locationOrigem;
var locationDestino;
var latOrigem  = 0;
var longOrigem = 0;
var latDestino  = 0;
var longDestino = 0;

mygc.geocode({'address' : 'Presidente Vargas 897, Centro RJ'}, function(results, status){
    locationOrigem = results[0].geometry.location;
    latOrigem   = results[0].geometry.location.lat();
    longOrigem  = results[0].geometry.location.lng();
    mygc.geocode({'address' : 'Abelardo Bueno 3000, Barra da Tijuca RJ'}, function(results, status){
        locationDestino = results[0].geometry.location;
        latDestino  = results[0].geometry.location.lat();
        longDestino = results[0].geometry.location.lng();
        console.log(locationOrigem);
        console.log(locationDestino);
        console.log(google.maps.geometry.spherical.puteDistanceBetween(locationOrigem, locationDestino));
    });
});

Also you need to check status of response. I haven't include because it is not the subject.

本文标签: javascriptHow to use computeDistanceBetween() correctlyStack Overflow