【问题标题】:Google maps v3 error drawing encoded polyline谷歌地图 v3 错误绘制编码折线
【发布时间】:2013-01-31 23:30:50
【问题描述】:

我有一张地图,用户可以在其中绘制折线,对其进行编码,然后将其保存到 mySQL 数据库中。然后,还有另一个地图从数据库中查询编码字符串,解码折线并将其显示在地图上。

我正在使用几何库中的encodePath() 函数,如下所示:

    if(overlayType == 'POLYGON'){
        newPolygonOverlay = overlaysArray[0];   //only one overlay
        newPolygonPath = newPolygonOverlay.getPath();    //get the path
        newPolygonPathCoordsStr = google.maps.geometry.encoding.encodePath(newPolygonPath);    //encode it

这段代码会产生这样的字符串:

    wwzhBz~|gP~mhMygvCt`cDjgiH{ohIf{fA

然后我继续使用 mediumtext 类型将此字符串存储在 mySQL DB 中。

第二张图,查询数据库,得到编码后的折线字符串,和我原来保存的一模一样。

然后我使用几何库中的 decode 函数对字符串进行解码并创建折线,如下所示:

    var array = google.maps.geometry.encoding.decodePath(str);  //str contains the encoded string from the db
    var levels = decodeLevels("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");

    var Poly = new google.maps.Polyline({
      path: array,
      levels: levels,
      strokeColor: '66FF00',
      strokeOpacity: 1.0,
      strokeWeight: 2
    });

    flightPath.setMap(map);

我的问题是,在途中的某个地方,一些折线点弄乱了,生成的折线与原来的不一样。

我尝试在 mySQL 中使用 blob 类型。结果一样。

我尝试通过在第二个映射上用编码字符串声明一个字符串变量来绕过数据库。结果一样。

【问题讨论】:

  • line 应该是什么样子?
  • 如果您在此处插入线路看起来是否正确? developers.google.com/maps/documentation/utilities/…
  • 原始行看起来正确,但从数据库查询的行不正确。我测试了几行,发现“\b”没有存储在数据库中,这是我的问题。解决方案似乎是像“\\b”一样逃避它。还有哪些字符需要转义?

标签: google-maps-api-3 google-polyline


【解决方案1】:

(由 OP 在问题编辑中回答。转换为社区 wiki 答案。请参阅Question with no answers, but issue solved in the comments (or extended in chat)

OP 写道:

问题是反斜杠字符被 javascript 解释(和删除)。因此,当编码字符串到达​​数据库时,它不再包含反斜杠,从而使折线成为完全不同的折线。我尝试用双反斜杠替换单个反斜杠:“\”->“\”,但是在我对编码函数进行替换之前,我遇到了麻烦;像这样:

newRoutePathCoordsStr = google.maps.geometry.encoding.encodePath(newRoutePath).replace(/\\/g,"\\\\");

【讨论】:

    猜你喜欢
    • 2013-07-02
    • 2014-04-14
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 2012-10-18
    • 2023-04-06
    相关资源
    最近更新 更多