2

I've taken code that I had written a while ago for another web application and used it for another. However, for no obvious reason, it's not retrieving results:

Geocode was not successful for the following reason: ZERO_RESULTS

I have the following code, which — as implied — is functioning with no problems elsewhere:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script type="text/javascript">
    function googleMaps (lat, lng, zoom) {
        geocoder = new google.maps.Geocoder();
        var myLatlng = new google.maps.LatLng(lat, lng);
        var myOptions = {
            zoom: zoom,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.TERRAIN
        }
        map = new google.maps.Map(document.getElementById("map-locations"), myOptions);
        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map
        });
        google.maps.event.addListener(map, "center_changed", function(){
            document.getElementById("latitude").value = map.getCenter().lat();
            document.getElementById("longitude").value = map.getCenter().lng();
            marker.setPosition(map.getCenter());
            document.getElementById("zoom").value = map.getZoom();
        });
        google.maps.event.addListener(map, "zoom_changed", function(){
            document.getElementById("zoom").value = map.getZoom();
        });
    }
    $(document).ready(function() {
        googleMaps(52.3555, -1.1743, 6);
    });
</script>
<script>
    $(document).ready(function(){
        $("#address").change(function(){
            geocoder.geocode({
                "address": $(this).attr("value")
            }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    map.setZoom(14);
                    map.setCenter(results[0].geometry.location);
                } else {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
        });
    })
</script>

I also have the "canvas" and corresponding address field, which are identical to the functioning version elsewhere.

As an aside, I went through the Google API procedure to create an API key, which I appear not to have had to do with the earlier version. But with or without the API key, it won't function.

So I'm wondering if this process has somehow broken things.

Wayne Smallman
  • 1,690
  • 11
  • 34
  • 56
  • What is the value of `$(this).attr("value")` when it returns ZERO_RESULTS? Is it a valid postal address? – geocodezip Feb 11 '14 at 11:06
  • Any address or postal code I've tried that retrieves results with the other web application (which is to say every query I've tried) fails with the above code. – Wayne Smallman Feb 11 '14 at 11:11
  • Deleted previous answer as it didn't make sense (I hadn't realized the geocoder was not being called as a result of those coords, should learn to read). Try alerting $(this).attr("value") as mentioned above and check the address/coords are actually being passed to the call in a valid manner. – ChrisSwires Feb 11 '14 at 11:14
  • I placed the alert just after $("#address").change(function()... and it's not passing a value, but I can't see why it won't. A mystery. – Wayne Smallman Feb 11 '14 at 11:31

1 Answers1

6

You have to change your code to

...
geocoder.geocode({
                "address": $("#address").val()
            }, function(results, status) {
...

to use $("#address").val() instead of $("#address").attr("value").

Update: See also SO question jQuery $obj.val() vs $obj.attr(“value”)

Community
  • 1
  • 1
Anto Jurković
  • 11,188
  • 2
  • 29
  • 42