我怎样才能知道某个城市/国家的经纬度?


How can I get the longtitute/lattitude for a given city/country?

例如,当我在http://www.infoghidromania.com/harta_romania.html网站上输入一个地址,然后按提交,它应该提取GPS坐标

该站点使用谷歌地图api。你可以使用相同的API来获得任何位置的经度/纬度,你可以在谷歌地图上找到它。

Javascript代码示例:
var geocoder =  new google.maps.Geocoder();
geocoder.geocode( { 'address': '<YOUR_SEARCH_STRING_HERE>' }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        alert("Lang/Lat : " + results[0].geometry.location.lat() + " " +results[0].geometry.location.lng()); 
    } else {
        alert('Error: ' + status);
    }
});
工作例子:

  • 示例1: http://jsfiddle.net/dovereem/kChXa/
  • 搜索框示例: http://jsfiddle.net/dovereem/kChXa/1/

您正在寻找的是所谓的"地理编码"。谷歌地图有一个API你可以使用-你可以在这里看到详细信息

一个如何使用它的快速示例(假设这是在一个名为latLong.php的文件中):

<html encoding="utf-8">
<body>
    Please enter address to look up:
    <form method="get" action="latLong.php">
        <input name="address" />
        <input type="submit" />
    </form>
<?php
    $crlf = "<br>";
    if (isset($_GET["address"])) {
        $addressString = $_GET["address"];
        $address = urlencode($addressString);
        $googleApi = 'http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false';     
        $json = file_get_contents(sprintf($googleApi, $address));   
        $resultObject = json_decode($json);
        $location = $resultObject->results[0]->geometry->location;
        $lat = $location->lat;
        $lng = $location->lng;
        echo "Requested address: ".$addressString.$crlf; 
        echo "Latitude: ".$lat.$crlf;
        echo "Longitude: ".$lng.$crlf;
    }
?>
</body>
</html>
使用jquery:
$(function(){
    $('idOfInputField').val("content");
    $('the id of the form').submit();
});