Thứ Hai, 21 tháng 1, 2019

Googlemap Geolocation "Lấy kinh độ + vĩ độ từ googlemap"

Link Geolocation:
- Link Geolocation : https://developers.google.com/maps/documentation/javascript/examples/map-geolocation


Link lấy key API googlemap:
https://console.cloud.google.com/home


Lấy thông tin của 1 địa điểm trên Google Maps:

Link:  https://tanvietblog.com/2012/07/15/php-lay-thong-tin-cua-1-dia-diem-tren-google-maps
<?php
  function get_infor_from_address($address = null) {
    $prepAddr = str_replace(' ', '+', stripUnicode($address));
    $geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
    $output = json_decode($geocode);
    return $output;
  }

  // Loại bỏ dấu tiếng Việt để cho kết quả chính xác hơn
  function stripUnicode($str){
    if (!$str) return false;
    $unicode = array(
      'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
      'd'=>'đ|Đ',
      'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
      'i'=>'í|ì|ỉ|ĩ|ị|Í|Ì|Ỉ|Ĩ|Ị',
      'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ|Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
      'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
      'y'=>'ý|ỳ|ỷ|ỹ|ỵ|Ý|Ỳ|Ỷ|Ỹ|Ỵ'
    );
    foreach($unicode as $nonUnicode=>$uni) $str = preg_replace("/($uni)/i",$nonUnicode,$str);
    return $str;
  }

  $demo = get_infor_from_address("154 Nguyen Luong Bang - Tp Da Nang");
  print_r($demo);
?>
code:
$address = get_infor_from_address("154 Nguyen Luong Bang - Tp Da Nang");
echo 'Vĩ độ (latitude): ' . $address->results[0]->geometry->location->lat;
echo 'Kinh độ (longitude): ' . $address->results[0]->geometry->location->lng;
output:
Vĩ độ (latitude): 16.0766843
Kinh độ (longitude): 108.1484813

Không có nhận xét nào:

Đăng nhận xét