首页 文章

基于php中的地址获取错误的位置纬度和经度

提问于
浏览
1

使用谷歌 Map 查找基于街道,城市,州,邮政编码的位置经度和纬度 . 我正在从谷歌 Map 中获取地址并尝试查找纬度和经度 . 但我的代码总是给出错误的位置 .

码:-

<?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;
}

1 回答

  • 1

    请尝试更换这些线路

    $address = $street . ', &nbsp;' . $city . ', &nbsp;' . $state. ', &nbsp;' . $zip; // Google HQ
    $prepAddr = str_replace(' ','+',$address);
    

    更换

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

相关问题