Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70442

How to set coordinates of Google Map when ionic app loads using Geolocation?

$
0
0

@Sweg wrote:

am trying to load Google Maps into my Ionic app, & center the map on my current coordaintes using Geolocation.

Below I am retrieving the long & lat of my current location, & assigning the values to local variables:

constructor(private afAuth: AngularFireAuth, private afs: AngularFirestore) {
    this.anonLogin();
  }

  anonLogin() {
    this.afAuth.auth.signInAnonymously().then(res => {
      this.user = res.user;
      this.isTracking = true;
      this.watch = Geolocation.watchPosition({}, (position, err) => {
        if (position) {
          this.lat = position.coords.latitude;
          this.lng = position.coords.longitude;
        }
      });
    });
  }

And here is where I’m creating the map:

ionViewWillEnter() {
    this.loadMap();
  }

  loadMap() {
    let latLng = new google.maps.LatLng(this.lng, this.lat);
    let mapOptions = {
      center: latLng,
      zoom: 5,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);
  }

In loadMap() . this.lng & this.lat are null. But in anonLogin() , they’re the correct values.

How can I center my map on these co-ordinates when the app / map loads?

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 70442

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>