2013-01-31 67 views
1

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

我使用的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 

我则继续存储这个字符串在MySQL DB使用中文文本类型。

第二张地图查询数据库以获得编码的折线字符串,这与我最初保存的一样。

我然后使用从几何库中的解码功能解码串并创建折线,像这样:

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类型。同样的结果。

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

+0

什么是[线](http://www.geocodezip.com/v3_GenericMapBrowser.asp?filename=SO_Polyline_encoded.xml)应该是什么样子的? – geocodezip

+0

如果你插入这里,线条看起来是否正确? https://developers.google.com/maps/documentation/utilities/polylineutility – Rick

+0

原始行看起来正确,但从数据库查询的行不。我测试了几行,发现“\ b”没有存储在数据库中,这是我的问题。解决方案似乎是像“\\ ​​b”一样逃避它。还有哪些角色需要转义? – ehenkle

回答

0

(通过在一个问题编辑OP回答转换为一个社区维基答案见Question with no answers, but issue solved in the comments (or extended in chat)。)

的OP写道:

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

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