ionic - save latitude and longitude on firebase

2

I created a project in ionic and I would like that when the user clicked on "save location", the position where he left the marker was saved in his user profile in firebase, however, I have no idea how to get the latitude and the length of the marker and save in the firebase, any solution ???

      loadMap(){
        this.geolocation.getCurrentPosition().then((position)=> {
          let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); // pegando localização atual

          let mapOptions = { //opções do mapa
            center: latLng,
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            disabledZoomDoubleClick: true,
            fullscreenControl: true
          }
          this.map = new google.maps.Map(document.getElementById('map'), mapOptions); //adicionando mapa com as opçoes

          let marker = new google.maps.Marker({ //Adicionando marcador
          map: this.map,
          animation: google.maps.Animation.DROP,
          position: latLng,
          draggable: true,

        });

        let data = {
          lat: null,
          lng: null
        }

        google.maps.event.addListener(marker, 'click', function(event){ //Aciona quando o usuario clica no marcador
          data.lat = event.latLng.lat();
          data.lng = event.latLng.lng();
        });

        }, (error) => {
          console.log(error);      
        });      

      }

  addFirebase(){ //aqui é a função do botão que vai salvar a localização

  }
    
asked by anonymous 27.10.2017 / 20:46

1 answer

0

To get your position in Latlng from the maker you need to implement a Listener that will be called every time you drag your marker. Note that in the function I'm expecting the dragend event, in addition to those you can pass dragstart and drag , to know more read the documentation You can create this function:

lastLatLng(marker) {
  google.maps.event.addListener(marker, 'dragend', () => {
    this.LastLat = marker.position.lat();
    this.LastLng = marker.position.lng();

    const toast = this.toast.create({
      message: 'Latitude: ${this.LastLat}\nLongitude: ${this.LastLng}',
      duration: 6000,
      position: 'botton'
    });
    toast.present();
  });
}

Notice that I created two variables LastLat and LastLng . Then you just call this function in your loadMap function and pass your marker as a parameter.

Now to pass this information to Firebase I think I would ask another question, with this method you already have lat and lng just get these variables in your function that sends the data to firebase.

Running the app demo:

    
28.10.2017 / 20:56