【问题标题】:With Ionic and Google Maps, how do I animate map marker movement to a new location?使用 Ionic 和 Google 地图,我如何将地图标记移动到新位置?
【发布时间】:2016-11-03 07:03:51
【问题描述】:

我有一个脚本,它首先找到用户的位置,然后按下按钮,在一定距离内放置一个随机定位的标记。接下来我需要做的是将随机放置的标记缓慢地移向用户的位置。这是一个如此新颖、无用的东西,我似乎无法在文档中的任何地方找到我想要做的事情,或者之前被问到过。我希望最终在按下按钮时总共出现三个标记,并以相同的速度独立地向用户位置移动。

我一直在使用的现有函数 - moveMarker1 - 不能解决问题。

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic', 'ngCordova'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  })
})

.config(function($stateProvider, $urlRouterProvider) {

  $stateProvider
  .state('map', {
    url: '/',
    templateUrl: 'templates/map.html',
    controller: 'MapCtrl'
  });

  $urlRouterProvider.otherwise("/");

})

.controller('MapCtrl', function($scope, $state, $cordovaGeolocation) {
  var options = {timeout: 10000, enableHighAccuracy: true};

  $cordovaGeolocation.getCurrentPosition(options).then(function(position){
    
    var latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

    var mapOptions = {
      center: latLng,
      zoom: 14,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    $scope.map = new google.maps.Map(document.getElementById("map"), mapOptions);

    //Wait until the map is loaded
    google.maps.event.addListenerOnce($scope.map, 'idle', function(){


      var marker = new google.maps.Marker({
          map: $scope.map,
          animation: google.maps.Animation.DROP,
          position: latLng
      });      

      var infoWindow = new google.maps.InfoWindow({
          content: "Your Location"
      });

      google.maps.event.addListener(marker, 'click', function () {
          infoWindow.open($scope.map, marker);
      });

      function getRandomLoc(min, max) {
        return Math.random() * (max - min) + min;
      }

      $scope.addPredators = function() {
        $scope.addPreds();
      }

      $scope.addPreds = function() {
        
        //Establish map boundaries
        var NEcornerlat = $scope.map.getBounds().getNorthEast().lat();
        var SWcornerlat = $scope.map.getBounds().getSouthWest().lat();
        var NEcornerlng = $scope.map.getBounds().getNorthEast().lng();
        var SWcornerlng = $scope.map.getBounds().getSouthWest().lng();
        
        // Create location info
        var latAdd1 = getRandomLoc(SWcornerlat, NEcornerlat);
        var lngAdd1 = getRandomLoc(SWcornerlng, NEcornerlng);
        var predLat1 = Number(latAdd1);
        var predLng1 = Number(lngAdd1);

        // Determine if predator location is too close to user
        if (Math.abs(latLng.lat() - predLat1) < 0.0075 && Math.abs(latLng.lng() - predLng1) < 0.0075) {
          console.log("too close");
          if (latLng.lat() > predLat1) {
            predLat1 = Number(predLat1) - 0.005;
          } else if (latLng.lat() < predLat1) {
            predLat1 = Number(predLat1) + 0.005;
          }
          if (latLng.lng() > predLng1) {
            predLng1 = Number(predLng1) - 0.005;
          } else if (latLng.lng() < predLng1) {
            predLng1 = Number(predLng1) + 0.005;
          }
        }

        //Place predMarker1 on map
        var predMarker1 = new google.maps.Marker({
          map: $scope.map,
          animation: google.maps.Animation.DROP,
          position: new google.maps.LatLng(predLat1, predLng1)
        });
        
        function moveMarker1() {
          if (predMarker1.position !== latLng) {
            console.log("NOT THERE");

            // Figure out if it's above or below lat, move it
            if (predLat1 < latLng.lat()) {
              console.log("Lat is below");
              //Lat is below, move it up
              function setMarkerPosition() {
                  predMarker1.lat - 0.005;
                  predMarker1.setPosition(
                      new google.maps.LatLng(
                          predMarker1.lat,
                          predMarker1.lng)
                  );
              }
              setMarkerPosition();
              setTimeout(moveMarker1, 1000);
              map.clearOverlays(predMarker1);
            } else {
              console.log("Lat is above");
              predMarker1.lat + 0.005;
              setTimeout(moveMarker1, 5000);
            }

          }
        }

        moveMarker1();


        console.log(predLat1, predLng1);
          // setInterval($scope.placecheck, 1500);
      };

    });

  }, function(error){
    console.log("Could not get location");
  });

});

【问题讨论】:

    标签: google-maps google-maps-api-3 ionic-framework


    【解决方案1】:

    最近我遇到了这个问题。主要概念非常简单,它的慢动作场景,将点缓慢移动到目标位置。然后我使用了这个技巧,我忘记了链接,这是我的工作代码:

    numDeltas: any = 100;
    delay = 10; //milliseconds
    i = 0;
    deltaLat;
    deltaLng;
    moveTo(lastPositionLat, lastPositionLng) {
        if (this.lastPositionLat != undefined && this.lastPositonLng != undefined) {
            this.i = 0;
            console.log('pos lat: ' + lastPositionLat);
            console.log('pos lng: ' + lastPositionLng);
            //Calculating gap between latitudes and divide by latitude step count, calculating how much to be latitude step
            this.deltaLat = (lastPositionLat - this.lastPositionLat) / this.numDeltas;
            //Calculating gap between longitudes and divide by longitude step count, calculating how much to be longitude step
            this.deltaLng = (lastPositionLng - this.lastPositonLng) / this.numDeltas;
            console.log(this.deltaLat + ' ' + this.deltaLng);
            this.moveMarker();
        }
    }
    
    interval: any;
    liveDots: any = [];
    moveMarker() {
        if (this.i != this.numDeltas) {
            this.i++;
    
            console.log('posLat' + this.i + ': ' + this.lastPositionLat);
            console.log('posLng' + this.i + ': ' + this.lastPositonLng);
            //Moving to target latitude step by latitude step
            this.lastPositionLat += this.deltaLat;
            //Moving to target longitute step by longitude step
            this.lastPositonLng += this.deltaLng;
            var latlng = new google.maps.LatLng(this.lastPositionLat, this.lastPositonLng);
            this.lastMarker.setPosition(latlng);
            this.map.setCenter(latlng);
            this.liveDots.push(latlng);
            this.addLine();
    
            let inter = setInterval(() => {
                this.moveMarker(); // Now the "this" still references the component
    
                if (this.i == this.numDeltas) {
                    clearInterval(inter);
                }
            }, this.delay);
        }
    }
    
    //Drawing line by certain step dots
    addLine() {
        var lineCoordinatesPath = new google.maps.Polyline(
            {
                path: this.liveDots,
                geodesic: true,
                strokeColor: '#007bff'
            }
        );
        lineCoordinatesPath.setMap(this.map);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      • 2013-04-19
      • 1970-01-01
      • 2017-10-12
      • 2011-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多