Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-24535] iOS: Ti.Geolocation Speed returning incorrect number or -1 for speed, requires docs-clarification

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2017-05-07T13:32:37.000+0000
Affected Version/sn/a
Fix Version/sRelease 6.1.0
ComponentsiOS
Labelsn/a
ReporterAmanda
AssigneeHans Knöchel
Created2017-03-26T15:49:07.000+0000
Updated2017-05-09T17:40:42.000+0000

Description

When listening for the location event for iOS, the speed returned is very often -1 and sometimes an incorrect value. It's very inconsistent. This was working before with our previous app release with SDK 5.5.0.GA but it no longer does with the newest SDK 6.0.2. I also tried rolling back to 5.5.0.GA but this doesn't seem to fix the issue either. index.js:
var numOfCalls = 0;
var locationCallback = function(e) {
	$.numOfLocationCalls.text = "Num of calls: " + numOfCalls;
	numOfCalls++;
	if (!e.success || e.error) {
		$.labelSpeed.text = "Error:" + JSON.stringify(e);
    } else {
    	var mph = e.coords.speed * 2.23694;
    	$.label.text = "Speed: " + mph;
    	var overSpeed = (e.coords.speed > 5) ? true : false;
    	$.labelSpeed.text = "Over speed: " + overSpeed;
    }
};

Ti.Geolocation.addEventListener('location', locationCallback);
Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS;
Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
Ti.Geolocation.distanceFilter = 5;

$.index.open();
index.xml:
<Alloy>
	<Window class="container">
		<View layout="vertical" height="Titanium.UI.SIZE">
			<Label id="label">Hello, World</Label>
			<Label id="labelSpeed">Hello, World</Label>
			<Label id="numOfLocationCalls">Hello, World</Label>
		</View>
	</Window>
</Alloy>
in tiapp.xml, I have these permissions:
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your location will only be used when the app is running.</string>
NOTE: I tried adding this to the axway support portal as I am a enterprise developer, but I can't seem to login at the moment. Will likely be adding this there once I get that sorted out.

Comments

  1. Amanda 2017-03-26

    I forgot to mention - this seems to work perfectly in the simulator when changing the location type in the debug menu. The issue occurs when driving and using an actual device. There is also a stackoverflow question on this here: http://stackoverflow.com/questions/42615548/appcelerator-geolocation-never-returns-a-speed-on-ios
  2. Hans Knöchel 2017-03-26

    Hey Amanda! Maybe it is an iOS issue? Especially if it worked with 5.5.0 but does not when using 5.5.0 now. We did not change Geolocation-related API's between those versions. We will take a look
  3. Amanda 2017-03-27

    I forgot to remove the onClick event for the label in index.xml, sorry about that. It might be that iOS did change something in relation to this. Testing with hyper loop, it seems that for the core location accuracy ( CLLocationAccuracy ) , the most consistent value has been k​CLLocation​Accuracy​Best​For​Navigation. And we've been using Ti.Geolocation.ACCURACY_BEST, I'm guessing maps to k​CLLocation​Accuracy​Best. We ended up testing it again with Ti.Geolocation.ACCURACY_BEST_FOR_NAVIGATION accuracy, and it finally seems to be working again as expected. Can this be updated in the documentation somewhere?
  4. Kam Rezvani 2017-04-28

    Ti.Geolocation.ACCURACY_BEST does not work any more. It is used to as it is indicated here but it no longer is. Please address this issue. We are substituting ACCURACY_BEST_FOR_NAVIGATION as a workaround.
  5. Eric Merriman 2017-05-02

    Checking this issue.
  6. Eric Merriman 2017-05-02

    subtle changes to the sample code: add $.index.open to index.js remove onClick="doClick" from index.xml
  7. Eric Wieber 2017-05-02

    I see the same issue when using Ti.Geolocation.ACCURACY_BEST, using: MacOS 10.12 (16A323) Studio 4.8.1.201612050850 Ti SDK 6.0.4.GA Appc NPM 4.2.9 Appc CLI 6.2.0 Alloy 1.9.11 Xcode 8.3.2 (8E2002) I will get a -1 for the coords.speed value. In the couple hours I was testing it, I did not see the negative value while driving, but I would see it when I stopped (not consistently though). After changing the accuracy to Ti.Geolocation.ACCURACY_BEST_FOR_NAVIGATION, I did not see a negative value for speed. Again this was only within a couple hours of testing, so the negative could be so intermittent that it did not show in that time.
  8. Eric Wieber 2017-05-03

    Edited the code in the description to include the changes needed for it to run.
  9. Hans Knöchel 2017-05-03

    Looks like native behavior: - http://stackoverflow.com/a/4956243/5537752 - http://stackoverflow.com/questions/30098202/cllocationmanager-always-returning-speed-1 Still can check it though, but I am also pretty sure this cannot be an iOS issue, since all we do to return the speed is [this line](https://github.com/appcelerator/titanium_mobile/blob/master/iphone/Classes/GeolocationModule.m#L971) to bridge the native type to the JavaScript type.
  10. Amanda 2017-05-03

    It's very possible it's native behavior. If that is the case, the documentation for appcelerator needs to be updated to reflect if we are needing speed, then the navigation setting needs to be used.
  11. Hans Knöchel 2017-05-03

    [~amandavines] You are very correct! Here are my test-results (wrapping the test-case into a single file):
        var win = Ti.UI.createWindow({
            backgroundColor: '#fff'
        });
        
        var numOfCalls = 0;
        
        var label = Ti.UI.createLabel({
            text: 'Speed: (Not set)',
            top: 100
        });
        
        var labelSpeed = Ti.UI.createLabel({
           text: 'Error: (None)',
           top: 200,
           color: 'red' 
        });
        
        var numOfLocationCalls = Ti.UI.createLabel({
           text: '# Location Calls: ' + numOfCalls,
           top: 300
        });
        
        var locationCallback = function(e) {
        	numOfLocationCalls.text = " Location Calls: " + numOfCalls;
        	numOfCalls++;
        	
            if (!e.success || e.error) {
        		labelSpeed.text = "Error: " + JSON.stringify(e);
            } else {
            	var mph = e.coords.speed * 2.23694;
            	label.text = "Speed: " + mph;
            	var overSpeed = (e.coords.speed > 5) ? true : false;
            	labelSpeed.text = "Over speed: " + overSpeed;
            }
        };
         
        Ti.Geolocation.addEventListener('location', locationCallback);
        Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS;
        Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
        Ti.Geolocation.distanceFilter = 5;
        
        win.add([label, labelSpeed, numOfLocationCalls]);
        win.open();
        
    I was driving around the Apple campus (virtually, using the iOS Simulator) and I've got correct values. Anyway, when you do not have a clear GPS signal, it will have value anomalies around the speed like stated in the above links. Do you have a proper wording for a note we could add? I feel like I am not into it enough so far to word it properly. Maybe this? {quote} Due to the Apple Geolocation API, set the Ti.Geolocation.accuracy to Ti.Geolocation.ACCURACY_BEST_FOR_NAVIGATION in order to properly measure speed changes and prevent the application from returning -1 values inside the location event. {quote} I would add this paragraph to the location event docs (see exact wording in the PR below). Finally, I am still wondering why it would be different between 5.5.x and 6.x, there has been no change between those versions. If you can, please check that one again.
  12. Hans Knöchel 2017-05-03

    PR: https://github.com/appcelerator/titanium_mobile/pull/9010 No backport required since docs are always pushed from master.
  13. Eric Wieber 2017-05-09

    Text/wording changes are good and describe the issue/solution.

JSON Source