Ionic - save latitude and longitude on firebase

Asked

Viewed 464 times

3

I created a project on Ionic and I would like that when the user clicked on "save location", the position from where he left the marker was saved in his user profile in firebase, however, I have no idea how to take the latitude and longitude of the marker and save in firebase, some 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

  }
  • 1

    What you’ve already done?

  • 1

    How you plan to capture the location, by marker, by address name..?

  • need to capture the location of the marker at the point the user has dragged, it is draggable

  • Got it, could you add your map code? Just edit your question and add the code snippet

  • Okay, just a second

  • just need the location of the marker that he dragged, stay saved in firebase, when click on the "save location" button and will call the "addFirebase()"

  • Let’s go by parts, first you need to take the latitude and longitude and then save rs

  • OK kkk this very rs

  • Another question, when you click it is not running the addListener function?

  • that click was just a test I did, it was the closest I got to getting the location of the marker

  • but it was not useful because I could not use it in the "addFirebase()" that is triggered when you click the button

  • All right, when I get home I’ll set an example for you.

  • All right, thanks bro

Show 8 more comments

1 answer

1

To get your position in Latlng from the maker you need to implement a Listener that will be called every time you drag your marketing. Note that in the function I am waiting for the event of dragend, beyond these you can pass the dragstart and the drag, to learn 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 its function loadMap and pass your marketing as parameter.

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

Demo of the running app:

inserir a descrição da imagem aqui

  • thank you very much, helped too, I will test

  • did not know about this 'dragend' function'

  • @Juniordossantos tries to implement and see if it works, if it helps you can mark this answer as a solution, so other people with the same problem will be helped.

  • The button Voce created "add Marker" calls the lastLatLng function???

  • No, the button only adds a marker instead of loading the map with a marker

Browser other questions tagged

You are not signed in. Login or sign up in order to post.