-2

As rookie using the Google JavaScript API.. I have to ask it!

I tried find some information about it but I didn't... I know we can create maps by long/lat and many many kind of types but, I need to create a Map by a location like:

I want to point for exemple: Barcelona, Passeig de gracia, 32

Its viable to create a map using and pointing this location as the center? Cecause I have not the ZIP code... or I really need it to consult to the API?

really thanks!

duncan
  • 31,401
  • 13
  • 78
  • 99

2 Answers2

1

Calling this url with address paramater https://maps.googleapis.com/maps/api/geocode/json?address=Barcelona,+Passeig+de+gracia,+32&key=YOUR_GOOGLE_API_KEY obtains that json

{
"results" : [
{
  "address_components" : [
    {
      "long_name" : "32",
      "short_name" : "32",
      "types" : [ "street_number" ]
    },
    {
      "long_name" : "Passeig de Gràcia",
      "short_name" : "Passeig de Gràcia",
      "types" : [ "route" ]
    },
    {
      "long_name" : "Barcelona",
      "short_name" : "Barcelona",
      "types" : [ "locality", "political" ]
    },
    {
      "long_name" : "Barcelona",
      "short_name" : "Barcelona",
      "types" : [ "administrative_area_level_2", "political" ]
    },
    {
      "long_name" : "Catalunya",
      "short_name" : "CT",
      "types" : [ "administrative_area_level_1", "political" ]
    },
    {
      "long_name" : "España",
      "short_name" : "ES",
      "types" : [ "country", "political" ]
    },
    {
      "long_name" : "08007",
      "short_name" : "08007",
      "types" : [ "postal_code" ]
    }
  ],
  "formatted_address" : "Passeig de Gràcia, 32, 08007 Barcelona, Barcelona, España",
  "geometry" : {
    "location" : {
      "lat" : 41.3907821,
      "lng" : 2.1672485
    },
    "location_type" : "ROOFTOP",
    "viewport" : {
      "northeast" : {
        "lat" : 41.39213108029149,
        "lng" : 2.168597480291502
      },
      "southwest" : {
        "lat" : 41.38943311970849,
        "lng" : 2.165899519708498
      }
    }
  },
  "place_id" : "ChIJlyIRgPKipBIRd60v8v6Vc_Y",
  "types" : [ "street_address" ]
}

], "status" : "OK" }

Calling this function and set lat and lng with new values you can create a map component centered in this location.

function initMap() {
    // Create a map object and specify the DOM element for display.
    var map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: -34.397, lng: 150.644},
        scrollwheel: false,
        zoom: 8
    });
}

in the html file

<div id="map"></div>
Borja Tur
  • 779
  • 1
  • 6
  • 20
1

2 solutions:

1: Create a temporary (invisible) map, make a search for "Barcelona, Passeig de gracia, 32" and use returned lat/lng to create and init a new map.

<div id="tmpMap" style="display: none; width: 0; height: 0;"></div>
<div id="realMap"></div>

<script>
var map = new google.maps.Map(document.getElementById('tmpMap'), {
  center: {lat: 0, lng: 0},
  zoom: 15
});
var geocoder = new google.maps.Geocoder();
geocoder.geocode({address: 'Barcelona, Passeig de gracia, 32'}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    latlng = results[0].geometry.location;
    var map = new google.maps.Map(document.getElementById('realMap'), {
      center: results[0].geometry.location,
      zoom: 15
    });
  } else {
    alert('Geocode was not successful for the following reason: ' + status);
  }
});
</script>

2: Create a map without initial place, make a search for "Barcelona, Passeig de gracia, 32" and show it on the map.

<div id="map" style="display: none;"></div>

<script>
var map = new google.maps.Map(document.getElementById('map'), {
  center: {lat: 0, lng: 0},
  zoom: 15
});
var geocoder = new google.maps.Geocoder();
geocoder.geocode({address: 'Barcelona, Passeig de gracia, 32'}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    map.setCenter(results[0].geometry.location);
  } else {
    alert('Geocode was not successful for the following reason: ' + status);
  }
});
</script>
cdm
  • 1,360
  • 11
  • 18