簡體   English   中英

如何獲得任何地址的經度和緯度?

[英]How to get longitude and latitude of any address?

你好,如果我在PHP中有street_name,State_name,City_name,Country_name和郵政編碼,那么獲取任何地址的經度和緯度的是什么? 謝謝

你可以使用下面的代碼來獲取lat,長期使用php。 這是兩種類型來獲得這個

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;

?>

編輯 - Google地圖請求必須通過https

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>
<?php
$address = 'BTM 2nd Stage, Bengaluru, Karnataka 560076'; // Address
$apiKey = 'api-key'; // Google maps now requires an API key.
// Get JSON results from this request
$geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false&key='.$apiKey);
$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
}
?>

您需要訪問地理編碼服務(即來自Google),沒有簡單的公式可以將地址傳輸到地理坐標。

您可以使用Google Maps API。 有關詳細信息,請參閱下面的博客文章。

http://stuff.nekhbet.ro/2008/12/12/how-to-get-coordinates-for-a-given-address-using-php.html

PHP有一些很好的內置函數來獲取地理位置。 也許看看這里: http//php.net/manual/en/ref.geoip.php

根據php手冊,“此擴展需要安裝GeoIP C庫1.4.0或更高版本。您可以從» http://www.maxmind.com/app/c獲取最新版本並自行編譯。”

我想出了以下內容,其中考慮了垃圾傳入和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) {
    }

然后得到電線:其中$ pc是郵政編碼或地址.... $ address = get_lonlat($ pc); $ l1 = $ address-> lat; $ l2 = $ address-> lng;

沒有論壇,因為街道名稱和城市基本上是隨機發放的。 需要在數據庫中查找地址。 或者,您可以在數據庫中查找郵政編碼所針對的區域的郵政編碼。

你沒有提到一個國家,所以我假設你只想要在美國的地址。 您可以使用許多數據庫,一些是免費的,一些不是。

您還可以使用Google Maps API讓他們在您的數據庫中查找地址。 這可能是最簡單的解決方案,但要求您的應用程序始終具有可用的Internet連接。

暫無
暫無

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

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