Search code examples
phpgoogle-mapsgoogle-maps-embed

Embed URL to coordinates, kml or geojson


I've got a complex directions URL and embed URL that I would like to get polylines for. Once I can get them into polylines or something similar I can convert to final format: GeoJSON.

Direction Link

-or-

Embed Link

I have looked at the API's and I can't find anything that accepts or would decode the PB (what is this? it's not a protocol buffer). So far this is as far as I've got:

//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

The closest hints I could find are from this thread. I will keep looking but I'm stuck.

I'm trying to create an API based solution that has an input of one of these URL's and returns a GeoJSON.


Solution

  • would decode the PB (what is this? it's not a protocol buffer)

    For the record, because this overflow question keeps popping up on google results: it is a protocol buffer. PB litteraly stands for protocol buffer.

    It's just a different ASCII encoding (a compact URL encoding reminiscent of the binary encoding, not the usual JSON-like text encoding. When you squint at it it's not that much different than torrent's structure encoding), and Google doesn't provide us the .proto file.

    For each field:

    • first character is the id (identifies the field according to the corresponding .proto file)
    • second character is the type of the field
      • m is for message
      • s is for string
      • i, j, u, v are for various type of ints
      • f, d are for floating points
      • e is for enum
    • the rest is the payload

    So to unpack the fields you're seeing (even if we don't have the .proto file):

    • 1m73 message of type 1, containing 73 elements (the whole message set)
      • 1m12 submessage of type 1, contains 12 elements (probably information about the view box in the map box)
        • 1m3 sub-sub-message type 1, contains 3 elements (probably map coordinates)
          • 1d1472548.9575794793 first double field (probably zoom level)
          • 2d-72.8191002664707 second double field (probably longitude)
          • 3d43.87505426780168 third double field (probably latitude)
        • 2m3 second sub-sub-sub message (no idea given that it's not filled. Maybe a starting point if you code a route instead of a single point ?)
          • 1f0, 2f0, 3f0 the three members, currently just zero
        • 3m2 third block (looks like a screen resolution)
          • 1i1024, 2i768 : 1024x768 ? (and probably the omited field 3 would have been the color depth if present ??)
          • 4f13.1 no idea, but it's a float
      • 4m58 next message with 58 elements (to me it looks like a bunch of POI that we need to display in the box)
        • 3e0 an enum, set to zero (this one would be completely impossible to interpret without a proto or without experimenting, as you need the list of enums)

        • 4m5 five more elements probably a map poi - 1s0x0%3A0xa58b3d6041ba69f8 string '0x0:0xa58b3d6041ba69f8', note the use of Url_encoded character. In turn it looks like a pair of hex numbers, maybe a GUID ? - 2sGuilford+Welcome+Center string, the name with plus instead of blank (like most URLs) - 3m2 two elements to come - 1d42.8120069 and 2d-72.56614689999999 doubles probably map coordinates

        • 4m3 again a message of type 4 in this level, so probably another poi - 3m2, 1d43.3893165, 2d-72.40772249999999 but this one only specifies coordinates, and nothing else

        • 4m5 another poi - 1s0x4cb52e78df455c83%3A0xb6946ec850907db8 different pair of hex, GUID

          ...you got the idea...

      • 5e1 another bunch of information probably general settings
        • 3m2 this setting is a message (and looks like a locale)
          • 1sen, 2sus locale is en_US
        • 4v1472497940601 some other large number...

    Note: the original proto that Google doesn't show us, is probably a single multi level structure. Thus, the sub-sub-message ID don't have always the same meaning: they aren't global ID, but ID within the parent message.

    • inside a sub message ID 1 (view box ?), the sub-sub message 3 seems to be resolution.
    • inside a sub message ID 4 (POIs ?), the sub-ID 3 isn't even a message but some enum
    • inside a sub message ID 5 (parameters), the sub-sub message 3 is a locale