我使用 Google 地图获取有关位置的任何信息:http://code.google.com/apis/maps/index.html
它可以返回 XML 或 JSON 格式的数据,以便您轻松解析和保存数据。
这是华盛顿特区的示例链接:http://maps.google.com/maps/geo?output=xml&oe=utf8&sensor=false&hl=en&q=washington%20dc
它将返回此 XML 数据:
<?xml version="1.0" encoding="UTF-8" ?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Response>
<name>washington dc</name>
<Status>
<code>200</code>
<request>geocode</request>
</Status>
<Placemark id="p1">
<address>Washington, DC, USA</address>
<AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
<Country>
<CountryNameCode>US</CountryNameCode>
<CountryName>USA</CountryName>
<AdministrativeArea>
<AdministrativeAreaName>DC</AdministrativeAreaName>
<SubAdministrativeArea>
<SubAdministrativeAreaName>District of Columbia</SubAdministrativeAreaName>
<Locality>
<LocalityName>Washington</LocalityName>
</Locality>
</SubAdministrativeArea>
</AdministrativeArea>
</Country>
</AddressDetails>
<ExtendedData>
<LatLonBox north="38.9645516" south="38.8256040" east="-76.9083064" west="-77.1644252" />
</ExtendedData>
<Point>
<coordinates>-77.0363658,38.8951118,0</coordinates>
</Point>
</Placemark>
</Response>
</kml>
然后您可以使用 SimpleXML 或 DOMDocument 之类的函数来解析数据。如果您只想回显标签中支持的坐标,请使用以下代码:
$xml = simplexml_load_string(file_get_contents('http://maps.google.com/maps/geo?output=json&oe=utf8&sensor=false&hl=de&q='.urlencode($address)));
echo (string)$xml->Placemark->Point->coordinates;
如果您想要单个坐标,请使用标签中的数据,例如:
$coordinates = explode(',', $xml->Placemark->Point->coordinates);
echo $coordinates[0];
echo $coordinates[1];
我希望这就是您要搜索的内容。