Hello what is the formulla to get longitude and latitude of any address , if i have street_name,State_name,City_name,Country_name and zip code in PHP? Thanks
你好,如果我在PHP中有street_name,State_name,City_name,Country_name和邮政编码,那么获取任何地址的经度和纬度的是什么?谢谢
8 个解决方案
#1
83
you can use bellow code for getting lat,long using php. Here are two type to get this
你可以使用波纹管代码获取lat,使用php。这是两种类型来获得这个
Type-1
1型
<?php
// Get lat and long by address
$address = $dlocation; // Google HQ
$prepAddr = str_replace(' ','+',$address);
$geocode=file_get_contents('https://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$latitude = $output->results[0]->geometry->location->lat;
$longitude = $output->results[0]->geometry->location->lng;
?>
edit - Google Maps requests must be over https
编辑 - Google地图请求必须通过https
Type-2
2型
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(50.804400, -1.147250);
var mapOptions = {
zoom: 6,
center: latlng
}
map = new google.maps.Map(document.getElementById('map-canvas12'), mapOptions);
}
function codeAddress(address,tutorname,url,distance,prise,postcode) {
var address = address;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
var infowindow = new google.maps.InfoWindow({
content: 'Tutor Name: '+tutorname+'<br>Price Guide: '+prise+'<br>Distance: '+distance+' Miles from you('+postcode+')<br> <a href="'+url+'" target="blank">View Tutor profile</a> '
});
infowindow.open(map,marker);
} /*else {
alert('Geocode was not successful for the following reason: ' + status);
}*/
});
}
google.maps.event.addDomListener(window, 'load', initialize);
window.onload = function(){
initialize();
// your code here
<?php foreach($addr as $add) {
?>
codeAddress('<?php echo $add['address']; ?>','<?php echo $add['tutorname']; ?>','<?php echo $add['url']; ?>','<?php echo $add['distance']; ?>','<?php echo $add['prise']; ?>','<?php echo substr( $postcode1,0,4); ?>');
<?php } ?>
};
</script>
<div id="map-canvas12"></div>
#2
10
<?php
$address = 'BTM 2nd Stage, Bengaluru, Karnataka 560076'; // Address
// Get JSON results from this request
$geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false');
$geo = json_decode($geo, true); // Convert the JSON to an array
if (isset($geo['status']) && ($geo['status'] == 'OK')) {
$latitude = $geo['results'][0]['geometry']['location']['lat']; // Latitude
$longitude = $geo['results'][0]['geometry']['location']['lng']; // Longitude
}
?>
#4
2
You need to access a geocoding service (i.e. from Google), there is no simple formula to transfer addresses to geo coordinates.
您需要访问地理编码服务(即来自Google),没有简单的公式可以将地址传输到地理坐标。
#5
2
You can use the Google Maps API for that. See the blog post below for more information.
您可以使用Google Maps API。有关更多信息,请参阅下面的博客文章。
http://stuff.nekhbet.ro/2008/12/12/how-to-get-coordinates-for-a-given-address-using-php.html
http://stuff.nekhbet.ro/2008/12/12/how-to-get-coordinates-for-a-given-address-using-php.html
#6
1
PHP has some nice built in functions for getting geographic location. Maybe have a look here: http://php.net/manual/en/ref.geoip.php
PHP有一些很好的内置函数来获取地理位置。也许看看这里:http://php.net/manual/en/ref.geoip.php
According to php manual, "This extension requires the GeoIP C library version 1.4.0 or higher to be installed. You can grab the latest version from » http://www.maxmind.com/app/c and compile it yourself."
根据php手册,“此扩展需要安装GeoIP C库1.4.0或更高版本。您可以从»http://www.maxmind.com/app/c获取最新版本并自行编译。”
#7
1
I came up with the following which takes account of rubbish passed in and file_get_contents failing....
我想出了以下内容,其中考虑了垃圾传入和file_get_contents失败....
function get_lonlat( $addr ) {
try {
$coordinates = @file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($addr) . '&sensor=true');
$e=json_decode($coordinates);
// call to google api failed so has ZERO_RESULTS -- i.e. rubbish address...
if ( isset($e->status)) { if ( $e->status == 'ZERO_RESULTS' ) {echo '1:'; $err_res=true; } else {echo '2:'; $err_res=false; } } else { echo '3:'; $err_res=false; }
// $coordinates is false if file_get_contents has failed so create a blank array with Longitude/Latitude.
if ( $coordinates == false || $err_res == true ) {
$a = array( 'lat'=>0,'lng'=>0);
$coordinates = new stdClass();
foreach ( $a as $key => $value)
{
$coordinates->$key = $value;
}
} else {
// call to google ok so just return longitude/latitude.
$coordinates = $e;
$coordinates = $coordinates->results[0]->geometry->location;
}
return $coordinates;
}
catch (Exception $e) {
}
then to get the cords: where $pc is the postcode or address.... $address = get_lonlat( $pc ); $l1 = $address->lat; $l2 = $address->lng;
然后得到电线:其中$ pc是邮政编码或地址.... $ address = get_lonlat($ pc); $ l1 = $ address-> lat; $ l2 = $ address-> lng;
#8
0
There is no forumula, as street names and cities are essentially handed out randomly. The address needs to be looked up in a database. Alternatively, you can look up a zip code in a database for the region that the zip code is for.
没有论坛,因为街道名称和城市基本上是随机发放的。需要在数据库中查找地址。或者,您可以在数据库中查找邮政编码所针对的区域的邮政编码。
You didn't mention a country, so I'm going to assume you just want addresses in the USA. There are numerous databases you can use, some free, some not.
你没有提到一个国家,所以我假设你只想要在美国的地址。您可以使用许多数据库,一些是免费的,一些不是。
You can also use the Google Maps API to have them look up an address in their database for you. That is probably the easiest solution, but requires your application to have a working internet connection at all times.
您还可以使用Google Maps API让他们在您的数据库中查找地址。这可能是最简单的解决方案,但要求您的应用程序始终具有可用的Internet连接。
#1
83
you can use bellow code for getting lat,long using php. Here are two type to get this
你可以使用波纹管代码获取lat,使用php。这是两种类型来获得这个
Type-1
1型
<?php
// Get lat and long by address
$address = $dlocation; // Google HQ
$prepAddr = str_replace(' ','+',$address);
$geocode=file_get_contents('https://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$latitude = $output->results[0]->geometry->location->lat;
$longitude = $output->results[0]->geometry->location->lng;
?>
edit - Google Maps requests must be over https
编辑 - Google地图请求必须通过https
Type-2
2型
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(50.804400, -1.147250);
var mapOptions = {
zoom: 6,
center: latlng
}
map = new google.maps.Map(document.getElementById('map-canvas12'), mapOptions);
}
function codeAddress(address,tutorname,url,distance,prise,postcode) {
var address = address;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
var infowindow = new google.maps.InfoWindow({
content: 'Tutor Name: '+tutorname+'<br>Price Guide: '+prise+'<br>Distance: '+distance+' Miles from you('+postcode+')<br> <a href="'+url+'" target="blank">View Tutor profile</a> '
});
infowindow.open(map,marker);
} /*else {
alert('Geocode was not successful for the following reason: ' + status);
}*/
});
}
google.maps.event.addDomListener(window, 'load', initialize);
window.onload = function(){
initialize();
// your code here
<?php foreach($addr as $add) {
?>
codeAddress('<?php echo $add['address']; ?>','<?php echo $add['tutorname']; ?>','<?php echo $add['url']; ?>','<?php echo $add['distance']; ?>','<?php echo $add['prise']; ?>','<?php echo substr( $postcode1,0,4); ?>');
<?php } ?>
};
</script>
<div id="map-canvas12"></div>
#2
10
<?php
$address = 'BTM 2nd Stage, Bengaluru, Karnataka 560076'; // Address
// Get JSON results from this request
$geo = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false');
$geo = json_decode($geo, true); // Convert the JSON to an array
if (isset($geo['status']) && ($geo['status'] == 'OK')) {
$latitude = $geo['results'][0]['geometry']['location']['lat']; // Latitude
$longitude = $geo['results'][0]['geometry']['location']['lng']; // Longitude
}
?>
#3
#4
2
You need to access a geocoding service (i.e. from Google), there is no simple formula to transfer addresses to geo coordinates.
您需要访问地理编码服务(即来自Google),没有简单的公式可以将地址传输到地理坐标。
#5
2
You can use the Google Maps API for that. See the blog post below for more information.
您可以使用Google Maps API。有关更多信息,请参阅下面的博客文章。
http://stuff.nekhbet.ro/2008/12/12/how-to-get-coordinates-for-a-given-address-using-php.html
http://stuff.nekhbet.ro/2008/12/12/how-to-get-coordinates-for-a-given-address-using-php.html
#6
1
PHP has some nice built in functions for getting geographic location. Maybe have a look here: http://php.net/manual/en/ref.geoip.php
PHP有一些很好的内置函数来获取地理位置。也许看看这里:http://php.net/manual/en/ref.geoip.php
According to php manual, "This extension requires the GeoIP C library version 1.4.0 or higher to be installed. You can grab the latest version from » http://www.maxmind.com/app/c and compile it yourself."
根据php手册,“此扩展需要安装GeoIP C库1.4.0或更高版本。您可以从»http://www.maxmind.com/app/c获取最新版本并自行编译。”
#7
1
I came up with the following which takes account of rubbish passed in and file_get_contents failing....
我想出了以下内容,其中考虑了垃圾传入和file_get_contents失败....
function get_lonlat( $addr ) {
try {
$coordinates = @file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($addr) . '&sensor=true');
$e=json_decode($coordinates);
// call to google api failed so has ZERO_RESULTS -- i.e. rubbish address...
if ( isset($e->status)) { if ( $e->status == 'ZERO_RESULTS' ) {echo '1:'; $err_res=true; } else {echo '2:'; $err_res=false; } } else { echo '3:'; $err_res=false; }
// $coordinates is false if file_get_contents has failed so create a blank array with Longitude/Latitude.
if ( $coordinates == false || $err_res == true ) {
$a = array( 'lat'=>0,'lng'=>0);
$coordinates = new stdClass();
foreach ( $a as $key => $value)
{
$coordinates->$key = $value;
}
} else {
// call to google ok so just return longitude/latitude.
$coordinates = $e;
$coordinates = $coordinates->results[0]->geometry->location;
}
return $coordinates;
}
catch (Exception $e) {
}
then to get the cords: where $pc is the postcode or address.... $address = get_lonlat( $pc ); $l1 = $address->lat; $l2 = $address->lng;
然后得到电线:其中$ pc是邮政编码或地址.... $ address = get_lonlat($ pc); $ l1 = $ address-> lat; $ l2 = $ address-> lng;
#8
0
There is no forumula, as street names and cities are essentially handed out randomly. The address needs to be looked up in a database. Alternatively, you can look up a zip code in a database for the region that the zip code is for.
没有论坛,因为街道名称和城市基本上是随机发放的。需要在数据库中查找地址。或者,您可以在数据库中查找邮政编码所针对的区域的邮政编码。
You didn't mention a country, so I'm going to assume you just want addresses in the USA. There are numerous databases you can use, some free, some not.
你没有提到一个国家,所以我假设你只想要在美国的地址。您可以使用许多数据库,一些是免费的,一些不是。
You can also use the Google Maps API to have them look up an address in their database for you. That is probably the easiest solution, but requires your application to have a working internet connection at all times.
您还可以使用Google Maps API让他们在您的数据库中查找地址。这可能是最简单的解决方案,但要求您的应用程序始终具有可用的Internet连接。