Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ionic framework 在ionic中使用watchposition更新google标记_Ionic Framework_Ionic4 - Fatal编程技术网

Ionic framework 在ionic中使用watchposition更新google标记

Ionic framework 在ionic中使用watchposition更新google标记,ionic-framework,ionic4,Ionic Framework,Ionic4,我正在创建一张地图来跟踪用户位置,我正在使用@geolocation插件并使用以下方式显示地图: <script src="https://maps.googleapis.com/maps/api/js?key=API_KEYscript> import { Component, ViewChild, ElementRef } from '@angular/core'; import { Geolocation, GeolocationOptions,

我正在创建一张地图来跟踪用户位置,我正在使用@geolocation插件并使用以下方式显示地图:

    <script src="https://maps.googleapis.com/maps/api/js?key=API_KEYscript>

     import { Component, ViewChild, ElementRef } from '@angular/core';
     import { Geolocation, GeolocationOptions, Geoposition, PositionError } from '@ionic-native/geolocation';
     declare var google;

@Component({
  selector: 'app-tab1',
  templateUrl: 'tab1.page.html',
  styleUrls: ['tab1.page.scss']
})
export class Tab1Page {

  geolocation = Geolocation;
  options: GeolocationOptions;
  currentPos: Geoposition;
  @ViewChild('map', { static: false }) mapElement: ElementRef;
  map: any;

  constructor() { }

  ionViewDidEnter() {
    this.getUserPosition();
  }

  getUserPosition() {
    this.options = {
      enableHighAccuracy: false
    };
    //get location
    this.geolocation.getCurrentPosition(this.options).then((pos: Geoposition) => {
      this.currentPos = pos;
      console.log(pos);

      //add map
      let latLng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
      let mapOptions = {
        center: latLng,
        zoom: 18,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);
      this.addMarker(latLng);
    }, (err: PositionError) => {
      console.log("error : " + err.message);
    })

    let subscription = this.geolocation.watchPosition().subscribe(position =>{
      let userPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
          this.addMarker(userPosition);
    })
  }

  addMarker(position) {
    let marker = new google.maps.Marker({
      map: this.map,
      animation: google.maps.Animation.DROP,
      position: position
    });
    let content = "<p>This is your current position !</p>";
    let infoWindow = new google.maps.InfoWindow({
      content: content
    });
    google.maps.event.addListener(marker, 'click', () => {
      infoWindow.open(this.map, marker);
    });
  }

}