Get the long lat of a certain place


#1

Hi, im new in ionic framework, my problem is im trying to get the longitude and latitude of a certain place, not only my current location, how do i do this?? Can someone give link if this concern is being opened before. or Can i ask for help?

thank you!


#2

hey @harniel1,

Actually to get those information you will need to use google api. but to get good answer you will need to give more information, contextualize your question. For example wich places are you trying to get lng and lat, city, business places. The more clear you’ll be more people could help you easily.

Thanks


#3

@quieterkali this is my code snippet:

in my AutoCompletePage.ts

import { Component, NgZone } from ‘@angular/core’;
import { IonicPage, NavController, NavParams, ViewController } from ‘ionic-angular’;

import { Geolocation } from ‘@ionic-native/geolocation’;
import { NativeGeocoder, NativeGeocoderReverseResult, NativeGeocoderForwardResult } from ‘@ionic-native/native-geocoder’;

declare var google;

@IonicPage()
@Component({
selector: ‘page-autocompletepage’,
templateUrl: ‘autocompletepage.html’,
})
export class AutocompletepagePage {

// constructor(public navCtrl: NavController, public navParams: NavParams) {
// }

// ionViewDidLoad() {
// console.log(‘ionViewDidLoad AutocompletepagePage’);
// }

autocompleteItems: any = [];
autocomplete;
service = new google.maps.places.AutocompleteService();

constructor (public viewCtrl: ViewController, private zone: NgZone, public navCtrl: NavController,
public navParams: NavParams, public geolocation: Geolocation) {
this.autocompleteItems;
this.autocomplete = {
query: ‘’
};
}

dismiss() {
this.viewCtrl.dismiss();
}

chooseItem(item: any) {
this.viewCtrl.dismiss(item);
}

updateSearch() {
if (this.autocomplete.query == ‘’) {
this.autocompleteItems = [];
return;
}
let me = this;
this.service.getPlacePredictions({ input: this.autocomplete.query, componentRestrictions: {country: ‘PH’} }, function (predictions, status) {
// if (status != google.maps.places.PlacesServiceStatus.OK) {
// alert(status);
// return;
// }
me.autocompleteItems = [];
me.zone.run(function () {
predictions.forEach(function (prediction) {
me.autocompleteItems.push(prediction.description);
});
});
});
}

}

in the registerPage.ts

showAddressModal () {
let modal = this.modalCtrl.create(AutocompletepagePage);
let me = this;
modal.onDidDismiss(data => {
this.place = data;
});
modal.present();

}

im trying to get the lng lat of the result of this, what ever the place the user input: how can i get the long lat of “this.place”?
this.place contains the address returned by the autocompletemodal.


#4

I see, the way i will solve this, will be ask user to inform the address of the place he deserve to knowthe lat and lng, with that address i will use google api to get the lat and lng of that place.
Is that help you? Feel free to tell me what you think about this way to solve the problem.


#5

@quieterkali the user will not know the long lat i will store the long lat and store it for database purposes,

ahmm can you post the link for the google api that gets the long lat?

for ionic 3/2. please thank you!


#6

Here is the link: https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

function geocodeAddress(geocoder, resultsMap) {
        var address = document.getElementById('address').value;
        geocoder.geocode({'address': address}, function(results, status) {
          if (status === 'OK') {
            resultsMap.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
              map: resultsMap,
              position: results[0].geometry.location
            });
          } else {
            alert('Geocode was not successful for the following reason: ' + status);
          }
        });
      }

You just need to understand the code and use it at your convenience :slight_smile:

Hope that will help


#7

@quieterkali thank you very much for your time and hellp :slight_smile: it really worked!!


#8

you’re welcome, it’s lways good to help :slight_smile:


#9

Hey i am trying to display country name from latitude and longitude using reverse geocoding. Will you please help me do it