【问题标题】:How to retain the zoom and refresh after setinterval using google maps如何在使用谷歌地图设置间隔后保留缩放和刷新
【发布时间】:2018-03-27 15:37:49
【问题描述】:

您好,我正在使用 angular web 开发跟踪应用程序。每 30 秒我能够从服务器渲染数据,我的问题是每当我更新坐标时,它会刷新自动缩放我的意思是如果我正在查看在 setinterval init 之后放大地图以缩放:16 它显示缩放级别@10 然后我如何在刷新后保持缩放和居中以及 有什么方法可以在不使用设置间隔的情况下更新标记 下面是我的代码

{
    "page": 2,

    "data": [
        {
            "id": 4,
            "first_name": "Eve",
            "last_name": "Holt",
            "lat":"25.6599899",
            "lng":"45.3664646",
            "status":"0"

        },
        {
            "id": 5,
            "first_name": "Charles",
            "last_name": "Morris",
            "lat":"25.99899",
            "lng":"45.4646",
             "status":"1"

        },
        {
            "id": 6,
            "first_name": "Tracey",
            "last_name": "Ramos",
            "lat":"25.2339899",
            "lng":"45.56664646",
            "status":"1"

        }
    ]
}

import {Component, OnDestroy, OnInit, ViewChild, ViewContainerRef} from '@angular/core';
import {DataService} from '../service/data.service';
import {Router} from '@angular/router';
import { NgxSpinnerService } from 'ngx-spinner';
import {Alert} from 'selenium-webdriver';






declare var google: any;

@Component({
  selector: 'app-tracking',
  templateUrl: './tracking.component.html',
  styleUrls: ['./tracking.component.css']
})
export class TrackingComponent implements OnInit, OnDestroy {


  public LocData: any;
  public DriversData: any;
  public location: any;
  public drivers: any;
  public mapObject: any;
  public markers: any = [];
  mapData: any;

  markerName: any;


  timer: any;

  response: any;
   public zoomlevel:any=13;

  public latlng:any={lat:25.204849,lng:55.270783};

  public locLatlng:any;
  public loczoomlevel:any;

  public getloclng:any;
  public getzoomlevel:any;
  public mapOptions:any;
  public mapCenter:any;




  constructor(public serv: DataService, private router: Router, public spinner: NgxSpinnerService) {

    // this is for loading the locations data
    this.spinner.show();
    this.serv.LoadLocation().subscribe(res => {
      this.spinner.hide();
      this.LocData = res.Data;
     // console.log(this.LocData);
      this.location = this.LocData[0].Id;
      this.onLoChange(this.location);
    }, err => {
      this.spinner.hide();

    });

   //
    this.timer = setInterval(() => {this.getMapData(this.drivers, this.location); }, 30000);




    this.getMapData(this.drivers, this.location);



  }


  onLoChange(data) {
   // console.log('Location', data);
    this.driverLoadData(data);
    //this.location = event.target.value;
    this.getMapData(this.drivers, this.location);
  }

  onDriverChange(event) {
    //console.log('Driver Id', event.target.value);
    this.drivers = event.target.value;
    this.getMapData(this.drivers, this.location);

  }

  driverLoadData(data) {
    this.spinner.show();
    this.serv.LoadDrivers(data).subscribe(res => {
      this.spinner.hide();
      //console.log(res);
      this.DriversData = res.Data;

      this.drivers = this.DriversData[0].Id;

    }, err => {
      this.spinner.hide();
      alert('Unable to load Driver Data');
    });

  }





  getMapData(data, dataOne) {
   // this.spinner.show();
    this.serv.getMapData(data, dataOne).subscribe(res => {
      //this.spinner.hide();
      this.deleteMarkers();



      debugger;

      if (res.Data && res.Data.length > 0) {
        // do something
        this.mapData = res.Data;
       // console.log(JSON.stringify(this.mapData));
        // rendering markers

        if (this.mapData != null && this.mapData.length > 0) {
          for (let i = 0; i < this.mapData.length; i++) {
            this.latlng = {lat: parseFloat(this.mapData[i].latitude), lng: parseFloat(this.mapData[i].longitude)};
            this.addMarker(this.latlng, this.mapObject, this.mapData[i].Name);
            this.markerName = this.mapData[i].Name;


          }
        }
      } else {

       // this.response = 'No Data Exist';
        alert('No Data Exist');
      }

    }, err => {
      //this.spinner.hide();
      alert('Unalbe to display data');
    });


  }

  addMarker(latlng, mapobj, markerLabel) {
    var marker = new google.maps.Marker({
      position: latlng,
      label: '',
      map: mapobj,
//animation: google.maps.Animation.DROP,
    });




    const infowindow = new google.maps.InfoWindow({
      content: markerLabel
    });

    google.maps.event.addListener(marker, 'click', function() {
     // infowindow.open(Map,marker);
    });

    infowindow.open(Map, marker);





    const styless = [
      {
        "featureType": "poi.attraction",
        "stylers": [
          {
            "visibility": "off"
          }
        ]
      },

      {
        "featureType": "poi.medical",
        "stylers": [
          {
            "visibility": "off"
          }
        ]
      },
      {
        "featureType": "poi.park",
        "elementType": "labels.text",
        "stylers": [
          {
            "visibility": "off"
          }
        ]
      },
      {
        "featureType": "poi.place_of_worship",
        "stylers": [
          {
            "visibility": "off"
          }
        ]
      },
      {
        "featureType": "poi.school",
        "stylers": [
          {
            "visibility": "off"
          }
        ]
      },
      {
        "featureType": "poi.sports_complex",
        "stylers": [
          {
            "visibility": "off"
          }
        ]
      }
    ]

    mapobj.setOptions({styles: styless});




    // This is for set postion for the marker after getting dynamic data it posittions to the point
   mapobj.setZoom(14);
   mapobj.panTo(marker.position);
    this.markers.push(marker);

  }





// Sets the map on all markers in the array.
  setMapOnAll(map) {
    for (let i = 0; i < this.markers.length; i++) {
      this.markers[i].setMap(map);
      this.markers[i].setPosition(this.markers[i].position);

    }
  }

  // Removes the markers from the map, but keeps them in the array.
  clearMarkers() {
    this.setMapOnAll(null);
  }


  // Deletes all markers in the array by removing references to them.
  deleteMarkers() {
    this.clearMarkers();
    this.markers = [];
  }





  ngOnInit() {


   this.mapOptions ={

      zoom:this.zoomlevel,
      center: this.latlng,
      gestureHandling: 'greedy'
   }

    this.mapObject = new google.maps.Map(document.getElementById('googleMap'),this.mapOptions );

    var trafficLayer = new google.maps.TrafficLayer();
    trafficLayer.setMap(this.mapObject);

    this.addMarker(this.latlng, this.mapObject, 'Current Location');



  }

  ngOnDestroy() {
    clearInterval(this.timer);
  }




}

【问题讨论】:

    标签: javascript angular google-maps google-maps-api-3


    【解决方案1】:

    检查event-properties 并使用map.getZoom() 动态获取缩放级别。 更新您的 addMarker 函数

      addMarker(latlng, mapobj, markerLabel,zoomLevel){
        var marker = new google.maps.Marker({
          position: latlng,
          label: '',
          map: mapobj,
          zoom:zoomLevel,
          //animation: google.maps.Animation.DROP,
        });
      }
    

    在循环内更新getMapData(data, dataOne)

    if (this.mapData != null && this.mapData.length > 0) {
     this.zoomlevel=this.mapObject.getZoom();
      for (let i = 0; i < this.mapData.length; i++) {
        this.latlng = {
          lat: parseFloat(this.mapData[i].latitude),
          lng: parseFloat(this.mapData[i].longitude)
        };
        this.addMarker(this.latlng, this.mapObject, this.mapData[i].Name,this.zoomlevel);
        this.markerName = this.mapData[i].Name;
      }
    }
    

    更新您的ngOnInit

    this.addMarker(this.latlng, this.mapObject, 'Current Location',this.zoomlevel);
    

    stackblitz演示

    使用 OP 数据更新演示

    Demo

    【讨论】:

    • 实际上这里发生的事情是每隔 15 秒动态放置标记,我调用 getmapdata() 以便每 15 秒更新一次数据,这里的问题是假设放置了 10 个标记 n当我放大到特定标记时,半径为 2-10 公里的地图可以,但刷新会发生 15 秒,因此当标记刷新时,地图会闪烁/将位置更改为最大标记,而不是当前的标记看到
    • 它应该关注循环中的最后一个标记,而不是你当前看到的内容
    • 是的,请使用动态数据检查更新后的问题,假设我有 1,2,3,4,5 个标记,如果我专注于第 4 个焦点,则第 4 个标记正在移动,当数据更新时,则焦点应该只是这个,而不是现在我的地图正在闪烁它的位置到最大标记
    • 目前,当我的更新发生时,它正在跳跃位置,就像突然拖动它的位置有什么办法可以顺利移动
    • getPosition() 检查developers.google.com/maps/documentation/javascript/… 并使用this.mapObject.panTo(this.marker.getPosition()); 标记您要聚焦的标记
    猜你喜欢
    • 1970-01-01
    • 2021-06-26
    • 2012-11-26
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    相关资源
    最近更新 更多