您能告诉我有关“如何使用php在Google地图中保存位置”的示例吗? 我正在创建一个属性网站,当管理员添加新属性时,他们需要在Google地图上保存位置。 请帮忙。
提前致谢
答案 0 :(得分:1)
第一步是将您的地址设置为url编码状态,以便在Google地理编码API中使用。这是一个示例:
$Address = '';
$format = 'xml'; // Set this to xml or json
if(!empty($row['Address_1'])) $Address .= ($row['Address_1']);
if(!empty($row['Address_2'])) $Address .= ($row['Address_2']);
if(!empty($Address)) $Address .= ",+";
if(!empty($row['City'])) $Address .= ($row['City']);
if(!empty($Address)) $Address .= ",+";
if(!empty($row['State'])) $Address .= ($row['State']);
$Address = str_replace(' ', '+', $Address);
$url = 'http://maps.googleapis.com/maps/api/geocode/'.$format.'?address='.htmlentities($Address).'&sensor=false';
接下来,您将要获取结果并存储它:
if($format == 'xml')
{
$document = new DOMDocument('1.0');
$document->load($url);
// Check Status
$RequestStatus = $document->getElementsByTagName("status")->item(0)->firstChild->nodeValue;
if($RequestStatus !== 'OK')
{
echo("There was an error with the request. The status returned was: ".$RequestStatus);
}
// Grab the Geometry->Location Node
$GeoLoc = $document->getElementsByTagName("location")->item(0);
foreach ($GeoLoc->childNodes as $node)
{
if($node->nodeName == 'lat') echo "Latitude: " . $node->nodeValue . "<br />";
if($node->nodeName == 'lng') echo "Longitude: " . $node->nodeValue . "<br />";
}
}
else if($format == 'json')
{
$json = file_get_contents($url,0,null,null);
$json_output = json_decode($json, true);
if($json_output['status'] != 'OK')
{
echo("There was an error with the request. The status returned was: ".$json_output['status']);
// Set your lat/long data to 0, null, etc
}
foreach ($json_output['results'] as $result)
{
echo "Latitude: " . $result['geometry']['location']['lat'] . "<br />";
echo "Longitude: " . $result['geometry']['location']['lng'] . "<br />";
// Store your lat/long data in your database
}
}
答案 1 :(得分:0)
答案 2 :(得分:0)
我通过以下方式解决它(虽然它适用于GM2,它适用于Joomla模块):
1.Get location name from db (which stored by admin)
2.Get the latitude and longitude of the place using getLatLng.
3.Use the latitude and longitude to plot the place in map.