Slotting in Google's geocoder in place of Nominatim, using jquery

264 views
Skip to first unread message

wboykinm

unread,
Jul 28, 2011, 5:43:50 PM7/28/11
to google-maps-ap...@googlegroups.com
Hello good people of googlemaplandia . . .

I've got a fully-functional map object based in OpenLayers to which I added a geocoder calling to the nominatim service. A few days of "address not found" later I've decided to plug in Google's service instead, but I'm running into problems with the ajax components.

I previously had a POST request running to this url: var geoCodeURL = "http://nominatim.openstreetmap.org/search?format=json&q=";

I thought it was simply a matter of switching it to a GET request at this url: var geoCodeURL = "http://maps.googleapis.com/maps/api/geocode/json?address=";

But that sucker is not working with any variations on the /json?etc. part of the url. My geocoder function is below (and here: http://pastebin.com/N4fmrLim)- does anyone know what I need to tweak in order to get back a useable response (Lat, Lng)? Thanks for your help, in any case.


//replacing this:

//with this:

var zoomToPlace = function() {
   var address = document.getElementsByName("geocoder")[0].value;
   if (!address)
      alert("Please type an address or location!");
   else {
       $.ajax({
              type: "GET",
              dataType: 'json',
              url: geoCodeURL + encodeURI(address),
              error: function(jqXHR, textStatus, errorThrown){
                alert('error: ' + textStatus + ' ' + errorThrown);
              },
              success: function (response, textStatus, XMLHttpRequest) {
                  //console.log(response);
                  if (!response.length) {
                     alert('geocoder did not return any records');
                  } else {
                      // zoom to first returned
                      var lng = response[0].lng;
                      var lat = response[0].lat;
                      {{ module }}.map.setCenter(
                         new OpenLayers.LonLat( lng, lat).transform(
                            new OpenLayers.Projection("EPSG:4326"),
                            {{ module }}.map.getProjectionObject()
                         ), 13);
                  }
              }
        });
  };
}

// Then down in the HTML . . .

<p>Zoom to Address:
<input id="{{ id }}_geocoder" name="geocoder" value="">
<button type="button" onclick="zoomToPlace()" >Go!</button>
</p>


Barry Hunter

unread,
Jul 29, 2011, 5:53:10 AM7/29/11
to google-maps-ap...@googlegroups.com
JSON/AJAX calls dont work cross domain. Its possible/probable that
Jquery automatically converts it a JSONP call.

The Google Maps Webservices does NOT support client created JSONP.

If you want to use the API in the browser you must use the Javascript
Maps API - which provides functions to do it.

... which isnt a big stretch, because to use the Geocoder you must be
using a Google Map anyway.


(So it doesnt sound like your use case qualifies for using the Google
Maps Geocoder)

> --
> You received this message because you are subscribed to the Google Groups
> "Google Maps API Web Services" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-maps-api-web-services/-/s-9K5FjZKWsJ.
> To post to this group, send email to
> google-maps-ap...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-maps-api-web-...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-maps-api-web-services?hl=en.
>

wboykinm

unread,
Jul 29, 2011, 9:46:04 AM7/29/11
to google-maps-ap...@googlegroups.com
Ah, the mysteries of JSON and JSONP . . .

Thanks for the heads-up. I realized there would be some headaches implementing this across platforms; it's definitely easy to slot this into a GMaps object. 

But as far as the terms of use are concerned, I had thought my use of google base layers (hybrid & terrain) in the OpenLayers object made it legit. Is that not the case?

Thanks for your help, in any case. I appreciate it.
Message has been deleted

Barry Hunter

unread,
Jul 29, 2011, 10:17:07 AM7/29/11
to google-maps-ap...@googlegroups.com
I think it depends on how loading those tiles. Directly accessing the
tiles via openlayers is not permitted. Openlayers just wrapping around
the Maps API (so even though you interface with OL - it acully just
works as a connector to a GMaps API implementation) would be OK.

On Fri, Jul 29, 2011 at 2:46 PM, wboykinm <bill.boy...@gmail.com> wrote:
> Ah, the mysteries of JSON and JSONP . . .
> Thanks for the heads-up. I realized there would be some headaches
> implementing this across platforms; it's definitely easy to slot this into a
> GMaps object.
> But as far as the terms of use are concerned, I had thought my use of google
> base layers (hybrid & terrain) in the OpenLayers object made it legit. Is
> that not the case?

> Thanks for your help, in any case. I appreciate it.


>
> --
> You received this message because you are subscribed to the Google Groups
> "Google Maps API Web Services" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/google-maps-api-web-services/-/n2ViYpFXRIcJ.

wboykinm

unread,
Jul 30, 2011, 10:46:52 AM7/30/11
to google-maps-ap...@googlegroups.com
I apologize for continuing the thread, but I'm not sure what the boundary is between the "wrapper" and "accessor" models of OpenLayers. I see that some folks discussed this previously (http://lists.osgeo.org/pipermail/openlayers-users/2009-June/012209.html), and seemed to define the google API as dominant. i.e. OpenLayers - once using the google API to pull in base layers - is nothing more than javascript built around the google API. Does that sound right from a legal perspective?


The reason I don't switch over to the google maps interface completely is that the OL feature editing functionality is more thoroughly-developed (and already built to play nicely with django). Otherwise all the tools I need are sitting on the google side. A bit of a dilemma . . .

But thanks again for your perspective.


Reply all
Reply to author
Forward
0 new messages