【问题标题】:Embed URL to coordinates, kml or geojson将 URL 嵌入坐标、kml 或 geojson
【发布时间】:2016-08-29 20:08:01
【问题描述】:

我有一个复杂的路线 URL 和嵌入 URL,我想为其获取折线。一旦我可以将它们变成折线或类似的东西,我就可以转换为最终格式:GeoJSON。

Direction Link

-或-

Embed Link

我查看了 API,但找不到任何可以接受或解码 PB 的内容(这是什么?它不是协议缓冲区)。到目前为止,这是我所掌握的:

//php
$pb_array = explode('!', $pb);
foreach($pb_array as $key => $value){
    echo "$key - $value<br/>";
}

===

1 - 1m73 2 - 1m12 3 - 1m3 4 - 1d1472548.9575794793 5 - 2d-72.8191002664707 6 - 3d43.87505426780168 7 - 2m3 8 - 1f0 9 - 2f0 10 - 3f0 11 - 3m2 12 - 1i1024 13 - 2i768 14 - 4f13.1 15 - 4m58 16 - 3e0 17 - 4m5 18 - 1s0x0%3A0xa58b3d6041ba69f8 19 - 2sGuilford+Welcome+Center 20 - 3m2 21 - 1d42.8120069 22 - 2d-72.56614689999999 23 - 4m3 24 - 3m2 25 - 1d43.3893165 26 - 2d-72.40772249999999 27 - 4m5 28 - 1s0x4cb52e78df455c83%3A0xb6946ec850907db8 29 - 2s130+Lower+Michigan+Road%2C+Pittsfield%2C+VT+05762 30 - 3m2 31 - 1d43.76898 32 - 2d-72.815214 33 - 4m4 34 - 1s0x0%3A0xea2de48bba82cc86 35 - 3m2 36 - 1d44.042544 37 - 2d-72.6046997 38 - 4m5 39 - 1s0x0%3A0x6bb602ed58bf4413 40 - 2sJay+Peak+Resort 41 - 3m2 42 - 1d44.9379515 43 - 2d-72.5045433 44 - 4m5 45 - 1s0x4cb392aaa4333a07%3A0x160aef1559868340 46 - 2sDolly+Copp+Campground+Rd%2C+Gorham%2C+NH+03581 47 - 3m2 48 - 1d44.335842199999995 49 - 2d-71.21837339999999 50 - 4m5 51 - 1s0x4cb392684201a94d%3A0xfa4a6f490a05429d 52 - 2sMt+Washington+Auto+Road%2C+1+Mount+Washington+Auto+Road%2C+Gorham%2C+NH+03581 53 - 3m2 54 - 1d44.288384099999995 55 - 2d-71.22459599999999 56 - 4m5 57 - 1s0x4cb38e798f42c3d9%3A0xc3b88e4dac01db12 58 - 2sMt+Washington 59 - 3m2 60 - 1d44.270585399999995 61 - 2d-71.3032723 62 - 4m5 63 - 1s0x89e2a7fa444124d5%3A0xe3ed24b6f864eba0 64 - 2sWells%2C+ME 65 - 3m2 66 - 1d43.322232899999996 67 - 2d-70.5805209 68 - 4m5 69 - 1s0x89e2ba813e828c71%3A0x8cdf74380f6a933d 70 - 2sLibby's+Oceanside+Camp%2C+York+Street%2C+York%2C+ME 71 - 3m2 72 - 1d43.147162 73 - 2d-70.626173 74 - 5e1 75 - 3m2 76 - 1sen 77 - 2sus 78 - 4v1472497940601

我能找到的最接近的提示来自这个thread。我会继续寻找,但我被困住了。

我正在尝试创建一个基于 API 的解决方案,该解决方案具有这些 URL 之一的输入并返回一个 GeoJSON。

【问题讨论】:

标签: php google-maps google-maps-embed


【解决方案1】:

将解码 PB(这是什么?它不是协议缓冲区)

记录在案,因为这个溢出问题不断在谷歌搜索结果中弹出:它 a protocol buffer。 PB 字面意思是 protocol buffer。

它只是一种不同的 ASCII 编码(一种紧凑的 URL 编码,让人联想到二进制编码,而不是通常的类似 JSON 的文本编码。当你眯着眼睛看它时,它与 torrent 的结构编码并没有太大区别),而 Google 没有向我们提供.proto 文件。

对于每个字段:

  • 第一个字符为id(根据对应的.proto文件标识字段)
  • 第二个字符是字段的类型
    • m 用于消息
    • s 用于字符串
    • i, j, u, v 用于各种类型的整数
    • f, d 用于浮点
    • e 代表枚举
  • 剩下的就是负载

所以要解压您看到的字段(即使我们没有.proto 文件):

  • 1m73 类型 1 的消息,包含 73 个元素(整个消息集)
    • 1m12 类型 1 的子消息,包含 12 个元素(可能是关于地图框中的视图框的信息)
      • 1m3子子消息类型1,包含3个元素(可能是地图坐标)
        • 1d1472548.9575794793 第一个双字段(可能是缩放级别)
        • 2d-72.8191002664707 第二个双字段(可能是经度)
        • 3d43.87505426780168第三个双场(可能是纬度)
      • 2m3 第二个 sub-sub-sub 消息(不知道它没有被填满。如果你编写一个路线而不是一个点,也许是一个起点?)
        • 1f02f03f0 三个成员,目前只有零个
      • 3m2第三块(看起来像屏幕分辨率)
        • 1i10242i768:1024x768? (如果存在的话,省略的字段 3 可能是颜色深度??)
        • 4f13.1 不知道,但它是一个浮动
    • 4m58 下一条消息,包含 58 个元素(对我来说,它看起来像是我们需要在框中显示的一堆 POI)
      • 3e0 一个枚举,设置为零(如果没有原型或没有实验,这个枚举完全不可能解释,因为您需要枚举列表)

      • 4m5另外五个元素可能是一个地图poi - 1s0x0%3A0xa58b3d6041ba69f8 string '0x0:0xa58b3d6041ba69f8',注意使用 Url_encoded 字符。反过来它看起来像一对十六进制数字,也许是一个 GUID ? - 2sGuilford+Welcome+Center 字符串,带有加号而不是空白的名称(与大多数 URL 一样) - 3m2 两个元素来了 - 1d42.81200692d-72.56614689999999 可能是地图坐标的两倍

      • 4m3 在这个级别又是一个类型 4 的消息,所以可能是另一个 poi - 3m2, 1d43.3893165, 2d-72.40772249999999 但是这个只指定坐标,没有别的

      • 4m5 另一个兴趣点 - 1s0x4cb52e78df455c83%3A0xb6946ec850907db8 不同的十六进制对,GUID

        ...你明白了...

    • 5e1另外一堆资料大概是通用设置
      • 3m2此设置是一条消息(看起来像一个语言环境)
        • 1sen, 2sus 语言环境是 en_US
      • 4v1472497940601 其他一些大数字...

注意:Google 没有向我们展示的原始原型可能是一个单一的多级结构。因此,子子消息 ID 并不总是具有相同的含义:它们不是全局 ID,而是父消息中的 ID。

  • 在子消息 ID 1(视图框?)内,子子消息 3 似乎是解析。
  • 在子消息 ID 4 (POI ?) 中,子 ID 3 甚至不是消息,而是一些枚举
  • 在子消息 ID 5(参数)内,子子消息 3 是语言环境

【讨论】:

    【解决方案2】:

    嗯,这是我草率但可行的解决方案。我的需求需要 GeoJSON,但其他人可以使用 google maps 服务在您拥有 lat/lng 数组后获得所需的输出。

    $embed = '<iframe src="https://www.google.com/maps/embed?pb=!1m73!1m12!1m3!1d1472548.9575794793!2d-72.8191002664707!3d43.87505426780168!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!4m58!3e0!4m5!1s0x0%3A0xa58b3d6041ba69f8!2sGuilford+Welcome+Center!3m2!1d42.8120069!2d-72.56614689999999!4m3!3m2!1d43.3893165!2d-72.40772249999999!4m5!1s0x4cb52e78df455c83%3A0xb6946ec850907db8!2s130+Lower+Michigan+Road%2C+Pittsfield%2C+VT+05762!3m2!1d43.76898!2d-72.815214!4m4!1s0x0%3A0xea2de48bba82cc86!3m2!1d44.042544!2d-72.6046997!4m5!1s0x0%3A0x6bb602ed58bf4413!2sJay+Peak+Resort!3m2!1d44.9379515!2d-72.5045433!4m5!1s0x4cb392aaa4333a07%3A0x160aef1559868340!2sDolly+Copp+Campground+Rd%2C+Gorham%2C+NH+03581!3m2!1d44.335842199999995!2d-71.21837339999999!4m5!1s0x4cb392684201a94d%3A0xfa4a6f490a05429d!2sMt+Washington+Auto+Road%2C+1+Mount+Washington+Auto+Road%2C+Gorham%2C+NH+03581!3m2!1d44.288384099999995!2d-71.22459599999999!4m5!1s0x4cb38e798f42c3d9%3A0xc3b88e4dac01db12!2sMt+Washington!3m2!1d44.270585399999995!2d-71.3032723!4m5!1s0x89e2a7fa444124d5%3A0xe3ed24b6f864eba0!2sWells%2C+ME!3m2!1d43.322232899999996!2d-70.5805209!4m5!1s0x89e2ba813e828c71%3A0x8cdf74380f6a933d!2sLibby&#39;s+Oceanside+Camp%2C+York+Street%2C+York%2C+ME!3m2!1d43.147162!2d-70.626173!5e1!3m2!1sen!2sus!4v1472497940601" width="600" height="450" frameborder="0" style="border:0" allowfullscreen></iframe>';
    
    $array = array();
    preg_match( '/src="([^"]*)"/i', $embed, $array ) ;
    list($pre, $pb) = split("pb=", $array[1]);
    
    if($pb == "" || strpos($pb, "!") === false)
        die(json_encode(array("success"=>false)));
    
    //echo "PB Extracted:<br>";
    //echo $pb;
    ///echo "<br><br>Decode:<br/>";
    $pb_array = explode('!', $pb);
    
    $coords = array();
    $address;$addressHex;
    $results = array();
    foreach($pb_array as $key => $value){
        //uncomment to debug output
        //echo "$key - $value<br/>";
    
        if($value == "3m2" || $value == "2m2"){
            //3m2 seems to be the divider of these 'places'
            if(count($coords) != 3) //don't add the center of map data (3 coordinates [height, lng, lat])
                array_push($results, array("coords"=>$coords,"address"=>$address,"addressHex"=>$addressHex));
            $coords = array(); //reset array
        }else{
            $type = substr($value, 1, 1);
            $stype = substr($value, 0, 2);
            $value = substr($value, 2);
            //echo "$type - $value<br/>";
            if($type == "d"){
                //Found Lat,Lng
                array_push($coords, $value);
            }else if($stype == "2s"){
                //Address
                $address = $value;
            }else if($stype == "1s"){
                //Address Encoded in some way
                $addressHex = $value;
            }
        }
    }
    
    //echo "<br><br>Google Result<br/>";
    //echo json_encode($results);
    
    
    //echo "<br><br>Mapbox API:<br/>";
    $waypoints = array();
    for($i=0;$i<count($results);$i++){
        if(count($results[$i]["coords"])){
            $lat = $results[$i]["coords"][0];
            $lng = $results[$i]["coords"][1];
            array_push($waypoints, "$lng%2C$lat");
        }
    }
    $waypoints = implode("%3B", $waypoints); //convert to string
    
    $mapbox_api_key = "pk.eyJ1I.....";
    $url = "https://api.mapbox.com/directions/v5/mapbox/driving/$waypoints.json?steps=false&alternatives=false&overview=full&geometries=geojson&access_token=$mapbox_api_key";
    
    //echo "<br><br>Mapbox Response:<br/>";
    $response = file_get_contents($url); 
    
    $json = json_decode($response,true);
    //echo "<br><br>Mapbox Geometry:<br/>";
    $coordinates = $json["routes"][0]["geometry"]["coordinates"];
    
    $geojson = (array("type"=>"FeatureCollection","features"=>array(array("type"=>"Feature","geometry"=>array("type"=>"LineString","coordinates"=>$coordinates),"properties"=>array()))));
    
    echo json_encode(array("success"=>true, "geojson"=>$geojson));
    

    【讨论】:

      猜你喜欢
      • 2013-08-05
      • 1970-01-01
      • 2020-07-16
      • 2013-04-09
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      相关资源
      最近更新 更多