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