[RESOLVED] Unable to add button with mapView

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

I had some runtime errors that I did not see till I looked in the log on my device. Now I am getting an error saying myLong is nil. This is set with:

 myLocation = myMap:getUserLocation() myLat = myLocation.latitude myLong = myLocation.longitude 

I put an alert after it to show what myLong is and I got the error there. Are there any issues with the getUserLocation?

Thanks

I added some code to show the location after fixing some errors and the location being returned in unknown. Is there a reason why? I have the GPS set to On. And below is my code and the build so you see my settings. Thanks!!! Btw, the map does show my place on the map with a blue marker which is correct. So it is using the GPS.

Build file:

-- Supported values for orientation: -- portrait, portraitUpsideDown, landscapeLeft, landscapeRight settings = { android = { usesPermissions = { -- Permission to retrieve current location from the GPS. "android.permission.ACCESS\_FINE\_LOCATION", -- Permission to retrieve current location from WiFi or cellular service. "android.permission.ACCESS\_COARSE\_LOCATION", "android.permission.INTERNET", }, }, }

 

Code:

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, 360 ) myMap.mapType = "standard" -- other mapType options are "satellite" or "hybrid" local locationTable = myMap:getUserLocation() local locationtxt = display.newText("My location is: ", 0, 410, native.systemFont, 16) if locationTable.errorCode then locationtxt.text = locationtxt.text .. locationTable.errorMessage else locationtxt.text = locationtxt.text .. locationTable.latitude .. ", " ..locationTable.longitude end myLocation = myMap:getUserLocation() myLat = myLocation.latitude myLong = myLocation.longitude local locationText3 = "Latitude: " .. myLat .. ", Longitude: " .. myLong local alert3 = native.showAlert( "You Are Here", locationText3, { "OK" } ) -- 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

I got it working. Seems I needed to wait for the mapView to show the map of my current location. Then I called the routine to show the lat/long and worked. I was also able to add a button. There were runtime errors before the button was added that stopped it.

Thanks!

I had some runtime errors that I did not see till I looked in the log on my device. Now I am getting an error saying myLong is nil. This is set with:

 myLocation = myMap:getUserLocation() myLat = myLocation.latitude myLong = myLocation.longitude 

I put an alert after it to show what myLong is and I got the error there. Are there any issues with the getUserLocation?

Thanks

I added some code to show the location after fixing some errors and the location being returned in unknown. Is there a reason why? I have the GPS set to On. And below is my code and the build so you see my settings. Thanks!!! Btw, the map does show my place on the map with a blue marker which is correct. So it is using the GPS.

Build file:

-- Supported values for orientation: -- portrait, portraitUpsideDown, landscapeLeft, landscapeRight settings = { android = { usesPermissions = { -- Permission to retrieve current location from the GPS. "android.permission.ACCESS\_FINE\_LOCATION", -- Permission to retrieve current location from WiFi or cellular service. "android.permission.ACCESS\_COARSE\_LOCATION", "android.permission.INTERNET", }, }, }

 

Code:

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, 360 ) myMap.mapType = "standard" -- other mapType options are "satellite" or "hybrid" local locationTable = myMap:getUserLocation() local locationtxt = display.newText("My location is: ", 0, 410, native.systemFont, 16) if locationTable.errorCode then locationtxt.text = locationtxt.text .. locationTable.errorMessage else locationtxt.text = locationtxt.text .. locationTable.latitude .. ", " ..locationTable.longitude end myLocation = myMap:getUserLocation() myLat = myLocation.latitude myLong = myLocation.longitude local locationText3 = "Latitude: " .. myLat .. ", Longitude: " .. myLong local alert3 = native.showAlert( "You Are Here", locationText3, { "OK" } ) -- 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

I got it working. Seems I needed to wait for the mapView to show the map of my current location. Then I called the routine to show the lat/long and worked. I was also able to add a button. There were runtime errors before the button was added that stopped it.

Thanks!