0

https://stackoverflow.com/a/6798005/2068148

The answer in the above link is answered by Michal.

After getting the results from geocoder.geocode I didn't understand why he checked if(results[1]), he could have checked if(results)...

Please help me in understanding this.

Community
  • 1
  • 1
Puneet
  • 131
  • 1
  • 1
  • 4

2 Answers2

0

As per Google developers site:

geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address }, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    map.setCenter(results[0].geometry.location);
    var marker = new google.maps.Marker({
    map: map,
    position: results[0].geometry.location
  });

So, i think it should be results[0], see more details at Geocoding Strategies

See complete example response below for formatted_address

{
  "status": "OK",
  "results": [ {
    "types": street_address,
    "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": street_number
    }, {
      "long_name": "Amphitheatre Pkwy",
      "short_name": "Amphitheatre Pkwy",
      "types": route
    }, {
      "long_name": "Mountain View",
      "short_name": "Mountain View",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "San Jose",
      "short_name": "San Jose",
      "types": [ "administrative_area_level_3", "political" ]
    }, {
      "long_name": "Santa Clara",
      "short_name": "Santa Clara",
      "types": [ "administrative_area_level_2", "political" ]
    }, {
      "long_name": "California",
      "short_name": "CA",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United States",
      "short_name": "US",
      "types": [ "country", "political" ]
    }, {
      "long_name": "94043",
      "short_name": "94043",
      "types": postal_code
    } ],
    "geometry": {
      "location": {
        "lat": 37.4220323,
        "lng": -122.0845109
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 37.4188847,
          "lng": -122.0876585
        },
        "northeast": {
          "lat": 37.4251799,
          "lng": -122.0813633
        }
      }
    }
  } ]
}

Cheers !!

Sachin Thapa
  • 3,559
  • 4
  • 24
  • 42
0

You are right. The if(results[1]) seems to be a mistake/typo because geocoder.geocode writes the results to an array (as documented here).

So, the results array has one object for each result. This can also be seen by inspecting the console.log messages when the code runs-there is a console.log(results) in Michal's code.

The if statement should be: if(results.length > 0) or even if(results[0]), but even this would be redundant since if there are no results the previous if statement: if (status == google.maps.GeocoderStatus.OK) will resolve to false (with status="ZERO_RESULTS").

So you can just remove the if(results[1]) or replaced it with if(results.length > 0) or if(results[0]).

Malt
  • 28,965
  • 9
  • 65
  • 105