0

I'm using Google maps api, and I got a problem when I'm trying to get markers position.

I got two text fields as an address input, and i show the results with markers.

When I want to get the markers position(by getPosition() function), for using new google.maps.LatLngBounds(), the markers position is correct on the map, but the getPosition() function, gives me a wrong answer, only on the second time I search for the address the getPosition, is updated for the first address search.

It's like it has a dealy and when I'm using getPosition(), the position is not updated yet.

Anyone have any idea why?

Thanx

This is part of my code. If I'll use JSON request for getting the address location, will it work better?

function GetAddress(add , map , pointtype) {

    var country = 'france';        

    var address =  add + ', ' + country; 

    geocoder = new google.maps.Geocoder();
      if(pointtype == 0){
        geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {                

          origmarker.setPosition(results[0].geometry.location) ;  
          origmarker.setTitle(add);


        } 
       });
      }
      else{
          geocoder.geocode( { 'address': address}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {              

            desmarker.setPosition(results[0].geometry.location) ;  
            desmarker.setTitle(add);


        } 
       });
      }
}

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

bounds.extend(origmarker.getPosition());
bounds.extend(desmarker.getPosition());

map.fitBounds(bounds);
Gryu
  • 2,102
  • 2
  • 16
  • 29
guy
  • 203
  • 2
  • 3
  • 13
  • You have not really provided enough information. You should provide some code detailing what you are doing (see the [FAQ](http://stackoverflow.com/faq)). – geocodezip Dec 05 '12 at 13:28
  • Possible duplicate of [Calculate distance between two points in google maps V3](http://stackoverflow.com/questions/1502590/calculate-distance-between-two-points-in-google-maps-v3) – Anis D Dec 12 '16 at 14:42

1 Answers1

0

Sounds like you are using the geocoder to place the markers on the map. The geocoder is asynchronous, you can't use the results until the callback routine runs. Sounds like you are trying to use the position of the marker before the callback runs (so you get the value from the last call).

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • Thank you.you are right i'm using geocoder. is there a way to change it from asynchronous, or use a way for knowing when the callback returns? – guy Dec 05 '12 at 13:45
  • No, you can't make it synchronous (that defeats the point). You know the data is available when the callback runs. Please update your question with the code you are using if you want more detailed answers. – geocodezip Dec 05 '12 at 13:47