I am developing a mobile application which has a requirement that I use Ti.Map for iOS and Android and that I can track your location on the map, and keep your location in the centre of the map.
I am using 3.4.0 of the API.
I have use the Tracking Position and Heading to find out what is needed to get the location and to get the updates.
I have used Ti.Android.currentActivity and added event handlers as mentioned in the section Android Lifecycle Events
I have used the Ti.Map to create a map view, added two annotations to it, and added a route between those two points.
The problem that I am having is that the log is showing that the Destroy event is being fired on my Activity and so that location event is only fired once.
Ti.Geolocation.purpose = 'Get Current Location'; Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST; if (Ti.API.IsAndroid) { Ti.Geolocation.Android.accuracy = Titanium.Geolocation.ACCURACY_BEST; } Ti.Geolocation.distanceFilter = 10; Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS; addHandler(); if (Ti.API.IsAndroid) { // Defined in globals.js var activity = Ti.Android.currentActivity; activity.addEventListener('destroy', stopHandler); activity.addEventListener('pause', pauseHandler); activity.addEventListener('resume', addHandler); }This is run once and then the Activity seems to be destroyed and I am not sure why. Obviously I have missed something, but not quite sure what.
The code for the event handlers is as follows:
var locationAdded = false; var location = function(e) { if (!e.success) { alert('Error: ' + e.error); } else { Ti.API.debug('Locationn Handler: ' + JSON.stringify(e)); if ( typeof e.coords !== 'undefined') { createNetworkRoute(e.coords.latitude, e.coords.longitude, mapview); } } }; var addHandler = function() { if (!locationAdded) { Ti.API.debug('Add Handler run'); Ti.Geolocation.addEventListener('location', location); locationAdded = true; } }; var pauseHandler = function() { Ti.API.debug('Pause Handler run'); removeHandler(); }; var stopHandler = function() { Ti.API.debug('Stop Handler run'); removeHandler(); }; var removeHandler = function() { if (locationAdded) { Ti.API.debug('Remove Handler run'); Ti.Geolocation.removeEventListener('location', location); locationAdded = false; } };Here is the log from when Add Handler is run, to when the Window is closed.
Note that the windows says it is closed, even though the MapView and another button View is still visible on screen and function as expected.
DEBUG] : Add Handler run [DEBUG] : Locationn Handler: {"type":"location","source":{"bubbleParent":true,"hasCompass":true,"__propertiesDefined__":true,"preferredProvider":"gps","purpose":"Get Current Location","lastGeolocation":"{\"timestamp\":1416804388000,\"longitude\":138.65011800080538,\"latitude\":-34.79561974760145,\"speed\":0,\"accuracy\":10,\"heading\":294,\"altitude\":31.9000244140625}","Android":{"bubbleParent":true,"manualMode":false,"apiName":"Ti.Geolocation.Android","accuracy":2},"locationServicesEnabled":true,"distanceFilter":10,"accuracy":2,"_events":{"location":{}},"apiName":"Ti.Geolocation"},"provider":{"power":3,"accuracy":1,"name":"gps"},"coords":{"timestamp":1416804388000,"speed":0,"altitude":31.9000244140625,"longitude":138.65011800080538,"latitude":-34.79561974760145,"accuracy":10,"heading":294,"altitudeAccuracy":null},"bubbles":false,"success":true,"code":0,"cancelBubble":false} [INFO] : URL: https://maps.googleapis.com/maps/api/directions/json?origin=-34.79561974760145,138.65011800080538&destination=-37.922783,145.046548&sensor=true [DEBUG] : Window: Checkpoint: postWindowCreated() [INFO] : u: Making Creator dynamically [INFO] : I/Google Maps Android API: Google Play services client version: 6171000 [INFO] : I/Google Maps Android API: Google Play services package version: 6188034 [DEBUG] : dalvikvm: GC_CONCURRENT freed 701K, 13% free 13848K/15879K, paused 17ms+13ms, total 61ms [DEBUG] : dalvikvm: GC_CONCURRENT freed 787K, 13% free 14507K/16647K, paused 12ms+3ms, total 45ms [DEBUG] : dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 32ms [DEBUG] : dalvikvm: GC_FOR_ALLOC freed 869K, 16% free 14734K/17351K, paused 33ms, total 33ms [DEBUG] : dalvikvm: GC_CONCURRENT freed 692K, 12% free 15880K/17863K, paused 4ms+3ms, total 42ms [DEBUG] : dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 29ms [DEBUG] : REQUEST: Using server: https://clients4.google.com/glm/mmap/api [DEBUG] : REQUEST: Add Data Request: 75 [DEBUG] : dalvikvm: GC_CONCURRENT freed 1373K, 14% free 16586K/19271K, paused 19ms+34ms, total 217ms [DEBUG] : REQUEST: Add Data Request: 75 [DEBUG] : REQUEST: Connection opened to:https://clients4.google.com/glm/mmap/api [DEBUG] : REQUEST: Open Connection [DEBUG] : USER_EVENTS: Event: type=78, status: MD, data: T [INFO] : [{"distance":{"text":"0.1 km","value":121},"duration":{"text":"1 min","value":10},"end_location":{"lat":-34.79657830000001,"lng":138.6491145},<SNIP> [DEBUG] : adding new route [DEBUG] : AbsListView: Get MotionRecognitionManager [DEBUG] : dalvikvm: GC_CONCURRENT freed 1397K, 14% free 17184K/19975K, paused 15ms+4ms, total 84ms [DEBUG] : dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 60ms [DEBUG] : USER_EVENTS: Event: type=113, status: mc, data: [DEBUG] : REQUEST: Add Data Request: 147 [WARN] : ResourceType: Failure getting entry for 0x010802c0 (t=7 e=704) in package 0 (error -75) [WARN] : ResourceType: Failure getting entry for 0x010802c0 (t=7 e=704) in package 0 (error -75) [DEBUG] : HTTPClient: The persistent handle is disposed. [DEBUG] : USER_EVENTS: Event: type=58, status: gl, data: |r=8|g=8|b=8|a=0|d=24|s=8|v=ARM|i=OpenGL ES-CM 1.1|c=Mali-400 MP|e=t| [DEBUG] : REQUEST: Processing DataRequest: com.google.maps.api.android.lib6.b.d@42d9b6a0 [DEBUG] : REQUEST: Processing DataRequest: com.google.maps.api.android.lib6.gmm6.d.h@431cf660 [DEBUG] : REQUEST: Processing DataRequest: com.google.maps.api.android.lib6.gmm6.n.o@42c9d988 [DEBUG] : REQUEST: DRD(0): 62|75|75, 1s, <1kb [DEBUG] : REQUEST: Close [DEBUG] : USER_EVENTS: Event: type=22, status: fb, data: 1122 [DEBUG] : REQUEST: Connection opened to:https://clients4.google.com/glm/mmap/api [DEBUG] : REQUEST: Open Connection [DEBUG] : dalvikvm: GC_FOR_ALLOC freed 1469K, 15% free 17508K/20551K, paused 89ms, total 89ms [DEBUG] : USER_EVENTS: Event: type=22, status: lb, data: 1178 [DEBUG] : USER_EVENTS: Event: type=22, status: flbs, data: fb=1122|lb=1178|s=61 [DEBUG] : REQUEST: Add Data Request: 7 [DEBUG] : Stop Handler run [DEBUG] : Remove Handler run [DEBUG] : Window: Window is closed normally.