[英]Getting wrong location latitude and longitude based on address in php
使用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;
}
請嘗試替換這些行
$address = $street . ', ' . $city . ', ' . $state. ', ' . $zip; // Google HQ
$prepAddr = str_replace(' ','+',$address);
更換
$address = $street . '+' . $city . '+' . $state. '+' . $zip;
$prepAddr = urlencode($address);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.