使用Google地图根据街道,城市,州,邮政编码查找位置经度和纬度。我从Google地图中获取地址并尝试查找纬度和经度。但是我的代码总是给出错误的位置。
代码: -
<?php
$con = mysql_connect("localhost","location","password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$street =$_REQUEST[street];
$city =$_REQUEST[city];
$state =$_REQUEST[state];
$zip =$_REQUEST[zip];
$result=null;
mysql_select_db("map", $con);
$address = $street . ', ' . $city . ', ' . $state. ', ' . $zip; // Google HQ
$prepAddr = str_replace(' ','+',$address);
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
if($lat==0.0000 && $long==0.0000){
$result="ERROR";
}else{
$sql="INSERT INTO markers (name, address, lat, lng) VALUES ('".$street."', '".$city.", " .$state ."', '".$lat."', '".$long."')";
$result="SUCCESS";
}
if (!mysql_query($sql,$con)) {
echo $result;
} else {
echo $result;
}
答案 0 :(得分:1)
请尝试更换这些行
$address = $street . ', ' . $city . ', ' . $state. ', ' . $zip; // Google HQ
$prepAddr = str_replace(' ','+',$address);
替换
$address = $street . '+' . $city . '+' . $state. '+' . $zip;
$prepAddr = urlencode($address);