Question

hello I am trying to get earthquake info when user types city into map. Right now I make bounding box by hardcoding (required for geonames earthquake api). I would like to make bounding box the size of the city entered. I see in geocoder api v3 to do this:

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

problem- this will not let me get north east south west which geonames needs. anyone know how?

my current code

var address = document.getElementById("address").value;
            geocoder.geocode( { 'address': address}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                  // Calculate bounding box based on the given address
                  var north = results[0].geometry.location.lat() + .5;
                  var south = results[0].geometry.location.lat() - .5;
                  var east = results[0].geometry.location.lng() + .5;
                  var west = results[0].geometry.location.lng() - .5;

                  var earthquake = 'http://api.geonames.org/earthquakesJSON?north=' + north + '&south=' + south + '&east=' + east + '&west=' + west + '&username=*****';

The +5 -5 is what extends the numbers in the coordinates to create north south....etc

Was it helpful?

Solution

You want to use the result[0].geometry.viewport which is a google.maps.LatLngBounds object

var north = results[0].geometry.viewport.getNorthEast().lat();
var south = results[0].geometry.viewport.getSouthWest().lat();
var east   = results[0].geometry.viewport.getNorthEast().lng();
var west   = results[0].geometry.viewport.getSouthWest().lng();

fixed fiddle

fiddle with return message from geonames

fiddle with hard-coded data that shows markers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top