views:

14

answers:

1

I'm showing a Google Map based on a street address supplied from an outside source.

My JavaScript is very simple, and I build the link like so

var googleMap = 'http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=' + address + '&ie=UTF8';

This is quick and easy, and works. But some of the incoming addresses aren't exactly addresses (they include some stuff which shouldn't be there).

When Google Maps can't make heads or tails of the address, it defaults to showing a map of USA. Unfortunately, I'm in Australia.

Is there any way using this lazy method to know if Google Maps couldn't match the address, or can I show Australia by default?

... or do I need to look at the API?

+1  A: 

What about using maps.google.com.au? Note that the parameters might be a bit different.

http://maps.google.com.au/maps?q=Melbourne

http://maps.google.com.au/maps?q=Something-Garbage-That-Does-Not-Exist

You could also use the Maps API, as you suggested. Something like that would be very easy to do:

<!DOCTYPE html>
<html> 
<head> 
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>Google Maps Geocoding Default Location</title> 
   <script src="http://maps.google.com/maps/api/js?sensor=false" 
           type="text/javascript"></script> 
</head> 
<body> 
   <div id="map" style="width: 400px; height: 300px;"></div> 

   <script type="text/javascript"> 

   var address = 'Melbourne, Australia';

   var map = new google.maps.Map(document.getElementById('map'), { 
       mapTypeId: google.maps.MapTypeId.TERRAIN,
       center: new google.maps.LatLng(-25.50, 135.00),
       zoom: 3
   });

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

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

   </script> 
</body> 
</html>

Screenshot:

Google Maps Geocoding Default Location

And another screenshot if you were to use address = 'Something Garbage That Does Not Exist':

Google Maps Geocoding Default Location

Daniel Vassallo
Thanks, that worked! :)
alex