views:

634

answers:

3

Hi,

I have a town name, gmap modules and plg. i want to focus desired location in gmap by giving town name as input instead of longitude and latitude. Is there any way to do this in Joomla 1.5.x.

Thanks in advance
naveen

+3  A: 

You will need a GeoCoder to get the location based on the city name. Don't know nothing about Joomla, but here is and example using JavaScript.

The HTML

<div id='myMap' style="position:relative; width:740px; height:400px;"></div>

The script

You have to complete with the Google API Key and the city you want

<script src="http://maps.google.com/maps?file=api&amp;amp;v=2.x&amp;amp;key=XXXXX"&gt;
</script>

<script type="text/javascript">
    function addLoadEvent(func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function')
        { window.onload = func; }
        else {
            window.onload = function()
            { oldonload(); func(); }
        }
    }

    var map = null;
    var geocoder = null;

    addLoadEvent(initialize);

    function initialize() {
        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("myMap"));
            map.setCenter(new GLatLng(-12.08, -53.08), 4);
            map.addControl(new GSmallMapControl());
            map.addControl(new GOverviewMapControl());
            geocoder = new GClientGeocoder();
            showAddress('YourCity, YourCountry');
        }
    }

    function showAddress(address) {
        if (geocoder) {
            geocoder.getLatLng(
          address,
          function(point) {
              if (!point) {
                  $("#myMap").hide();
              } else {
                  map.setCenter(point, 8);
                  var marker = new GMarker(point);
                  map.addOverlay(marker);
                  marker.openInfoWindowHtml(address);
              }
          }
        );
        }
    }
</script>
Eduardo Molteni
You don't need the addLoadEvent custom funciton. To simplify code, use:GEvent.addDomListener( window, 'load', initialize );GEvent namespace is available with the Google Maps API JavsScript.
Salman A
A: 

I was looking for a way to do something similar myself. I tried using the Google Maps API to search for the location, but it was sometimes wrong. Eventually I ended up querying the Wikipedia page relating to the town and parsing the coordinates, then using those. Probably not the solution you were looking for, but it works for me. I would advise trying to search the Maps API first as Eduardo details, but if that doesn't work, this might.

Samir Talwar
A: 

You can make a HTTP call from Javascript to Google's geocoding service, passing it a place name/postcode, and getting lat/long back.

I do this using the CSV option as I am calling it from ASP, but I think you can get back XML or JSON too.

Fire off this URL if you have a Google maps API key: http://maps.google.com/maps/geo?gl=uk&amp;q=guildford,+uk&amp;output=csv&amp;key=XXXXXXXXXXX

First parameter back is response code. 200 means success. Then you get latitude and longitude, comma seperated.

Magnus Smith