setCenter/setRegion Issue with Mapview on Android

I’m having an issue where setCenter and setRegion do not always center a Mapview at the specified location (latititude, longitude) in a program that I am working on. I can reproduce this same issue with the sample Mapview program that comes with Corona. To reproduce, touch the “Current Location” button on the sample program then pinch zoom out just a bit. Then press the “Current Location” button again.

The issue does NOT always happen but usually within 5 or 6 tries you will see that the map does not center back to your current location. Once the issue occurs, another touch of the “Current Location” button works perfectly. It only seems to happen if the user zooms prior to touching the “Current Location” button. It seems that the farther you zoom out the farther off center the map will be while the less you zoom out the less off center it will be. 

I have tested with three devices all running Android 4.1.1 (Asus Transformer Prime tablet, Samsung SIII phone and a HTC Droid DNA phone) with the same results. I have tried on public release builds 971 and 1076. This could be an issue with only Android 4.1.1. To rule that out I was hoping some of you could run a few quick tests with the sample Mapview using other versions of Android to see if the issue occurs with those versions as well. All assistance is greatly appreciated!

Thanks ahead of time!

Scott

  

Confirmed on Iphone 4/IOS 6 and Sony Xperia Z also. Documentation says the region values are in ‘degrees’, but with the values 1,1 and 150,150 and 1500,1500 and 150000,150000, I get the same result in the map view. It’s zoomed in to a region of about 500x500 meters.

Hi guys,

If one of you could submit a bug test case on this, that will log it into the system to get inspected and hopefully fixed. @mobile36, it sounds like you already have a pretty solid test case built, so if you can trim that down to the “essentials” and submit it, that would be highly appreciated.

http://developer.coronalabs.com/content/bug-submission

Thanks,

Brent Sorrentino

Hi Brent,

Thanks for the response. I submitted a bug report on May 2, 2013. Corona case number is 23216. 

Thanks again!

Scott

Meanwhile, is there some way I can “manually” zoom out, say 2 seconds after the mapView is loaded? (I guess not, but asking just in case). 

There’s not a lot of levers and buttons to work with regarding maps…

You can zoom out, but you’d need to know where the map was centered and it’s zoom level to do it. Once the user zooms / pans the map, your app has no way to tell where it is centered / zoom level…

To zoom out 2 seconds after instantiating the map, you’d use a timer.performWithDelay() call. The actual call to zoom the map would look like:

[lua]

   currMap:setRegion( tonumber(centerLat), tonumber(centerLong), math.abs(spanLat), math.abs(spanLong), true )

   – using tonumber() because Android build freaks out if it’s a string, iOS side does not

[/lua]

But getting the timing right as to exactly when the map “is loaded” is a little tricky/probably app specific… The mapView takes up a significant amount of cpu resources (seems to have priority during enterFrame, and causes animations to stutter if it is heavily updating)… You’ll notice this if you try and update other UI elements while the map is actually zooming.

In my apps case, I am updating UI elements first, waiting a coupe hundred milliseconds for the UI to update onscreen, then making the map changes.

You might also notice some display update issues if you place an enterFrame handler in your main (while having mapViews zoom around on various screens)… But that’s a separate issue by itself…

Confirmed on Iphone 4/IOS 6 and Sony Xperia Z also. Documentation says the region values are in ‘degrees’, but with the values 1,1 and 150,150 and 1500,1500 and 150000,150000, I get the same result in the map view. It’s zoomed in to a region of about 500x500 meters.

Hi guys,

If one of you could submit a bug test case on this, that will log it into the system to get inspected and hopefully fixed. @mobile36, it sounds like you already have a pretty solid test case built, so if you can trim that down to the “essentials” and submit it, that would be highly appreciated.

http://developer.coronalabs.com/content/bug-submission

Thanks,

Brent Sorrentino

Hi Brent,

Thanks for the response. I submitted a bug report on May 2, 2013. Corona case number is 23216. 

Thanks again!

Scott

Meanwhile, is there some way I can “manually” zoom out, say 2 seconds after the mapView is loaded? (I guess not, but asking just in case). 

There’s not a lot of levers and buttons to work with regarding maps…

You can zoom out, but you’d need to know where the map was centered and it’s zoom level to do it. Once the user zooms / pans the map, your app has no way to tell where it is centered / zoom level…

To zoom out 2 seconds after instantiating the map, you’d use a timer.performWithDelay() call. The actual call to zoom the map would look like:

[lua]

   currMap:setRegion( tonumber(centerLat), tonumber(centerLong), math.abs(spanLat), math.abs(spanLong), true )

   – using tonumber() because Android build freaks out if it’s a string, iOS side does not

[/lua]

But getting the timing right as to exactly when the map “is loaded” is a little tricky/probably app specific… The mapView takes up a significant amount of cpu resources (seems to have priority during enterFrame, and causes animations to stutter if it is heavily updating)… You’ll notice this if you try and update other UI elements while the map is actually zooming.

In my apps case, I am updating UI elements first, waiting a coupe hundred milliseconds for the UI to update onscreen, then making the map changes.

You might also notice some display update issues if you place an enterFrame handler in your main (while having mapViews zoom around on various screens)… But that’s a separate issue by itself…

As a followup to this thread, the pseudocode below will cause this issue on Android. If the mapview is defined outside the function that displays the map itself, then it will not always center the map when viewing on an Android. Works fine on IOS.

local savLat = "35.732696" local savLng = "-78.62988379" local myMap = native.newMapView( 3, 3, 314, 225)  myMap.mapType = "standard" local function dspMap() myMap:setCenter(savLat, savLng, true) myMap:setRegion(savLat, savLng, .01, .01, true) end dspMap()  

Changing the code to place the mapview definition within the display map function solved the issue for me but I do not know why. 

local savLat = "35.732696" local savLng = "-78.62988379" local myMap  local function dspMap()      myMap = native.newMapView( 3, 3, 314, 225)       myMap.mapType = "standard"      myMap:setCenter(savLat, savLng, true)      myMap:setRegion( savLat, savLng, .01, .01, true) end dspMap()   

Hope this helps! Scott

But that’s impossible.

As a followup to this thread, the pseudocode below will cause this issue on Android. If the mapview is defined outside the function that displays the map itself, then it will not always center the map when viewing on an Android. Works fine on IOS.

local savLat = "35.732696" local savLng = "-78.62988379" local myMap = native.newMapView( 3, 3, 314, 225)  myMap.mapType = "standard" local function dspMap() myMap:setCenter(savLat, savLng, true) myMap:setRegion(savLat, savLng, .01, .01, true) end dspMap()  

Changing the code to place the mapview definition within the display map function solved the issue for me but I do not know why. 

local savLat = "35.732696" local savLng = "-78.62988379" local myMap  local function dspMap()      myMap = native.newMapView( 3, 3, 314, 225)       myMap.mapType = "standard"      myMap:setCenter(savLat, savLng, true)      myMap:setRegion( savLat, savLng, .01, .01, true) end dspMap()   

Hope this helps! Scott

But that’s impossible.

I’m seeing the setCenter issue as well. Was the case number is 23216 reported on May 2, 2013 ever fixed? It is almost a year old case and seems like it is still open unless my observation is a new problem. Any thoughts?

ksan, I do not think the case was ever resolved. The workaround I mentioned above was the only thing I found that worked for me. The problem with this approach is it creates a new map object every time you display the map which can eventually fill up memory so I had to make sure I removed each map before creating a new one…ugly, but it worked for me. 

mobile36, thanks for confirming. I spent a whole day troubleshooting my code thinking I have a bug somewhere in there then thankfully I found your thread. The workaround isn’t a useable one for me as I have a very frequent map -> detail view from marker tap ->  back to map cycle. The wait time on each map creation makes the app unusable so I hide and unhide the map. Thank you very much for the idea though. Most appreciated. 

Its a pity that bug like this, nearly a year old, is still out there. 

Just out of curiosity, do you see the wrong-centering always consistently? In my case the centering occurs correctly horizontally but vertically it is closer to the bottom of the screen than where it should be. I actually only see this issue on IOS. Just verified on Android and it seems to work as expected there. So maybe mine is a different variant of the same bug.

By the way, I just saw that bug # 23216 appears to be closed in the Bug Reporting system.