简体   繁体   中英

Using reverse geocode API to return JSON

I am trying to use the reverse geocode API from geocodefarm.com to get an address using latitude and longitude. When I run this, i get the alert "error", but the geocodefarm status thing says I've used one of my queries... So I know it's going to the url, I'm just not sure exactly how I'm supposed to be getting the JSON and parsing it. it's failing on the getLocation(). any help would be great!

<td><button onclick="getLocation();return false;" runat="server"></button></td>

Script

    function getLocation() {
        jQuery.support.cors = true;
        $.ajax({
            url: 'http://www.geocodefarm.com/api/reverse/json/*apiKey*/45.2040305/-93.3995728/',
            type: 'GET',
            dataType: 'json',
            async: false,
            success: function (data) {
                WriteResponse(data);
            },
            error: function (error) {
                alert("Error");
            }
        });
    }

function WriteResponse(location) {
        if (location != null) {
            $("#txtAddZipCode").html = location.address
        }
    }

this is how the site says it should be returned in JSON form

{

    },
    "STATUS": {
        "access": "KEY_VALID, ACCESS_GRANTED",
        "status": "SUCCESS"
    },
           "PROVIDER": {
        "provider": "LOCAL_FARM",
        "import": "ALREADY_STORED"
    },
    "ADDRESS": {
        "address": "522-534 West Main Street, Anoka, MN 55303, USA",
        "accuracy": "GOOD ACCURACY"
    },
    "COORDINATES": {
        "latitude": "45.2040305",
        "longitude": "-93.3995728"
    },
    "STATISTICS": {
        "load_time": "0.52",
        "https_ssl": "DISABLED, INSECURE"
    }
}

}

You have a few issues, firstly you can't do a synchronous ajax call cross-domain, it's not possible so there's no reason to add async : false , it's ignored.

Secondly, this is the wrong syntax

$("#txtAddZipCode").html = location.address

it should be

$("#txtAddZipCode").html(location.address);

And finally, it seems the URL you're using doesn't support CORS, I get No 'Access-Control-Allow-Origin' error when running it, but I don't have an API key, so that could be what's causing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM