0

このコードを使用して、ユーザーの完全な住所情報を取得しています

function getGeo() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (a) {
            $("#geoLoc").html("Determing your location..");
            $.post("https://mysite.com/getloc.php?l=" + a.coords.latitude + "," + a.coords.longitude, function (b) {
                var c = jsonParse(b);
                geo_adres = c.results[0].formatted_address;
                latitude = a.coords.latitude;
                longitude = a.coords.longitude;
                $("#geoLoc").html(c.results[0].formatted_address);
                $("#geoLoc").show("slow")
            })
        }, function (a) {
            alert(geolocationErrorMessages[a.code])
        }, {
            enableHighAccuracy: true,
            maximumAge: 12e4
        });
        return false
    } else {
        alert("Your browser doesn't support geo-location feature...")
    }
}

編集:getloc.phpにはこのコードが含まれています(javascriptのc var)

$data = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng=". $_GET['l'] ."&sensor=false");
print $data;

実際、私が欲しいのは、ユーザーにそのような都市と国の情報を取得することだけですcity, country

c.results[0].formatted_addressそれを達成するためにこれをどのように変更する必要がありますか?

4

2 に答える 2

0

この動作するphpコードを試してみてください。これがお役に立てば幸いです:)そしてクエリが発生した場合はmewに知らせてください-

<?php

$data = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false");

$jsonnew =json_decode($data,true);

    echo "<pre>";
    print_r($jsonnew['results'][7]['address_components'][2]['long_name']);

echo "<pre>";
    print_r($jsonnew['results'][6]['address_components'][2]['long_name']);
于 2012-09-06T05:58:47.300 に答える
0

getlocPHPスクリプトは必要ありません。MapsJavascriptAPIにはGeocoderクラスが含まれています。

于 2012-09-06T07:22:40.910 に答える