获取行程纬度使用PHP从Google地图获取经度 [英] Get itinerary latitudes & longitude from google maps with PHP

查看:110
本文介绍了获取行程纬度使用PHP从Google地图获取经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想列出行程的纬度和经度.可能是所有点,也可能是1-2公里内的所有点.

I want to list the latitude and longitude of itinerary. It could be all points or it could be all points in 1-2 kilometers.

我想做的是:用户选择A作为起点,选择B作为终点.我想在地图上显示A和B之间的道路附近的一些地方.但是我需要一个职位.

What I'm trying to do is: user selected A as the starting point and B as the ending point. I want to show some places near the road between A and B on the map. But I need a positions for this.

例如,在此处共享JavaScript代码,据说可以使用 DirectionsResult对象.

As an example, a JavaScript code is shared here and It is said that this can be done with DirectionsResult Object.

var request = {
  origin: start_point,
  destination: end_point,
  travelMode: google.maps.TravelMode.DRIVING
};

var directionsService = new google.maps.DirectionsService();
  directionsService.route(request, function(response, status) {
  if (status == google.maps.DirectionsStatus.OK) {
    var path = (response.routes[0].overview_path);
  }
});

但是我试图用php来做到这一点,而我必须用php来做到这一点.

But I'm trying to do this with php and I have to do this with php.

我阅读了Google Map API.我还阅读了yandex map api,但这似乎只能通过javascript完成.

I read google map api. I've also read the yandex map api, but this seems to be done only with javascript.

有人知道用php做到这一点的方法吗?

Does anyone know a way to do this with php?

推荐答案

从注释中我可以理解,问题是找到(使用PHP)可以从< google方向查询中的strong>折线点.

From comments I understand the question is to find (using PHP) the intermediate lat,lng pairs that can be extracted from the polyline points in a google directions query.

这有点不寻常,因为人们通常在浏览器中使用折线点进行地图绘制,因此JavaScript库已经很好地完成了此任务.但是,在PHP中并非如此.

This is a bit unusual because people normally use the polyline points for map drawing in the browser, and so the JavaScript libraries are well equipped for this task. However, not so in PHP.

点数据以ASCII字符字符串的形式出现在JSON结果对象中,有时很长,并且始终不可读".在此字符串中,编码了每条腿的起点和终点之间的中间经度对列表.编码方法显示在Google网站 https://developers.google.com/maps /documentation/utilities/polylinealgorithm ,下面的算法只是该算法的一个逆转,因此进行了注释.

The points data appears in the JSON result object as string of ascii characters, sometimes quite long and always 'unreadable'. Into this string is encoded a list of intermediate lat lng pairs between the start and end of each leg. The coding method is presented at the google site https://developers.google.com/maps/documentation/utilities/polylinealgorithm and the algorithm below is just a reversal of that and is commented accordingly.

该示例显示了在澳大利亚珀斯的月牙形街道上2个点之间找到的路线.选择起点到终点是为了鼓励绘制路线时需要多个中间点.根据需要替换您自己的搜索.

The example shows a directions find between 2 points, on crescent shaped streets, in Perth, Australia. The start-end points were chosen to encourage multiple intermediate points as would be needed to draw the route. Substitute your own search as needed.

请注意,JSON还在每个结果对象的末尾提供了这些字段.

Note that the JSON also provides these fields also at the end of each results object.

     "overview_polyline" : {
        "points" : "~n{aEmbwaU_B@cCBk@Lo@d@UVOb@Mh@Ab@@@@BBF@DGNABD`@Fh@Pb@VZn@b@d@J"
     },

这要少得多,也不太准确(如果您绘制的地图可能会偏离地图上的实际道路线),但是也可以用相同的方式进行解码.

This is much less detailed and less accurate (if you draw will probably depart from actual road lines on map), but can also be decoded in the same way.

但是,最好的中间点是通过使用以下步骤来迭代步骤:

The best intermediate points are however, by iterating through the steps using:

    "polyline" : {
        "points" : "~n{aEmbwaUg@@w@?{A?g@BUBUHSJ[XUVOb@Mh@Ab@"
     }, 

最后,可以在此处

Finally, the original source for the algorithm can be found here http://unitstep.net/blog/2008/08/02/decoding-google-maps-encoded-polylines-using-php/. So thanks to Peter Chng for this work back in 2008! Peter also acknowledges Mark MClure who did the original coding in JavaScript. I hacked about with and added more comments - to make more aligned with the google recipe, but no more.

我也刚刚意识到有此链接 https://github. com/emcconville/google-map-polyline-encoding-tool (我认为但尚未测试)提供了一个类和CLI工具来完成两种转换.

I have also just realised there is this link https://github.com/emcconville/google-map-polyline-encoding-tool which (I think but have not tested) provides a class and a CLI tool to do the conversions both ways.

$json = file_get_contents("https://maps.googleapis.com/maps/api/directions/json?origin=20%20%20Kintyre%20Crescent,%20Churchlands&destination=%2018Kinross%20Crescent,%20Churchlands&key="); 
$details = json_decode($json,true);
print_r($details);    // show the full result

$points = $details['routes'][0]['legs'][0]['steps'][0]['polyline']['points'];
echo($points);        // show the points string for one leg

// show the start and end locations for that leg
print_r($details['routes'][0]['legs'][0]['steps'][0]['start_location']);
print_r($details['routes'][0]['legs'][0]['steps'][0]['end_location']);

// work out the intermdiate points (normally used for drawing)
$decodedPoints= decodePolylinePoints($points);
print_r($decodedPoints);   // print out the intermediate points

// This function decodes the polylone points in PHP
function decodePolylinePoints($pointsString)
{
    $len = strlen($pointsString);
    $latLons = array();   // the output array
    $lat = 0;             // temp storage for lat and lng
    $lng = 0;

    $index = 0;           // index to curent character
    while ($index < $len)   // process each lat,lng pair
      {
        // first build the lat
        // NOTE: first lat is an absolute value
        // NOTE: subsequent lats are offsets from previous values for coding efficiency
        $char = 0;   // char as read from points string
        $shift = 0;  // cumulative shift amount
        $value = 0;  // temp value during computation

        do // Read, convert and shift 5 bit chunks until terminator is reached to get lat
        {
          $char = ord(substr($pointsString, $index++)) - 63; // return ascii value less 63
          $value |= ($char & 0x1f) << $shift;                // convert to 5 bit and shift left
          $shift += 5;                                       // next shift is 5 extra
        }
        while ($char >= 0x20);                               // value of 20 indicates end of lat

        $lat += (($value & 1) ? ~($value >> 1) : ($value >> 1));  // convert negative values and save

        // now build the lng
        // NOTE: first lng is an absolute value
        // NOTE: subsequent lngs are offsets from previous values for coding efficiency
        $shift = 0;
        $value = 0;

        do  // build up lng from 5 bit chunks
        {
          $char= ord(substr($pointsString, $index++)) - 63;  // return ascii value less 63
          $value |= ($char & 0x1f) << $shift;               // convert to 5 bit and shift left
          $shift += 5;                                       // next shift is 5 extra
        }
        while ($char >= 0x20);                               // value of 20 indicates end of lng

        $lng += (($value & 1) ? ~($value >> 1) : ($value >> 1)); // convert negative values and save

        $latLons[] = array($lat * 1e-5, $lng * 1e-5);        // original values were * 1e5
      }

      return $latLons;    // points array converted to lat,lngs 
}

这篇关于获取行程纬度使用PHP从Google地图获取经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆