Google地图:显示编码折线

时间:2011-05-08 21:55:46

标签: google-maps

好的,所以我有一个多边形,表示为编码折线。我想把它放在地图上,但无法弄清楚语法。这就是我得到的:

  setRegion = new google.maps.Polyline({
    locations: "}~kvHmzrr@ba\hnc@jiu@r{Zqx~@hjp@pwEhnc@zhu@zflAbxn@fhjBvqHroaAgcnAp}gAeahAtqGkngAinc@_h|@r{Zad\y|_D}_y@swg@ysg@}llBpoZqa{@xrw@~eBaaX}{uAero@uqGadY}nr@`dYs_NquNgbjAf{l@|yh@bfc@}nr@z}q@i|i@zgz@r{ZhjFr}gApob@ff}@laIsen@dgYhdPvbIren@",
    levels: "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
    strokeColor: "#FF0000",
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: "#FF0000",
    fillOpacity: 0.35
  });

  setRegion.setMap(map);

我使用Polyline Encoder Tool创建的。从这个页面我得到了它的使用,这是:

  

折线编码将出现在   编码折线和编码   级别字段如下。使用这些值   您的位置级别   创建你的google.maps.Polyline

但是,多边形不会出现。有人知道出了什么问题吗?

更新

我试过这个,但收到错误Uncaught TypeError: Cannot read property 'encoding' of undefined

<!doctype html> 
<html> 
<head> 
<title>Test</title> 
<meta charset="iso-8859-1"> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?libraries=geometry&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(51.65905179951626, 7.3835928124999555);
    var myOptions = {
        zoom: 8,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map"), myOptions);
    var decodedPath = google.maps.geometry.encoding.decodePath("}~kvHmzrr@ba\hnc@jiu@r{Zqx~@hjp@pwEhnc@zhu@zflAbxn@fhjBvqHroaAgcnAp}gAeahAtqGkngAinc@_h|@r{Zad\y|_D}_y@swg@ysg@}llBpoZqa{@xrw@~eBaaX}{uAero@uqGadY}nr@`dYs_NquNgbjAf{l@|yh@bfc@}nr@z}q@i|i@zgz@r{ZhjFr}gApob@ff}@laIsen@dgYhdPvbIren@");
    var decodedLevels = decodeLevels("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");

    setRegion = new google.maps.Polyline({
        locations: decodedPath,
        levels: decodedLevels,
        strokeColor: "#FF0000",
        strokeOpacity: 1.0,
        strokeWeight: 2
    });
    setRegion.setMap(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>

4 个答案:

答案 0 :(得分:49)

google.maps.geometry.encoding.decodePath(encodedPath:字符串)

http://code.google.com/apis/maps/documentation/javascript/reference.html#encoding

从该页面开始:

var decodedPath = google.maps.geometry.encoding.decodePath(encodedPolyline);
var decodedLevels = decodeLevels(encodedLevels);

// Decode an encoded levels string into an array of levels.
function decodeLevels(encodedLevelsString) {
  var decodedLevels = [];

  for (var i = 0; i < encodedLevelsString.length; ++i) {
    var level = encodedLevelsString.charCodeAt(i) - 63;
    decodedLevels.push(level);
  }

  return decodedLevels;
}

确保按照http://code.google.com/apis/maps/documentation/javascript/basics.html#Libraries

加载几何体libaray

编码折线的工作示例:http://jsfiddle.net/ryanrolds/ukRsp/

答案 1 :(得分:13)

如果您收到此错误:

Uncaught TypeError: Cannot read property 'encoding' of undefined

然后没有加载Geometry库。将几何库(&amp; libraries = geometry)添加到google maps api load中,如下所示:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=geometry"></script>

答案 2 :(得分:1)

我不知道为什么,但如果你将编码的折线和/或水平存储在变量中(比如折线数组的元素),则需要String()函数:

polyLayer = [
[['ynh`IcftoCyq@Ne@ncBds@EEycB'],
 ['PHIHP']],
...
];

...
for (var i = 0; i < polyLayer.length; i++) {
    poly = new google.maps.Polyline({
        ...,
        path: google.maps.geometry.encoding.decodePath(String(polyLayer[i][0])), 
        levels: decodeLevels(String(polyLayer[i][1])), 
        ...
    }); 
    poly.setMap(map);
}

答案 3 :(得分:0)

您有位置:,而不是路径:在您的选项中!我注意到这是因为生成器工具声明使用位置,但我认为这是不正确的,不确定,因为我也是新手!它似乎适用于路径。