วันพุธที่ 5 มิถุนายน พ.ศ. 2562

เขียนโค๊ด PHP ง่าย ๆ สำหรับแปลงค่าจากพิกัดเป็นชื่อตำแหน่ง reversed geocoding โดยใช้ OpenStreetMap


เนื้อหาในวันนี้เป็นการเขียนโค๊ด PHP ง่าย ๆ สำหรับแปลงค่าจากพิกัดเป็นชื่อตำแหน่ง reversed geocoder โดยใช้ OpenStreetMap ขอบระบบ gps tracking ปัญหาที่ผู้เขียนพบเจอของการใช้การแปลงพิกัดโดยใช้ google map ก็คือจำกัดการค้นหาชื่อตำแหน่งมีค่าใช้จ่าย 5 USD ต่อ 1000 ครั้ง และต้องสมัครขอรับคีย์สำหรับการใช้งาน เพื่อให้การดำเนินการแปลงค่าจากพิกัดเป็นชื่อตำแหน่ง reversed geocoding ทำได้โดยใช้แผนที่โอเพนซอร์สได้เหมือนกัน


Input Agent (lat/lon)---|---   PHP reversegeocoder.php(get/post) ---|---  Output XML

รูปที่ 1 การทำงานของระบบ reverse geocoding

เมื่อมีการส่งค่าอินพุทตำแหน่ง ละติจูดและลองกิจูด ผ่านทางเวบบราวเซอร์หรือเอเจนท์เพื่อทำการเปิดไฟล์ reversegeocoder.php จากนั้นโปรแกรม reversegeocoder.php  จะส่งค่าพิกัดดังกล่าวไปค้นหาชื่อตำแหน่งจาก OpenStreetMap เมื่อได้ค่าตำแหน่งมาแล้วก็แสดงผลเป็นแบบ XML จากตัวอย่างโค๊ดนี้จะใช้จุดเด่นของฟังก์ชั่น file_get_contents ของ PHP ที่จะอ่านค่าจะต้นทางเปลี่ยนเป็นสตริงค์

ทดสอบการทำงานสคริปออนไลน์

http://www.thaimdvr.com/geocoder.php?longitude=100.478607&latitude=13.887050
 
ลองหรือยังแต่ที่ผู้เขียนทดลองใช้งานมาครบปี มันก็ไม่เคยมีปัญหาใดๆ ใช้งานจริงได้ดั่งใจ ระบบ GPS TRACKING ที่ดีมีมาตรฐาน รู้ลึกรู้จริงเรื่องอุปกรณ์ และงานบริการหลังการงาน แวะไปเยี่ยมชมเวบไซต์จีพีเอสไทยได้ 
 

   <?php
 // get op, longitude and latitude from the method get/post url
  $op        = isset($_GET['op']) ? $_GET['op'] : 0;
  $lon       = isset($_GET['longitude']) ? $_GET['longitude'] : 0;
  $lat       = isset($_GET['latitude']) ? $_GET['latitude'] : 0;

 $geo_array = array (
    'Address' => 'Perfect Place Rattanathibet',
    'AdministrativeAreaName' => 'Nonthaburi',
    'SubAdministrativeAreaName' => 'Muang Nonthaburi',
    'CityName' => 'Saima',
    'StreetName' => 'Rattanathibet Road',
    'CountryName' => 'Thailand',
    'PostalCodeNumber' => '11000'
 );
 //set option for usign openstreetmap by using file_get_contents
 $opts = array('http'=>array('header'=>"User-Agent: StevesCleverAddressScript 3.7.6\r\n"));
 $context = stream_context_create($opts);
 //url for the openstreetmap
 $homepage = file_get_contents("https://nominatim.openstreetmap.org/reverse?format=json&lat=".$lat."&lon=".$lon."&zoom=27&addressdetails=1",false,$context);
 $homepage_json = json_decode($homepage,1);
 //assigned data from the openstreetmap to array
        $geo_array['Address']=$homepage_json['display_name'];
        $geo_array['AdministrativeAreaName']=$homepage_json['address']['state'];
        $geo_array['SubAdministrativeAreaName']=$homepage_json['address']['suburb'];
        $geo_array['CityName']=$homepage_json['address']['city'];
        $geo_array['StreetName']=$homepage_json['address']['road'];
        $geo_array['PostalCodeNumber']=$homepage_json['address']['postcode'];
        $geo_array['CountryName']=$homepage_json['address']['country'];
 //convert to xml 
 $xml = new SimpleXMLElement('<Location/>');
 array_walk_recursive($geo_array, function ($value, $key) use ($xml) { $xml->addChild($key,$value);});
 echo $xml->asXML();

?>