0

当使用 google Directions api 并将替代项设置为 true 时,一些建议的路线会越过公园。其他人是否观察到了这种行为?以下是我点击的网址,

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

它返回以下带有 3 个替代路由的 json 响应,

{
   "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"
}

我在javascript中为每个路线对象使用overview_polyline来查看地图中的折线。我将使用后端python应用程序的API,而javascript目前仅用于交叉检查,以防万一您想知道为什么我不使用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>

我想我做错了什么——但不确定是什么。任何帮助,将不胜感激。这个jsfiddle 链接显示了行为。

4

1 回答 1

0

这只是您的测试代码的工件。您的字符串文字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";

'\e'…中间有一个未转义的。大概无论您从哪里复制粘贴它,它只是实际打印的字符串,您只需在其周围添加引号即可将其转换为 JS 文字。但是在 JS 文字中,'\e'表示e,所以反斜杠丢失了,导致整个路径错误。

这个特定问题的解决方案就是逃避它:

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";

对小提琴的单字符更改使其产生正确的结果。

在您的实际代码中,这个问题甚至可能不会出现,因为您的源代码中不会有路径作为文字(或者做任何愚蠢的事情,比如尝试从字符串值生成和评估字符串文字);您只需从 JSON 中获取它们,它们就会是正确的。

(有趣的是,如果你一直在用 Python 进行测试,你就不会注意到这个问题,因为它'\e'会解决\e而不是e无论如何……这会更加神秘,因为它在你运行之前不会出现碰巧有一个测试,例如,a'\n'在中间。)

于 2013-08-28T23:21:39.740 回答