Reputation: 3
I am trying to do this while building build in ionic, but the location is on IOS and does not work with geolocation in ionic 2 app.
I have installed ionic cordova plugin add cordova-plugin-geolocation and
In android devices it is working when the location service is made on. When the location service is made off its not working. In iOS devices both scenarios not working. Need some help!!
import { Geolocation } from '@ionic-native/geolocation';
In Providers I have mentioned Geolocation; In home.ts I imported Geolocation.
To get the current location I Have written the following code
getCurrentLocation(){
this.geolocation.getCurrentPosition().then((position) => {
let loc = {
placeId: null,
name:null,
lat:null,
long:null,
}
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var latlng = new google.maps.LatLng(lat, lng);
var geocoder = geocoder = new google.maps.Geocoder();
}
Upvotes: 0
Views: 3344
Reputation: 181
You can also manually edit the platform/ios/{project}/{project}/project.info.plist file and add the following lines
<key>NSLocationWhenInUseUsageDescription</key>
<string>Location is needed because [your reason]</string>
Upvotes: 1
Reputation: 191
If location on IOS do not work with geolocation
Since iOS 10 it's mandatory to add a NSLocationWhenInUseUsageDescription entry in the info.plist.
NSLocationWhenInUseUsageDescription describes the reason that the app accesses the user's location.
When the system prompts the user to allow access, this string is displayed as part of the dialog box.
To add this entry you can pass the variable GEOLOCATION_USAGE_DESCRIPTION on plugin install.
Example: cordova plugin add cordova-plugin-geolocation --variable GEOLOCATION_USAGE_DESCRIPTION="your usage message"
If you don't pass the variable, the plugin will add an empty string as value.
To solve your problem, try: Uninstall the plugin: cordova plugin remove cordova-plugin-geolocation Reinstall with:
cordova plugin add cordova-plugin-geolocation --variable GEOLOCATION_USAGE_DESCRIPTION="my_project would like to use your location"
platform/ios/{project}/{project}/project.info.plist
This will automatically add entry in the info.plist file
Upvotes: 5