Bullyen
Bullyen

Reputation: 808

Phonegap GPS asking twice

I realize this question has been asked 100 times and I feel like I have read every question and answer but I have not found a solution to my problem.

I have the PhoneGap plugins installed and configured correctly.

I have confirmed all files are located in the plugins > org.apache.cordova.geolocation folder

Here is the call to the geolocation feature in my config.xml

<feature name="Geolocation">
    <param name="android-package" value="org.apache.cordova.geolocation.GeoBroker" />
    <param name="ios-package" value="CDVLocation" />
</feature>

My application is calling for the geolocation only when the user asks to find their location. Even so I have made sure to use:

document.addEventListener("deviceready", beginSetup, false); 

To ensure the device is ready before calling geolocation.

The first request uses my app name. The second call displays a long ugly url. After the first request the app name request goes away (perfect so far) but the ugly url still asks for permission on.

After the device is ready I am binding the location call to a tap event like so:

  geocoder = new google.maps.Geocoder();

  $("#my-location").bind( "tap", function(e){
      //unrelated code omitted
      //I know these times are big, but this is just testing at this point
      var options = { maximumAge: 3600000, timeout: 60000, enableHighAccuracy: true };

      if(map != null){
            circle.setMap(null);
            personalCircle.setMap(null);
            google.maps.event.clearInstanceListeners(map);
            deleteMarkers();
            map = null;
      }

      navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
});

Thank you in advance for your advice. Please let me know if you would like to see any other code.

EDIT I added the following lines to my config.xml per @cagdas advice. Still experiencing the same issue.

<gap:plugin name="org.apache.cordova.geolocation" />
<feature name="http://api.phonegap.com/1.0/geolocation"/>

UPDATE

This issue seems to have been resolved by a recent version of PhoneGap. Now when I compile the application I no longer have this issue. I did not have to change anything in my code.

Upvotes: 3

Views: 782

Answers (3)

Ben
Ben

Reputation: 459

You must include the cordova.js script in index.html.

<script type="text/javascript" src="cordova.js"></script>

This file gets generated and added to platforms/<platform>/www when you build. I banged my head against this problem for days and never saw any explicit mention that leaving this file out would cause this second permissions request. I checked the geolocation examples in the official docs and, sure enough, this line is in their index.html file as well.

I know this is an old question, but I hope it helps someone in the future.

Upvotes: 1

gro
gro

Reputation: 755

While I'm not completely clear on what you are experiencing re: "first request" and "second call", I will offer this, fwiw...

The initial app install and execution will display a prompt to allow the app to use your location (per CDV configuration requiring geolocation). This is the prompt that includes your app name. This is a one time thing, and how the user answers determines whether or not permission has been granted, which you should be verifying before making subsequent location calls.

While it is suggested that one should verify location permission is granted prior to calling any geolocation methods, it should fail if permission isn't granted.

The "long ugly url" is related to the explicit geolocation call.

If you are seeing this just in your simulator, then please make sure you have IOS Simulator -> Debug -> Location not set to None

If you are getting BOTH of these one after another, without tapping your control, then there must be a geolocation call happening outside of the event handler, or the event handler is being triggered programatically.

If you are getting BOTH of these one after another after tapping the control, then something may be wrong in your project setup, as the first prompt should show in initial install.

Please not that you need to delete the app from the simulator in order for the "initial app install" to occur (or reset the simulator).

If none of this helps, please provide iOS and phonegap/cordova versions.

Upvotes: 1

cagdas
cagdas

Reputation: 1644

You'll need to include below line in your config.xml:

<gap:plugin name="org.apache.cordova.geolocation" />

If it is not included, HTML Geolocation tries to find your location and as you mentioned some URL -possibly including your MAC or other device identifier- needs an approvement.

After including above line, for Android and Windows devices, please insert below line to adapt correct premissions:

<feature name="http://api.phonegap.com/1.0/geolocation"/>

Please check these documents checking with correct phonegap version:

http://docs.build.phonegap.com/en_US/2.9.0/configuring_features.md.html https://build.phonegap.com/plugins/627

Upvotes: 1

Related Questions