Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中根据地址获取错误的位置纬度和经度_Php_Google Maps - Fatal编程技术网

在php中根据地址获取错误的位置纬度和经度

在php中根据地址获取错误的位置纬度和经度,php,google-maps,Php,Google Maps,使用谷歌地图查找基于街道、城市、州、邮编的位置纬度和经度。我正在从谷歌地图上获取地址,并试图找到纬度和经度。但我的代码总是给出错误的位置 代码:- <?php $con = mysql_connect("localhost","location","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } $street =$_REQUEST[street]; $city =$_REQUEST[

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

代码:-

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

你能举一个例子说明你输入并输入到数据库中的内容,以及谷歌对电话的回应吗?@Jonathon examle,比如:-street->Synergy Web Tech Pvt.Ltd.701,Goyal贸易中心Shantivan Complex Colony,city->Borivali East Mumbai,state->Maharashtra zip->400066。。此地址取自谷歌地图。如果您对字符串进行URL编码,则不应在两者之间使用加号,而应使用空格