Question

When using google directions api with the alternatives set to true, some of the suggested routes go over park. Has this behaviour been observed by anyone else? Following is the URL I hit,

http://maps.googleapis.com/maps/api/directions/json?origin=37.557139,-121.975332&destination=37.539156,-121.923666&&alternatives=true&sensor=false

It returns following json response with 3 alternative routes,

{
   "routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : 37.56534449999999,
               "lng" : -121.923525
            },
            "southwest" : {
               "lat" : 37.5392255,
               "lng" : -121.9749241
            }
         },
         "copyrights" : "Map data ©2013 Google",
         "legs" : [
            {
               "distance" : {
                  "text" : "3.7 mi",
                  "value" : 6020
               },
               "duration" : {
                  "text" : "9 mins",
                  "value" : 531
               },
               "end_address" : "42701-42753 Mission Boulevard, Fremont, CA 94539, USA",
               "end_location" : {
                  "lat" : 37.5392255,
                  "lng" : -121.923525
               },
               "start_address" : "Fremont BART Station, Fremont, CA 94536, USA",
               "start_location" : {
                  "lat" : 37.5576351,
                  "lng" : -121.9749241
               },
               "steps" : [
                  {
                     "distance" : {
                        "text" : "400 ft",
                        "value" : 122
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 50
                     },
                     "end_location" : {
                        "lat" : 37.5570393,
                        "lng" : -121.9737687
                     },
                     "html_instructions" : "Head \u003cb\u003esoutheast\u003c/b\u003e toward \u003cb\u003eWalnut Ave\u003c/b\u003e",
                     "polyline" : {
                        "points" : "gnfdFffngVh@oA`A{BJY"
                     },
                     "start_location" : {
                        "lat" : 37.5576351,
                        "lng" : -121.9749241
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0.9 mi",
                        "value" : 1406
                     },
                     "duration" : {
                        "text" : "2 mins",
                        "value" : 90
                     },
                     "end_location" : {
                        "lat" : 37.5653107,
                        "lng" : -121.9618013
                     },
                     "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eWalnut Ave\u003c/b\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "ojfdF`_ngVUQQMqAaA]]GEe@g@US]e@wBgD}@iBUa@iBcDkA{B}BkEqAgCsDsH]o@kBeD_A{AeAgBYk@yDaGwAyBQWg@{@"
                     },
                     "start_location" : {
                        "lat" : 37.5570393,
                        "lng" : -121.9737687
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "2.8 mi",
                        "value" : 4492
                     },
                     "duration" : {
                        "text" : "7 mins",
                        "value" : 391
                     },
                     "end_location" : {
                        "lat" : 37.5392255,
                        "lng" : -121.923525
                     },
                     "html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eMission Blvd\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDestination will be on the right\u003c/div\u003e",
                     "maneuver" : "turn-right",
                     "polyline" : {
                        "points" : "e~gdFftkgVE]?G?G?EBIBM~JwIlAgA`EeDnBiBb@a@zA}AZ[dDmGxHuNxAoCP]d@_AZo@jBqDpAaCzJwQz@aBzAsCzIsO|AsCdGqKbBuCN]jGgMXm@r@}At@}Ab@y@Rg@n@sAh@gArAiCrBgE`@{@zAaD\\q@Zk@Ra@pF_LpG{M`AsBjAcCn@mAJSLUNWb@k@T[j@q@@?DGDERO@AJGrA{@jAw@PMbAu@"
                     },
                     "start_location" : {
                        "lat" : 37.5653107,
                        "lng" : -121.9618013
                     },
                     "travel_mode" : "DRIVING"
                  }
               ],
               "via_waypoint" : []
            }
         ],
         "overview_polyline" : {
            "points" : "gnfdFffngVjBkEJYUQcBoAe@c@{@{@]e@wBgDsAkCuD_HoEsIqEcJkDaG_BsCkIoMEe@?MFWlM_L`EeDnBiB~B_CZ[dDmGrKeS~D_IlMyUvCuFbXoe@zGeNlAkC|CsG|BqEtCcGhDaHpSsb@xAoCx@gAl@q@`@_@|DiCbAu@"
         },
         "summary" : "Mission Blvd",
         "warnings" : [],
         "waypoint_order" : []
      },
      {
         "bounds" : {
            "northeast" : {
               "lat" : 37.5576351,
               "lng" : -121.923525
            },
            "southwest" : {
               "lat" : 37.5311789,
               "lng" : -121.977954
            }
         },
         "copyrights" : "Map data ©2013 Google",
         "legs" : [
            {
               "distance" : {
                  "text" : "4.9 mi",
                  "value" : 7824
               },
               "duration" : {
                  "text" : "11 mins",
                  "value" : 653
               },
               "end_address" : "42701-42753 Mission Boulevard, Fremont, CA 94539, USA",
               "end_location" : {
                  "lat" : 37.5392255,
                  "lng" : -121.923525
               },
               "start_address" : "Fremont BART Station, Fremont, CA 94536, USA",
               "start_location" : {
                  "lat" : 37.5576351,
                  "lng" : -121.9749241
               },
               "steps" : [
                  {
                     "distance" : {
                        "text" : "354 ft",
                        "value" : 108
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 16
                     },
                     "end_location" : {
                        "lat" : 37.5571031,
                        "lng" : -121.9739004
                     },
                     "html_instructions" : "Head \u003cb\u003esoutheast\u003c/b\u003e toward \u003cb\u003eWalnut Ave\u003c/b\u003e",
                     "polyline" : {
                        "points" : "gnfdFffngVh@oA`A{B"
                     },
                     "start_location" : {
                        "lat" : 37.5576351,
                        "lng" : -121.9749241
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0.4 mi",
                        "value" : 683
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 74
                     },
                     "end_location" : {
                        "lat" : 37.5518666,
                        "lng" : -121.977954
                     },
                     "html_instructions" : "Take the 1st \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eWalnut Ave\u003c/b\u003e",
                     "polyline" : {
                        "points" : "{jfdFz_ngVXRzB|A`@ZdBpA~CvBjAx@`@XDBRNHHVR`Ap@p@d@hBpAbAr@VRzAdAVR"
                     },
                     "start_location" : {
                        "lat" : 37.5571031,
                        "lng" : -121.9739004
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0.3 mi",
                        "value" : 507
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 66
                     },
                     "end_location" : {
                        "lat" : 37.5493906,
                        "lng" : -121.9731305
                     },
                     "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003ePaseo Padre Pkwy\u003c/b\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "ejedFdyngVJWn@wARi@h@qA|AqD|AwDpA}CfBoE"
                     },
                     "start_location" : {
                        "lat" : 37.5518666,
                        "lng" : -121.977954
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "2.7 mi",
                        "value" : 4408
                     },
                     "duration" : {
                        "text" : "6 mins",
                        "value" : 330
                     },
                     "end_location" : {
                        "lat" : 37.5316415,
                        "lng" : -121.9341935
                     },
                     "html_instructions" : "Continue straight to stay on \u003cb\u003ePaseo Padre Pkwy\u003c/b\u003e",
                     "maneuver" : "straight",
                     "polyline" : {
                        "points" : "uzddF`{mgVzAqDRg@pAaDTi@x@iB~@uBtAmDdAuBn@mAdAoBXg@dEwHf@}@lEsIb@y@h@_ALSt@_A^e@JOhCyCfAsAz@aAP[T]Tc@Tk@V{@Nq@Ls@Hm@Bg@Be@@g@?k@Ag@Cc@Eo@OoBg@qEO}@aA{FcAkFaAkFKoAEo@AiA@w@FeA?EL{@Ls@Nk@r@oBf@iA^o@\\e@Ra@NWXc@x@_B\\m@b@{@Vs@TcANk@Hg@R{AT}Ab@kD~AuMn@yETeB@QHq@Nq@Lc@Xk@PWPO`@[TMVKZI\\EfC@N?nB@|@CvBWrAWVGREhAc@v@g@~@}@h@u@LQDIHQJQ@C@CFMJ[\\_A`@yAd@aBPo@\\gAJQ^g@l@g@tAm@"
                     },
                     "start_location" : {
                        "lat" : 37.5493906,
                        "lng" : -121.9731305
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0.2 mi",
                        "value" : 396
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 46
                     },
                     "end_location" : {
                        "lat" : 37.5311789,
                        "lng" : -121.9386345
                     },
                     "html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eWashington Blvd\u003c/b\u003e",
                     "maneuver" : "turn-right",
                     "polyline" : {
                        "points" : "wkadFtgfgVPnBPvBEZBp@H`BB`@NtDLvBLnC"
                     },
                     "start_location" : {
                        "lat" : 37.5316415,
                        "lng" : -121.9341935
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0.8 mi",
                        "value" : 1365
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 82
                     },
                     "end_location" : {
                        "lat" : 37.5385712,
                        "lng" : -121.927027
                     },
                     "html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e to merge onto \u003cb\u003eI-680 N\u003c/b\u003e toward \u003cb\u003eSacramento\u003c/b\u003e",
                     "polyline" : {
                        "points" : "{hadFlcggVg@H[Fc@Be@?m@Ok@a@_@a@c@o@]e@g@_@gAmCAECGGSACAE[w@gAmCyFwMAAuAcDkCsGmCoGkAsCmA}CoCgG"
                     },
                     "start_location" : {
                        "lat" : 37.5311789,
                        "lng" : -121.9386345
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0.2 mi",
                        "value" : 312
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 21
                     },
                     "end_location" : {
                        "lat" : 37.5395679,
                        "lng" : -121.9237934
                     },
                     "html_instructions" : "Take the \u003cb\u003eMission Blvd\u003c/b\u003e exit toward \u003cb\u003eCalifornia 238\u003c/b\u003e",
                     "maneuver" : "ramp-right",
                     "polyline" : {
                        "points" : "awbdF|zdgVCq@i@gAUg@Qa@K_@Mi@CWIgAAYASASAO?QAQCOEQCQEKEKGOCKGOQk@"
                     },
                     "start_location" : {
                        "lat" : 37.5385712,
                        "lng" : -121.927027
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "148 ft",
                        "value" : 45
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 18
                     },
                     "end_location" : {
                        "lat" : 37.5392255,
                        "lng" : -121.923525
                     },
                     "html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eMission Blvd\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDestination will be on the right\u003c/div\u003e",
                     "maneuver" : "turn-right",
                     "polyline" : {
                        "points" : "i}bdFtfdgVbAu@"
                     },
                     "start_location" : {
                        "lat" : 37.5395679,
                        "lng" : -121.9237934
                     },
                     "travel_mode" : "DRIVING"
                  }
               ],
               "via_waypoint" : [
                  {
                     "location" : {
                        "lat" : 37.542142,
                        "lng" : -121.9527896
                     },
                     "step_index" : 3,
                     "step_interpolation" : 0.4860572290564731
                  }
               ]
            }
         ],
         "overview_polyline" : {
            "points" : "gnfdFffngVjBkEtCpBfClBjFpDdAv@xAdAzCvBnE`DxBkFzDiJxDmJvEeLxB_FtAmDdAuBtB}D~E_JtFqKlAyBnBiCpEmFz@aAP[j@aAl@gB\\eBLuADmAAsAIsAOoBg@qEqAyHeCwMQ_CAiA@w@FkAZoBbA{CfAyBzAcCvAmCz@oBd@oB\\cCx@iGnCoTVwBXcBf@oAb@g@v@i@r@U\\EfC@~B@|@CvBWjB_@|Ai@v@g@~@}@v@gADIVg@r@mBfA{Dn@wBj@y@l@g@tAm@b@fFEZBp@LbC\\lHLnCg@H_AJe@?m@Ok@a@_@a@aAuAg@_@gAmCEMIW]}@aIeRqJiUyCqHoCgGCq@_AoB]aAQaAKaBEiAOeAWs@Y{@bAu@"
         },
         "summary" : "Paseo Padre Pkwy",
         "warnings" : [],
         "waypoint_order" : []
      },
      {
         "bounds" : {
            "northeast" : {
               "lat" : 37.5576351,
               "lng" : -121.923525
            },
            "southwest" : {
               "lat" : 37.5392255,
               "lng" : -121.977954
            }
         },
         "copyrights" : "Map data ©2013 Google",
         "legs" : [
            {
               "distance" : {
                  "text" : "4.3 mi",
                  "value" : 6957
               },
               "duration" : {
                  "text" : "12 mins",
                  "value" : 697
               },
               "end_address" : "42701-42753 Mission Boulevard, Fremont, CA 94539, USA",
               "end_location" : {
                  "lat" : 37.5392255,
                  "lng" : -121.923525
               },
               "start_address" : "Fremont BART Station, Fremont, CA 94536, USA",
               "start_location" : {
                  "lat" : 37.5576351,
                  "lng" : -121.9749241
               },
               "steps" : [
                  {
                     "distance" : {
                        "text" : "354 ft",
                        "value" : 108
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 16
                     },
                     "end_location" : {
                        "lat" : 37.5571031,
                        "lng" : -121.9739004
                     },
                     "html_instructions" : "Head \u003cb\u003esoutheast\u003c/b\u003e toward \u003cb\u003eWalnut Ave\u003c/b\u003e",
                     "polyline" : {
                        "points" : "gnfdFffngVh@oA`A{B"
                     },
                     "start_location" : {
                        "lat" : 37.5576351,
                        "lng" : -121.9749241
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0.4 mi",
                        "value" : 683
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 74
                     },
                     "end_location" : {
                        "lat" : 37.5518666,
                        "lng" : -121.977954
                     },
                     "html_instructions" : "Take the 1st \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eWalnut Ave\u003c/b\u003e",
                     "polyline" : {
                        "points" : "{jfdFz_ngVXRzB|A`@ZdBpA~CvBjAx@`@XDBRNHHVR`Ap@p@d@hBpAbAr@VRzAdAVR"
                     },
                     "start_location" : {
                        "lat" : 37.5571031,
                        "lng" : -121.9739004
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0.3 mi",
                        "value" : 507
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 66
                     },
                     "end_location" : {
                        "lat" : 37.5493906,
                        "lng" : -121.9731305
                     },
                     "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003ePaseo Padre Pkwy\u003c/b\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "ejedFdyngVJWn@wARi@h@qA|AqD|AwDpA}CfBoE"
                     },
                     "start_location" : {
                        "lat" : 37.5518666,
                        "lng" : -121.977954
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "1.6 mi",
                        "value" : 2600
                     },
                     "duration" : {
                        "text" : "3 mins",
                        "value" : 191
                     },
                     "end_location" : {
                        "lat" : 37.5411851,
                        "lng" : -121.9481001
                     },
                     "html_instructions" : "Continue straight to stay on \u003cb\u003ePaseo Padre Pkwy\u003c/b\u003e",
                     "maneuver" : "straight",
                     "polyline" : {
                        "points" : "uzddF`{mgVzAqDRg@pAaDTi@x@iB~@uBtAmDdAuBn@mAdAoBXg@dEwHf@}@lEsIb@y@h@_ALSt@_A^e@JOhCyCfAsAz@aAP[T]Tc@Tk@V{@Nq@Ls@Hm@Bg@Be@@g@?k@Ag@Cc@Eo@OoBg@qEO}@aA{FcAkFaAkFKoAEo@AiA@w@FeA?EL{@Ls@Nk@r@oBf@iA^o@\\e@Ra@NW"
                     },
                     "start_location" : {
                        "lat" : 37.5493906,
                        "lng" : -121.9731305
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0.7 mi",
                        "value" : 1195
                     },
                     "duration" : {
                        "text" : "2 mins",
                        "value" : 106
                     },
                     "end_location" : {
                        "lat" : 37.54969699999999,
                        "lng" : -121.9398308
                     },
                     "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eDriscoll Rd\u003c/b\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "mgcdFr~hgVOS]YeEsD{BmB_BsAuEoEwAsAuB{BYYg@g@MMaBeBsBsBwBsBwAwAoCsCkDgD"
                     },
                     "start_location" : {
                        "lat" : 37.5411851,
                        "lng" : -121.9481001
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "1.2 mi",
                        "value" : 1864
                     },
                     "duration" : {
                        "text" : "4 mins",
                        "value" : 244
                     },
                     "end_location" : {
                        "lat" : 37.5392255,
                        "lng" : -121.923525
                     },
                     "html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eMission Blvd\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDestination will be on the right\u003c/div\u003e",
                     "maneuver" : "turn-right",
                     "polyline" : {
                        "points" : "s|ddF|jggVjGgMXm@r@}At@}Ab@y@Rg@n@sAh@gArAiCrBgE`@{@zAaD\\q@Zk@Ra@pF_LpG{M`AsBjAcCn@mAJSLUNWb@k@T[j@q@@?DGDERO@AJGrA{@jAw@PMbAu@"
                     },
                     "start_location" : {
                        "lat" : 37.54969699999999,
                        "lng" : -121.9398308
                     },
                     "travel_mode" : "DRIVING"
                  }
               ],
               "via_waypoint" : [
                  {
                     "location" : {
                        "lat" : 37.5448008,
                        "lng" : -121.9447476
                     },
                     "step_index" : 4,
                     "step_interpolation" : 0.4176798920642508
                  }
               ]
            }
         ],
         "overview_polyline" : {
            "points" : "gnfdFffngVjBkEtCpBfClBjFpDdAv@xAdAzCvBnE`DxBkFzDiJxDmJvEeLxB_FtAmDdAuBtB}D~E_JtFqKlAyBnBiCpEmFz@aAP[j@aAl@gB\\eBLuADmAAsAIsAOoBg@qEqAyHeCwMQ_CAiA@w@FkAZoBbA{CfAyBp@gANWOScFmE{EaEmHcHoCuCkGoGoEkEoCsCkDgDdHuNhB{DpC}FfEqI|B}E~H_PnMaXXi@r@cA`AmAFGf@_@~CsBtAcA"
         },
         "summary" : "Paseo Padre Pkwy and Mission Blvd",
         "warnings" : [],
         "waypoint_order" : []
      }
   ],
   "status" : "OK"
}

I'm using the overview_polyline for each route object in javascript to see the polyline in map. I will be using the API from a backend python application and javascript is there only for cross checking at the moment just in case if you are wondering why I'm not using the javascript API.

<html> 
<head> 
<title>Test</title> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?libraries=geometry&amp;sensor=false"></script>
<style type="text/css"> 
    #map {width:670px;height:600px;}
</style> 
<script type='text/javascript'>
function initialize() {
    var myLatlng = new google.maps.LatLng(37.557139,-121.975332);
    var myOptions = {
        zoom: 12,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map"), myOptions);

    // between first two stops - alternate 2
    var polyLineStr = "gnfdFffngVjBkEtCpBfClBjFpDdAv@xAdAzCvBnE`DxBkFzDiJxDmJvEeLxB_FtAmDdAuBtB}D~E_JtFqKlAyBnBiCpEmFz@aAP[j@aAl@gB\eBLuADmAAsAIsAOoBg@qEqAyHeCwMQ_CAiA@w@FkAZoBbA{CfAyBp@gANWOScFmE{EaEmHcHoCuCkGoGoEkEoCsCkDgDdHuNhB{DpC}FfEqI|B}E~H_PnMaXXi@r@cA`AmAFGf@_@~CsBtAcA";

    var decodedPath = google.maps.geometry.encoding.decodePath(polyLineStr); 
    var decodedLevels = decodeLevels("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");

    var setRegion = new google.maps.Polyline({
        path: decodedPath,
        levels: decodedLevels,
        strokeColor: "#FF0000",
        strokeOpacity: 1.0,
        strokeWeight: 2,
        map: map
    });
}

function decodeLevels(encodedLevelsString) {
    var decodedLevels = [];

    for (var i = 0; i < encodedLevelsString.length; ++i) {
        var level = encodedLevelsString.charCodeAt(i) - 63;
        decodedLevels.push(level);
    }
    return decodedLevels;
}
</script> 
</head> 
<body onload="initialize()"> 
<div id="map"></div>
</body> 
</html>

I think I'm doing something wrong - but not sure what. Any help would be appreciated. This jsfiddle link shows the behaviour.

Was it helpful?

Solution

This is just an artifact of your test code. Your string literal for polyLineStr:

var polyLineStr = "gnfdFffngVjBkEtCpBfClBjFpDdAv@xAdAzCvBnE`DxBkFzDiJxDmJvEeLxB_FtAmDdAuBtB}D~E_JtFqKlAyBnBiCpEmFz@aAP[j@aAl@gB\eBLuADmAAsAIsAOoBg@qEqAyHeCwMQ_CAiA@w@FkAZoBbA{CfAyBp@gANWOScFmE{EaEmHcHoCuCkGoGoEkEoCsCkDgDdHuNhB{DpC}FfEqI|B}E~H_PnMaXXi@r@cA`AmAFGf@_@~CsBtAcA";

… has an unescaped '\e' in the middle of it. Presumably wherever you copied-and-pasted it from, it was just the actual printed string, and you turned it into a JS literal just by adding quotes around it. But in a JS literal, '\e' means e, so the backslash is lost, causing the whole path to be wrong.

The solution to this particular problem is just to escape it:

var polyLineStr = "gnfdFffngVjBkEtCpBfClBjFpDdAv@xAdAzCvBnE`DxBkFzDiJxDmJvEeLxB_FtAmDdAuBtB}D~E_JtFqKlAyBnBiCpEmFz@aAP[j@aAl@gB\\eBLuADmAAsAIsAOoBg@qEqAyHeCwMQ_CAiA@w@FkAZoBbA{CfAyBp@gANWOScFmE{EaEmHcHoCuCkGoGoEkEoCsCkDgDdHuNhB{DpC}FfEqI|B}E~H_PnMaXXi@r@cA`AmAFGf@_@~CsBtAcA";

That one-character change to your fiddle makes it give the right result.

In your actual code, this problem likely won't even arrive, as you won't have paths as literals in your source (or do anything silly like trying to generate and eval a string literal out of a string value); you'll just get them from JSON and they'll be correct.

(The funny thing is that if you'd been testing in Python, you wouldn't have noticed this problem, because '\e' would resolve to \e rather than just e anyway… which would be even more mysterious, because it wouldn't show up until you ran a test that happened to have, e.g., a '\n' in the middle.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top