Map:getUserlocation() not working

I’ve a client who needs a map. So I needed to get current location and used the same example in the docs but received no table. I’m using iOS 8.1 build 2528. I even set a timer to wait 5 seconds before setting map location with the returned table data. Any ideas what I’m doing wrong? Or is this a bug?

Hi @tartarsaucemedia,

Can you post some basic code that you’re using to test this? If possible, please reduce it down to as minimal as possible (i.e. no usage of Composer scenes and events, additional modules, etc.).

Also, please post the contents of your build.settings file.

Thanks,

Brent

main.lua

local myMap = native.newMapView( 0, 0, display.contentWidth, display.contentHeight ) myMap.x = display.contentCenterX myMap.y = display.contentCenterY local locationTable = myMap:getUserLocation() timer.performWithDelay( 5000, function() myMap:setCenter(locationTable.latitude, locationTable.longitude, false ) end)

build.settings

settings = { iphone = { plist = { CFBundleDisplayName = "MapApp", CFBundleName = "MapApp", NSLocationWhenInUseUsageDescription = "This app uses maps.", }, }, }

Hi @tartarsaucemedia,

Can you try the example in the docs which detects if there’s an error code/message when getting the location?

http://docs.coronalabs.com/api/type/Map/getUserLocation.html

local myMap = native.newMapView( 0, 0, 200, 200 ) myMap.x = display.contentCenterX myMap.y = display.contentCenterY local locationTable = myMap:getUserLocation() local locationtxt = display.newText( "My location is: ", display.contentCenterX, 100, native.systemFont, 16 ) locationtxt:setFillColor( 1 ) if ( locationTable.errorCode ) then locationtxt.text = locationtxt.text .. locationTable.errorMessage else locationtxt.text = locationtxt.text .. locationTable.latitude .. ", " ..locationTable.longitude

Prints “My location is: 0, 0”

But… if I set a timer this way it works.

timer.performWithDelay( 5000, function() locationTable = myMap:getUserLocation() if ( locationTable.errorCode ) then locationtxt.text = locationtxt.text .. locationTable.errorMessage else locationtxt.text = locationtxt.text .. locationTable.latitude .. ", " ..locationTable.longitude myMap:setCenter(locationTable.latitude, locationTable.longitude, false ) end end)

Hi @tartarsaucemedia,

Thanks for the testing on your part. I believe the engineers are doing some further testing on this at the moment (somewhat coincidental and not related to your specific report). When I find out more, I’ll post back.

In the meantime, instead of waiting 5000 milliseconds, perhaps you could just create a repeating timer of like 200-500 milliseconds which gets the user location. If it’s “0, 0”, then keep repeating the process until that value is not"0, 0", then stop the timer and break out to the other process you need (centering the map, it appears).

Brent

I’m confused.  In the top post you say it’s not working with a 5 second delay.  But in the last post, you say it work with the 5 second delay.

Normally you need to have a delay before you try to request the location.  The GPS takes a few seconds to fire up.  In the map app I’m currently building, I’m waiting 1 second before I try to call getUserLocation.   Even that fails a bit too often.  I set up a block of code that tries every second to get location and if it fails, it tries again, up to 10 times to get a location.

It takes time for the device to find the GPS.  If you are inside it can delay things even more.

Rob

The first attempt tried to get the location right away, and the second attempt waited 5 seconds before getting location. I’ve used gps with titanium in the past and should’ve known it takes a few seconds to get a location. Even a few attempts to get a precise location is a best practice because the first location is usually a cached location on some devices.

Hi @tartarsaucemedia,

Can you post some basic code that you’re using to test this? If possible, please reduce it down to as minimal as possible (i.e. no usage of Composer scenes and events, additional modules, etc.).

Also, please post the contents of your build.settings file.

Thanks,

Brent

main.lua

local myMap = native.newMapView( 0, 0, display.contentWidth, display.contentHeight ) myMap.x = display.contentCenterX myMap.y = display.contentCenterY local locationTable = myMap:getUserLocation() timer.performWithDelay( 5000, function() myMap:setCenter(locationTable.latitude, locationTable.longitude, false ) end)

build.settings

settings = { iphone = { plist = { CFBundleDisplayName = "MapApp", CFBundleName = "MapApp", NSLocationWhenInUseUsageDescription = "This app uses maps.", }, }, }

Hi @tartarsaucemedia,

Can you try the example in the docs which detects if there’s an error code/message when getting the location?

http://docs.coronalabs.com/api/type/Map/getUserLocation.html

local myMap = native.newMapView( 0, 0, 200, 200 ) myMap.x = display.contentCenterX myMap.y = display.contentCenterY local locationTable = myMap:getUserLocation() local locationtxt = display.newText( "My location is: ", display.contentCenterX, 100, native.systemFont, 16 ) locationtxt:setFillColor( 1 ) if ( locationTable.errorCode ) then locationtxt.text = locationtxt.text .. locationTable.errorMessage else locationtxt.text = locationtxt.text .. locationTable.latitude .. ", " ..locationTable.longitude

Prints “My location is: 0, 0”

But… if I set a timer this way it works.

timer.performWithDelay( 5000, function() locationTable = myMap:getUserLocation() if ( locationTable.errorCode ) then locationtxt.text = locationtxt.text .. locationTable.errorMessage else locationtxt.text = locationtxt.text .. locationTable.latitude .. ", " ..locationTable.longitude myMap:setCenter(locationTable.latitude, locationTable.longitude, false ) end end)

Hi @tartarsaucemedia,

Thanks for the testing on your part. I believe the engineers are doing some further testing on this at the moment (somewhat coincidental and not related to your specific report). When I find out more, I’ll post back.

In the meantime, instead of waiting 5000 milliseconds, perhaps you could just create a repeating timer of like 200-500 milliseconds which gets the user location. If it’s “0, 0”, then keep repeating the process until that value is not"0, 0", then stop the timer and break out to the other process you need (centering the map, it appears).

Brent

I’m confused.  In the top post you say it’s not working with a 5 second delay.  But in the last post, you say it work with the 5 second delay.

Normally you need to have a delay before you try to request the location.  The GPS takes a few seconds to fire up.  In the map app I’m currently building, I’m waiting 1 second before I try to call getUserLocation.   Even that fails a bit too often.  I set up a block of code that tries every second to get location and if it fails, it tries again, up to 10 times to get a location.

It takes time for the device to find the GPS.  If you are inside it can delay things even more.

Rob

The first attempt tried to get the location right away, and the second attempt waited 5 seconds before getting location. I’ve used gps with titanium in the past and should’ve known it takes a few seconds to get a location. Even a few attempts to get a precise location is a best practice because the first location is usually a cached location on some devices.