wboykinm

migrating from nominatim

Jul 28th, 2011
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //replacing this:
  2. //var geoCodeURL = "http://nominatim.openstreetmap.org/search?format=json&q=";
  3.  
  4. //with this:
  5. var geoCodeURL = "http://maps.googleapis.com/maps/api/geocode/json?address=";
  6.  
  7. var zoomToPlace = function() {
  8.    var address = document.getElementsByName("geocoder")[0].value;
  9.    if (!address)
  10.       alert("Please type an address or location!");
  11.    else {
  12.        $.ajax({
  13.               type: "GET",
  14.               dataType: 'json',
  15.               url: geoCodeURL + encodeURI(address),
  16.               error: function(jqXHR, textStatus, errorThrown){
  17.                 alert('error: ' + textStatus + ' ' + errorThrown);
  18.               },
  19.               success: function (response, textStatus, XMLHttpRequest) {
  20.                   //console.log(response);
  21.                   if (!response.length) {
  22.                      alert('geocoder did not return any records');
  23.                   } else {
  24.                       // zoom to first returned
  25.                       var lng = response[0].lng;
  26.                       var lat = response[0].lat;
  27.                       {{ module }}.map.setCenter(
  28.                          new OpenLayers.LonLat( lng, lat).transform(
  29.                             new OpenLayers.Projection("EPSG:4326"),
  30.                             {{ module }}.map.getProjectionObject()
  31.                          ), 13);
  32.                   }
  33.               }
  34.         });
  35.   };
  36. }
  37.  
  38. // Then down in the HTML . . .
  39.  
  40. <p>Zoom to Address:
  41. <input id="{{ id }}_geocoder" name="geocoder" value="">
  42. <button type="button" onclick="zoomToPlace()" >Go!</button>
  43. </p>
Advertisement
Add Comment
Please, Sign In to add comment