Карты Google: отображение кодированной полилинии
ОК, поэтому у меня есть многоугольник, который представлен как закодированная полилиния. Я хотел бы поместить это на карту, но не могу понять синтаксис. Вот что я получил:
setRegion = new google.maps.Polyline({
locations: "}[email protected]\[email protected]@r{[email protected]@[email protected]@[email protected]}[email protected]_h|@r{Zad\y|_D}[email protected]@[email protected]}llBpoZqa{@[email protected]~eBaaX}{[email protected]}[email protected]`dYs_NquNgbjAf{[email protected]|[email protected]@}[email protected]}[email protected]|[email protected]@r{ZhjFr}[email protected]}@[email protected]@",
levels: "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF0000",
fillOpacity: 0.35
});
setRegion.setMap(map);
Что я создал с помощью Инструмент Polyline Encoder. С этой же страницы я получил ее использование, которое:
Количественная полилиния будет отображаться в закодированная полилиния и кодировка Поля уровней ниже. Используйте эти значения для местоположений и уровней, когда вы создайте свою 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("}[email protected]\[email protected]@r{[email protected]@[email protected]@[email protected]}[email protected]_h|@r{Zad\y|_D}[email protected]@[email protected]}llBpoZqa{@[email protected]~eBaaX}{[email protected]}[email protected]`dYs_NquNgbjAf{[email protected]|[email protected]@}[email protected]}[email protected]|[email protected]@r{ZhjFr}[email protected]}@[email protected]@");
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>
Ответы
Ответ 1
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;
}
Убедитесь, что загружается геометрия libaray за: http://code.google.com/apis/maps/documentation/javascript/basics.html#Libraries
Рабочий пример закодированной полилинии: http://jsfiddle.net/ryanrolds/ukRsp/
Ответ 2
Если вы получаете эту ошибку:
Uncaught TypeError: Cannot read property 'encoding' of undefined
Тогда библиотека геометрии не загружается. Добавьте библиотеку геометрии (& library = geometry) к загрузке api-карт google, как в:
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=geometry"></script>
Ответ 3
Я не знаю почему, но если вы сохраняете свои закодированные полилинии и/или уровни в переменных (например, элементы массива полилиний), требуется функция String():
polyLayer = [
[['ynh`[email protected]@[email protected]'],
['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);
}
Ответ 4
у вас есть местоположения:, а не путь: в ваших вариантах! Я заметил это, поскольку инструмент генератора указывает на использование местоположений, но я думаю, что это неверно, не уверен, поскольку я новичок в этом! Кажется, он работает с путём.