gpt4 book ai didi

google-maps - InvalidValueError : in property origin: not a string in google map

转载 作者:行者123 更新时间:2023-12-02 11:09:12 33 4
gpt4 key购买 nike

我有一个应用程序,在其中我必须绘制折线并在Google map 上放置一些标记,以便绘制折线,我从数据库中获取数据,一切正常,但在开发人员控制台中却出现错误
InvalidValueError:属性来源中:不是字符串;而不是LatLng或LatLngLiteral:不是对象;不是对象

在尝试调试了很长时间之后,我发现绘制折线的方式存在一些问题(即,数组中存在一些未定义的值,请在for循环中查看)

for (var i = 0; i <lat_lng.length+2; i++) {
var src = lat_lng[i];
var des = lat_lng[i + 1];
var k=i;
i=i+1;
getDirections(src, des, colorVariable[k%colorVariable.length], map);
}

这是angularjs应用程序,我的完整 Controller 如下所示

//....map Controller .........
changiControllers.controller("map", function ($scope,$http) {
$(".calendarMain .selectDay").dateSelectSlider();
$(".timeSlider").timeSlider();
$scope.jsonData=[ {
"title": 'point11',
"lat": '1.351477',
"lng": '103.985701',
"description": 'uuu'
}, {
"title": 'point12',
"lat": '1.350265',
"lng": '103.985165',
"description": 'uuu'
}];
$scope.markerData=[{
"title": 'point3',
"lat": '1.354432',
"lng": '103.987262',
"description": 'zzz'
}];
socket.on('plotData', function (dataMap) {
$scope.jsonData=[];
$scope.markerData=[];
for(var i = 0;i<dataMap.data.length;i++){
if(i==3||i==6){
$scope.markerData.push(dataMap.data[i]);
}
$scope.jsonData.push(dataMap.data[i]);
}
$scope.$apply();
$scope.laneMapInit();
});


$scope.laneMapInit=function() {
var gmarkers = [];
var colorVariable = ["yellow", "green", "red"];
var map;
var mapOptions = {
center: new google.maps.LatLng(1.35, 103.987),
zoom: 17,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: true,
draggable: true,
disableDefaultUI: false,
heading: 90,
tilt: 0,
styles: [
{
"featureType": "poi",
"stylers": [
{ "visibility": "off" }
]
}
]
};
map = new google.maps.Map(document.getElementById("laneMap"), mapOptions);
var data= $scope.jsonData;
var lat_lng = new Array();
var markerModel= $scope.markerData;
var infoWindow = new google.maps.InfoWindow();
var latlngbounds = new google.maps.LatLngBounds();

for ( var a = 0; a < data.length; a++) {
var myLatlng = new google.maps.LatLng(data[a].lat, data[a].lng);
lat_lng.push(myLatlng);
}
for (var j = 0; j < markerModel.length; j++) {
var myLatlngMarker = new google.maps.LatLng(markerModel[j].lat, markerModel[j].lng);
var marker = new google.maps.Marker({
position: myLatlngMarker,
map: map,
icon: {
url:'images/liveCam32.png',
},
title: markerModel[j].title
});
latlngbounds.extend(marker.position);
(function(marker, markerModel) {
google.maps.event.addListener(marker, "click", function(e) {
infoWindow.setContent(markerModel.description);
infoWindow.open(map, marker);
});
})(marker, markerModel[j]);
gmarkers.push(marker);
}
map.setCenter(latlngbounds.getCenter());

for (var i = 0; i <lat_lng.length+2; i++) {
var src = lat_lng[i];
var des = lat_lng[i + 1];
var k=i;
i=i+1;
getDirections(src, des, colorVariable[k%colorVariable.length], map);
}
function getDirections(src, des, color, map) {
//Intialize the Direction Service
var service = new google.maps.DirectionsService( {preserveViewport: true} );
service.route({
origin: src,
destination: des,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
//Intialize the Path Array
var path = [];
for (var i = 0; i < result.routes[0].overview_path.length; i++) {
path.push(result.routes[0].overview_path[i]);
}
//Set the Path Stroke Color
var polyOptions = {
strokeColor: color,
strokeOpacity: 1.0,
strokeWeight: 8,
path: path,
map: map
}
poly = new google.maps.Polyline(polyOptions);
poly.setMap(map);

}
});
}
};

$scope.laneMapInit();

$scope.simulateMapDraw = function(flag){
if(flag){
$scope.mapPlotIntervalId=setInterval(function() {
$http.get('http://localhost:3000/newPlotMsg').then(function(result){
});
},4000);
}else if(!flag){
clearInterval($scope.mapPlotIntervalId);
}
}

});

最佳答案

您正在运行超出数组末尾的位置。 i不能等于或大于lat_lng.length,请更改:

for (var i = 0; i <lat_lng.length+2; i++) {
var src = lat_lng[i];
var des = lat_lng[i + 1];
var k=i;
i=i+1;
getDirections(src, des, colorVariable[k%colorVariable.length], map);
}

至:
for (var i = 0; i < lat_lng.length; i++) {
var src = lat_lng[i];
var des = lat_lng[i + 1];
var k=i;
i=i+1;
getDirections(src, des, colorVariable[k%colorVariable.length], map);
}

proof of concept fiddle

关于google-maps - InvalidValueError : in property origin: not a string in google map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33887438/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com