InvalidValueError:在属性origin中:不是google map

时间:2015-11-24 07:10:49

标签: google-maps compiler-errors google-maps-markers marker

我有一个应用程序,我必须绘制折线并在谷歌地图上放置一些标记,为了绘制折线,我从数据库获取数据每一件事都运行正常,但我在开发人员控制台收到错误具有 InvalidValueError:在属性origin中:不是字符串;而不是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应用程序我的完整控制器如下所示

// ....地图控制器.........

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);
    }
  }

});

1 个答案:

答案 0 :(得分:1)

您正在运行数组的末尾。 app/code/local/Webshop/controllers/EchoController.php 不能等于或大于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);
}

proof of concept fiddle