我正在尝试将新位置输入数据库,并希望正确输入lat和long坐标以进行映射。我已经研究了Googles帮助论坛,并提出了这个代码。
目前,我有一个备份表,如果通过我的谷歌地图调用地址不成功,则有一个通用的lat和长代码。这是有效的,但如果可能的话我想要精确的坐标,谷歌地图调用什么都不做。这是我的代码。 (我已经取出了我的谷歌aps键并取代了“我的钥匙”
if($facility_enter){
//GEOCODE
define("MAPS_HOST", "maps.google.com");
define("KEY", "my key");
// Do API find, returning address and id
// Initialize delay in geocode speed
$delay = 0;
$base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY;
//Get Address
$full_address = $_POST['fac_add'] . ' ' . $_POST['fac_city'] . ' ' .
$_POST['fac_state'] . ' ' .
$_POST['fac_zip'];
$prepAddr = str_replace(' ','+',$full_address);
$request_url = $base_url . "&q=" . urlencode($prepAddr);
$xml = simplexml_load_file($request_url);
$status = $xml->Response->Status->code;
//if successful
if (strcmp($status, "200") == 0) {
$coordinates = $xml->Response->Placemark->Point->coordinates;
$coordinatesSplit = split(",", $coordinates);
$lat = $coordinatesSplit[1];
$lng = $coordinatesSplit[0];
}
//otherwise just pull the generic zip code coords from the database
else{
$zip_sql = 'SELECT lat,lng FROM blahblah.zips WHERE zip = "' . $_POST['fac_zip'] .
'"';
$zip_res = mysql_query($zip_sql,$my_db);
$coords = mysql_fetch_array($zip_res);
$lat = $coords['lat'];
$lng = $coords['lng'];
}