Is there a way to know if the native.newMap is done loading?

Hi,

Has anyone notice this problem? 

In the API doc, it uses timer to point to user current location 1000ms after calling this:

myMap = native.newMapView( display.contentCenterX, display.contentCenterY, 300, 220 )

I found it worked fine in high performance devices (like iPhone 5 …), but in some of low capacity android devices (like Galaxy S3 mini…), the timer doesn’t work. If i increase the timer to 10000ms, then it works. I though it’s because Map was loading longer than timer. If i can know when map is finish loading, then it should solve the problem.

Does anyone know how to check that, or have other solution? Please share!

Thanks,

The doc is out of date and will be updated shortly. The recommended way to do this is in this sample:

SampleCode/Interface/MapView

Thank albert90 for your reply. But i don’t think it solves my problem.

Here is the situation:

I want the map to change to user current location without any inaction from user, where the sample code uses button to go to current location. We don’t want that. We want the map to go to user current location instantly after it’s done loading. If we use timer, the interval may differ based devices, resulting to fail to go current location and show other marker (Esp, in some android device).

Sorry if i don’t make myself clear. Below is sample code i use:

local currentLocation local mapView = native.newMapView( display.contentCenterX,display.contentCenterY,display.contentWidth,display.contentHeight) if mapView then mapView.mapType = "standard" -- Initialize map to a real location, since default location (0,0) is not very interesting mapView:setCenter( 11.558831, 104.917445 ) -- A function to handle the "mapAddress" event (also known as "reverse geocoding") -- local mapAddressHandler = function( event ) if event.isError then -- Failed to receive location information. native.showAlert( "Error", event.errorMessage, { "OK" } ) else -- Location information received. Display it. -- local locationText = -- "Latitude: " .. currentLatitude .. -- ", Longitude: " .. currentLongitude .. -- ", Address: " .. ( event.streetDetail or "" ) .. -- " " .. ( event.street or "" ) .. -- ", " .. ( event.city or "" ) .. -- ", " .. ( event.region or "" ) .. -- ", " .. ( event.country or "" ) .. -- ", " .. ( event.postalCode or "" ) -- native.showAlert( "You Are Here", locationText, { "OK" } ) end end -- This is returned as a mapLocation event local function mapLocationListener(event) -- -- print("map tapped latitude: ", event.latitude) -- -- print("map tapped longitude: ", event.longitude) end mapView:addEventListener("mapLocation", mapLocationListener) local function showMarkerData() -- Show Other markers in the map end local function callMap() -- Fetch the user's current location -- Note: in XCode Simulator, the current location defaults to Apple headquarters in Cupertino, CA currentLocation = mapView:getUserLocation() if currentLocation then -- Move map so that current location is at the center local currentLatitude = currentLocation.latitude local currentLongitude = currentLocation.longitude mapView:setRegion( currentLatitude, currentLongitude, 0.02, 0.02, true ) -- Look up nearest address to this location (this is returned as a "mapAddress" event, handled above) mapView:nearestAddress( currentLatitude, currentLongitude,mapAddressHandler ) -- native.showAlert( "Debuging", "End of Add Current location!",{"OK"} ) end -- timer.performWithDelay( 500, showMarkerData ) end -- A listener for the address lookup result -- (This could also be a table listener on the map itself, in case you have more than one simultaneous map.) Runtime:addEventListener( "mapAddress", mapAddressHandler ) timer.performWithDelay( 5000, callMap ) -- get current location after 5 second, sometimes it's fail also end

Great question. I would love to know the answer as well. How can we determine when the map is done loading? Thanks

Any way to determine this?

The doc is out of date and will be updated shortly. The recommended way to do this is in this sample:

SampleCode/Interface/MapView

Thank albert90 for your reply. But i don’t think it solves my problem.

Here is the situation:

I want the map to change to user current location without any inaction from user, where the sample code uses button to go to current location. We don’t want that. We want the map to go to user current location instantly after it’s done loading. If we use timer, the interval may differ based devices, resulting to fail to go current location and show other marker (Esp, in some android device).

Sorry if i don’t make myself clear. Below is sample code i use:

local currentLocation local mapView = native.newMapView( display.contentCenterX,display.contentCenterY,display.contentWidth,display.contentHeight) if mapView then mapView.mapType = "standard" -- Initialize map to a real location, since default location (0,0) is not very interesting mapView:setCenter( 11.558831, 104.917445 ) -- A function to handle the "mapAddress" event (also known as "reverse geocoding") -- local mapAddressHandler = function( event ) if event.isError then -- Failed to receive location information. native.showAlert( "Error", event.errorMessage, { "OK" } ) else -- Location information received. Display it. -- local locationText = -- "Latitude: " .. currentLatitude .. -- ", Longitude: " .. currentLongitude .. -- ", Address: " .. ( event.streetDetail or "" ) .. -- " " .. ( event.street or "" ) .. -- ", " .. ( event.city or "" ) .. -- ", " .. ( event.region or "" ) .. -- ", " .. ( event.country or "" ) .. -- ", " .. ( event.postalCode or "" ) -- native.showAlert( "You Are Here", locationText, { "OK" } ) end end -- This is returned as a mapLocation event local function mapLocationListener(event) -- -- print("map tapped latitude: ", event.latitude) -- -- print("map tapped longitude: ", event.longitude) end mapView:addEventListener("mapLocation", mapLocationListener) local function showMarkerData() -- Show Other markers in the map end local function callMap() -- Fetch the user's current location -- Note: in XCode Simulator, the current location defaults to Apple headquarters in Cupertino, CA currentLocation = mapView:getUserLocation() if currentLocation then -- Move map so that current location is at the center local currentLatitude = currentLocation.latitude local currentLongitude = currentLocation.longitude mapView:setRegion( currentLatitude, currentLongitude, 0.02, 0.02, true ) -- Look up nearest address to this location (this is returned as a "mapAddress" event, handled above) mapView:nearestAddress( currentLatitude, currentLongitude,mapAddressHandler ) -- native.showAlert( "Debuging", "End of Add Current location!",{"OK"} ) end -- timer.performWithDelay( 500, showMarkerData ) end -- A listener for the address lookup result -- (This could also be a table listener on the map itself, in case you have more than one simultaneous map.) Runtime:addEventListener( "mapAddress", mapAddressHandler ) timer.performWithDelay( 5000, callMap ) -- get current location after 5 second, sometimes it's fail also end

Great question. I would love to know the answer as well. How can we determine when the map is done loading? Thanks

Any way to determine this?

Any news on this? I would be interested to know as well.

Any news on this? I would be interested to know as well.

What about making a recursive function calling itself until a user location has been found?

Something like

[lua]local function setRegionMap()
local currentLocation = myMap:getUserLocation()

if currentLocation.errorCode == nil then
myMap:setRegion( currentLocation.latitude, currentLocation.longitude, 0.004, 0.002, true)
else
timer.performWithDelay( 1000, setRegionMap )
end

end[/lua]

Which was initiated by

[lua]myMap = native.newMapView( MapX, MapY, MapWidth , MapHeight )

if myMap then
myMap.mapType = “satellite”

timer.performWithDelay( 1000, setRegionMap )

end[/lua]

I don’t believe the map itself has a way of letting you know, however you can call one of its functions like: myMap:requestLocation() and that has a call back function to let  you know when it converted the address and it’s ready.

Rob

What about making a recursive function calling itself until a user location has been found?

Something like

[lua]local function setRegionMap()
local currentLocation = myMap:getUserLocation()

if currentLocation.errorCode == nil then
myMap:setRegion( currentLocation.latitude, currentLocation.longitude, 0.004, 0.002, true)
else
timer.performWithDelay( 1000, setRegionMap )
end

end[/lua]

Which was initiated by

[lua]myMap = native.newMapView( MapX, MapY, MapWidth , MapHeight )

if myMap then
myMap.mapType = “satellite”

timer.performWithDelay( 1000, setRegionMap )

end[/lua]

I don’t believe the map itself has a way of letting you know, however you can call one of its functions like: myMap:requestLocation() and that has a call back function to let  you know when it converted the address and it’s ready.

Rob