Javascript 谷歌地图-无法绘制多边形和线字符串
我正在使用平均堆栈和谷歌地图Api构建一个应用程序,在绘制多边形和线串时遇到了一个小问题 我有一组几何图形(点、多边形和线串),每次我找到其中一个时,我都需要将其正确地添加到地图中 在渲染标记时我没有问题,但在多边形和线串时我有问题 这是我的代码,其中包含初始化映射的逻辑Javascript 谷歌地图-无法绘制多边形和线字符串,javascript,angularjs,google-maps,google-maps-api-3,Javascript,Angularjs,Google Maps,Google Maps Api 3,我正在使用平均堆栈和谷歌地图Api构建一个应用程序,在绘制多边形和线串时遇到了一个小问题 我有一组几何图形(点、多边形和线串),每次我找到其中一个时,我都需要将其正确地添加到地图中 在渲染标记时我没有问题,但在多边形和线串时我有问题 这是我的代码,其中包含初始化映射的逻辑 // Initializes the map function initialize(latitude, longitude, filter) { // If map has not been created...
// Initializes the map
function initialize(latitude, longitude, filter) {
// If map has not been created...
if (!map) {
// Create a new map and place in the index.html page
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: new google.maps.LatLng(vm.selectedLat, vm.selectedLong)
});
}
// Loop through each location in the array and place a geometry
locations.forEach(function (n) {
if(n.type === 'LineString'){
console.log('LineString '+JSON.stringify(n.coords));
var linestring = new google.maps.Polyline({
position: n.coords,
map: map,
geodesic: true,
strokeColor: '#0404B4',
strokeOpacity: 1.0,
strokeWeight: 2
});
// For each linestring created, add a listener
google.maps.event.addListener(linestring, 'click', function () {
// When clicked, open the selected linestring's message
n.message.open(map, linestring);
});
linestring.setMap(map);
}
if(n.type === 'Polygon'){
console.log('Polygon '+JSON.stringify(n.coords));
var polygon = new google.maps.Polygon({
path: n.coords,
geodesic: true,
strokeColor: '#0404B4',
strokeOpacity: 0.8,
strokeWeight: 3,
fillColor: '#0404B4',
fillOpacity: 0.35
});
// For each polygon created, add a listener
google.maps.event.addListener(polygon, 'click', function () {
// When clicked, open the selected polygon's message
n.message.open(map, polygon);
});
polygon.setMap(map);
}
});
};
以下是坐标的两个console.log()
的屏幕截图
正如你从截图上看到的,我也得到了
InvalidValueError:索引0处:不是LatLng或LATLNERAL:在属性lat:不是数字中
我试图找到解决办法,但没有成功
为什么我不能绘制这些几何图形?如何解决InvalidValueError?
提前感谢。问题:
没有google.maps.Polyline
属性,它有一个position
属性,该属性接受一个path
数组(看起来像是传递到google.maps.LatLngLiterals
属性的内容)position
具有一个google.maps.Polygon
属性,该属性接受一个paths
数组,但您的数据格式不正确(并且您使用的是google.maps.LatLngLiterals
而不是path
)。它是一个由路径
组成的数组,{lat:[46.774,-48.19],lng:[46.466,-29.119]}
和lat
都不是数字,而是数组lng