Google地图:显示编码折线

25 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);
Run Code Online (Sandbox Code Playgroud)

我使用Polyline编码器工具创建的.从同一页面我得到了它的用处,即:

折线编码将显示在下面的编码折线和编码级别字段中.在创建google.maps.Polyline时,请将这些值用于位置级别

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

UPDATE

我试过这个,但得到了错误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>
Run Code Online (Sandbox Code Playgroud)

Rya*_*lds 50

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;
}
Run Code Online (Sandbox Code Playgroud)

确保按以下方式加载几何体libaray:http://code.google.com/apis/maps/documentation/javascript/basics.html#Libraries

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


ksi*_*elo 13

如果您收到此错误:

Uncaught TypeError: Cannot read property 'encoding' of undefined
Run Code Online (Sandbox Code Playgroud)

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

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=geometry"></script>
Run Code Online (Sandbox Code Playgroud)