簡體   English   中英

根據php中的地址獲取錯誤的位置經度和緯度

[英]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 . ', &nbsp;' . $city . ', &nbsp;' . $state. ', &nbsp;' . $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 . ', &nbsp;' . $city . ', &nbsp;' . $state. ', &nbsp;' . $zip; // Google HQ
$prepAddr = str_replace(' ','+',$address);

更換

$address = $street . '+' . $city . '+' . $state. '+' . $zip; 
$prepAddr = urlencode($address);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM