Hello,
I am trying out the mapView control and am trying to add a button at the bottom of the screen. When i run the following code it does not show. Am I doing something wrong? I have added buttons in other projects.
I am wanting to click the button and view the alert I have on there.
Thanks!
local widget = require ( "widget" ) local myMap local myLat local myLong local myLocation local myOptions local myMap local onbtnEvent = function ( event ) --if event.phase == "release" then local phase = event.phase if "ended" == phase then local currentLocation2 = myMap:getUserLocation() if currentLocation2 then -- Move map so that current location is at the center local currentLatitude = currentLocation2.latitude local currentLongitude = currentLocation2.longitude myMap:setCenter( currentLatitude, currentLongitude, true ) -- Look up nearest address to this location (this is returned as a "mapAddress" event, handled above) myMap:nearestAddress( currentLatitude, currentLongitude ) local locationText2 = "Latitude: " .. currentLatitude .. ", Longitude: " .. currentLongitude local alert2 = native.showAlert( "You Are Here", locationText2, { "OK" } ) else local alert2 = native.showAlert( "No Location","123", { "OK" } ) end end return true end myMap = native.newMapView( 5, 20, 315, 460 ) myMap.mapType = "standard" -- other mapType options are "satellite" or "hybrid" myLocation = myMap:getUserLocation() myLat = myLocation.latitude myLong = myLocation.longitude -- The MapView is just another Corona display object, and can be moved or rotated, etc. myMap.x = display.contentWidth / 2 myMap.y = 120 -- Initialize map to a real location, since default location (0,0) is not very interesting myMap:setCenter( myLat, myLong ) local btnPost = widget.newButton{ label = "Address", id = "btn002", left = 80, top = 450, width = 125, height = 40, fontSize = 11, cornerRadius = 8, onEvent = onbtnEvent } btnPost.x = 80 btnPost.y = 450 local function callMap() -- Fetch the user's current location -- Note: in XCode Simulator, the current location defaults to Apple headquarters in Cupertino, CA local currentLocation = myMap:getUserLocation() if currentLocation then -- Move map so that current location is at the center local currentLatitude = currentLocation.latitude local currentLongitude = currentLocation.longitude myMap:setCenter( currentLatitude, currentLongitude, true ) -- Look up nearest address to this location (this is returned as a "mapAddress" event, handled above) myMap:nearestAddress( currentLatitude, currentLongitude ) local locationText2 = "Latitude: " .. currentLatitude .. ", Longitude: " .. currentLongitude local alert2 = native.showAlert( "You Are Here", locationText2, { "OK" } ) else local alert2 = native.showAlert( "No Location","123", { "OK" } ) end end -- A function to handle the "mapAddress" event (also known as "reverse geocoding") -- local mapAddressHandler = function( event ) local locationText = "Latitude: " .. currentLatitude .. ", Longitude: " .. currentLongitude .. ", Address: " .. event.streetDetail .. " " .. event.street .. ", " .. event.city .. ", " .. event.region .. ", " .. event.country .. ", " .. event.postalCode local alert = native.showAlert( "You Are Here", locationText, { "OK" } ) 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( 1000, callMap ) -- get current location after 1 second