Runtime event listener "mapAddress", mapAddressHandler not working

Hi, i was trying out the example for the map view and ran into some problem.

http://developer.anscamobile.com/content/mapview

I just build the example provided from inside corona’s example folder and ran it on my iphone but the alert pop up did not show on my iphone 4.

This is a part of the code with the alert

local mapAddressHandler = function( event )
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
At first it lead me to believe that maybe it was the alert pop up problem so i did an alert pop up outside of the mapAddressHandler. It worked so now i am thinking that the runtime event listener does not work

Runtime:addEventListener( “mapAddress”, mapAddressHandler )

Hopefully someone can tell me what’s going on. Thanks in advance. [import]uid: 39846 topic_id: 8548 reply_id: 308548[/import]

where are you initializing currentLatitude and currentLongitude?

otherwise you will get an access violation and won’t get to the native.showAlert.

launch corona from the terminal and or use print statements to see what is going on inside of mapAddressHandler

c [import]uid: 24 topic_id: 8548 reply_id: 30704[/import]

Hi Carlos. Thanks for the quick reply. All i did was take the examples from corona and build it into my iphone 4 to do a run test so i guess the currentLatitude and currentLongitude are initialized where they should be. Below is the entire code from the example.

display.setStatusBar( display.HiddenStatusBar )

– Load external Lua libraries (from project directory)
local ui = require( “ui” )

local isAndroid = “Android” == system.getInfo(“platformName”)

if isAndroid then
native.showAlert(“Not supported”, “Maps not yet supported on Android devices.”, {“OK”}, onComplete)
end

local locationNumber = 1


– This is where the longtitude and latitude is initialized

local currentLocation, currentLatitude, currentLongitude

local background = display.newImage( “bkg_grass.png”, true )
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2

shadow = display.newRect( 7, 7, 306, 226 )
shadow:setFillColor( 0, 0, 0, 120 )
local label = display.newText( “Maps not supported in Corona Simulator.”, 20, 70, native.systemFont, 14 )
local label2 = display.newText( “(Build for XCode Simulator or iOS device.)”, 20, 95, native.systemFont, 14 )
– Create a native MapView (requires XCode Simulator build or device build)
– You can create multiple maps, if you like…
myMap = native.newMapView( 20, 20, 300, 220 )
myMap.mapType = “normal” – other mapType options are “satellite” or “hybrid”

– 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( 37.331692, -122.030456 )
– A handler for the native keyboard
local fieldHandler = function( event )
– Hide keyboard when the user clicks “Return” in this field
if ( “submitted” == event.phase ) then
native.setKeyboardFocus( nil )
end
end

– A native text input field (requires XCode Simulator build or device build)
inputField = native.newTextField( 10, 252, 300, 28, fieldHandler )
inputField.font = native.newFont( native.systemFont, 16 )
inputField.text = “Broadway and Columbus, San Francisco” – example of searchable location
inputField:setTextColor( 45, 45, 45 )
– A function to handle the “mapAddress” event (also known as “reverse geocoding”)
local mapAddressHandler = function( event )

– Tried printing out the city but didn’t work

print("event: "…event.city)
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

– Create buttons and their functions:

local button1Release = function( event )
– This calls a Google web API to find the location of the submitted string
– Valid strings include addresses, intersections, and landmarks like “Golden Gate Bridge”, “Eiffel Tower” or “Buckingham Palace”
latitude, longitude = myMap:getAddressLocation( inputField.text )

– Move map so this location is at the center
– (The final parameter toggles map animation, which may not be visible if moving a large distance)
myMap:setCenter( latitude, longitude, true )

markerTitle = "Location " … locationNumber
locationNumber = locationNumber + 1

– Add a pin to the map at the new location
myMap:addMarker( latitude, longitude, { title=markerTitle, subtitle=inputField.text } )
end

local button2Release = function( event )
– Fetch the user’s current location
– Note: in XCode Simulator, the current location defaults to Apple headquarters in Cupertino, CA
currentLocation = myMap:getUserLocation()
currentLatitude = currentLocation.latitude
currentLongitude = currentLocation.longitude

– Move map so that current location is at the center
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 )
end

local button3Release = function( event )
myMap:removeAllMarkers()
locationNumber = 1 – reset counter for popup labels
end
local button1 = ui.newButton{
default = “buttonGreen.png”,
over = “buttonGreenOver.png”,
onRelease = button1Release,
text = “Find Location”,
emboss = true
}

local button2 = ui.newButton{
default = “buttonOrange.png”,
over = “buttonOrangeOver.png”,
onRelease = button2Release,
text = “Current Location”,
emboss = true
}

local button3 = ui.newButton{
default = “buttonRed.png”,
over = “buttonRedOver.png”,
onRelease = button3Release,
text = “Remove All Markers”,
emboss = true
}

button1.x = display.contentWidth / 2; button1.y = 320
button2.x = display.contentWidth / 2; button2.y = 380
button3.x = display.contentWidth / 2; button3.y = 440

– 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 )

Sorry for pasting this huge chunk of code but you can see that it is the same as the mapview example given. [import]uid: 39846 topic_id: 8548 reply_id: 30749[/import]

even if you create a sample app that just loads a dialog doesn’t work?

maybe sample is wrong
try adding an oncomplete event

local function onComplete( event )  
 if "clicked" == event.action then  
 local i = event.index  
 if 1 == i then  
 -- Do nothing; dialog will simply dismiss  
 elseif 2 == i then  
 -- Open URL if "Learn More" (the 2nd button) was clicked  
 system.openURL( "http://developer.anscamobile.com" )  
 end  
 end  
end  
   
-- Show alert with five buttons  
local alert = native.showAlert( "Corona", "Dream. Build. Ship.",   
 { "OK", "Learn More" }, onComplete )  
  

and see what happens
c. [import]uid: 24 topic_id: 8548 reply_id: 30752[/import]

Hi Carlos. I tried to compile it for the xCode simulator and the alert pop up works. Now this is really wierd. So it just doesn’t work on my Iphone 4 which is really puzzling now. Both Iphone and Xcode simulator software versions are the same 4.3.

Was the mapview example ever tried on the Iphone? Did everything worked? [import]uid: 39846 topic_id: 8548 reply_id: 30750[/import]

Thanks Carlos. I will try that. [import]uid: 39846 topic_id: 8548 reply_id: 30753[/import]

Hi Carlos. I tried what you said. It works when i put the alert outside of mapAddressHandler. However, it doesn’t work when i put it inside. I think it might be because mapAddressHandler didn’t run at all. i tried printing it something inside and it didnt show as well.


– alert is outside the mapAddressHandler. Shows right away.

local function onComplete( event )
if “clicked” == event.action then
local i = event.index
if 1 == i then
– Do nothing; dialog will simply dismiss
elseif 2 == i then
– Open URL if “Learn More” (the 2nd button) was clicked
system.openURL( “http://developer.anscamobile.com” )
end
end
end

– A function to handle the “mapAddress” event (also known as “reverse geocoding”)
local mapAddressHandler = function( event )
print(“ssss”)
locationText =
"Latitude: " … currentLatitude …
", Longitude: " … currentLongitude …
", Address: " … event.streetDetail … " " … event.street …
", " … event.city …
", " … event.region …
", " … event.country …
", " … event.postalCode
end

– Show alert with five buttons
local alert = native.showAlert( “Corona”, “Dream. Build. Ship.”,
{ “OK”, “Learn More” }, onComplete )


– alert is inside the mapAddressHandler. Didn’t show even after i clicked on the current location button.

local function onComplete( event )
if “clicked” == event.action then
local i = event.index
if 1 == i then
– Do nothing; dialog will simply dismiss
elseif 2 == i then
– Open URL if “Learn More” (the 2nd button) was clicked
system.openURL( “http://developer.anscamobile.com” )
end
end
end

– A function to handle the “mapAddress” event (also known as “reverse geocoding”)
local mapAddressHandler = function( event )
print(“ssss”)
locationText =
"Latitude: " … currentLatitude …
", Longitude: " … currentLongitude …
", Address: " … event.streetDetail … " " … event.street …
", " … event.city …
", " … event.region …
", " … event.country …
", " … event.postalCode

– Show alert with five buttons
local alert = native.showAlert( “Corona”, “Dream. Build. Ship.”,
{ “OK”, “Learn More” }, onComplete )
end

So my conclusion is that either one of these is not working.

myMap:nearestAddress( currentLatitude, currentLongitude )

Runtime:addEventListener( “mapAddress”, mapAddressHandler ) [import]uid: 39846 topic_id: 8548 reply_id: 30755[/import]

WHACK !

did you try

  
local currentLatitude, currentLongitude;  
  
currentLatitude, currentLongitude = myMap:getAddressLocation("Eiffel Tower" )  
  

[import]uid: 24 topic_id: 8548 reply_id: 30756[/import]

Hi Carlos. Thanks for trying to help me out here. I inserted the code in and the alert still won’t show. Below is the code i am using in the main.lua. Maybe you can try using it on your Iphone and see if it works.

local currentLatitude, currentLongitude

myMap = native.newMapView( 20, 20, 300, 220 )
myMap.mapType = “normal” – other mapType options are “satellite” or “hybrid”

– The MapView is just another Corona display object, and can be moved or rotated, etc.
myMap.x = display.contentWidth / 2
myMap.y = 120


– inserted the code here

currentLatitude, currentLongitude = myMap:getAddressLocation(“Eiffel Tower” )

myMap:setCenter( currentLatitude, currentLongitude )

local function onComplete( event )
if “clicked” == event.action then
local i = event.index
if 1 == i then
– Do nothing; dialog will simply dismiss
elseif 2 == i then
– Open URL if “Learn More” (the 2nd button) was clicked
system.openURL( “http://developer.anscamobile.com” )
end
end
end

– A function to handle the “mapAddress” event (also known as “reverse geocoding”)
local mapAddressHandler = function( event )
locationText =
"Latitude: " … currentLatitude …
", Longitude: " … currentLongitude …
", Address: " … event.streetDetail … " " … event.street …
", " … event.city …
", " … event.region …
", " … event.country …
", " … event.postalCode

– Show alert with five buttons
local alert = native.showAlert( “Corona”, “Dream. Build. Ship.”,
{ “OK”, “Learn More” }, onComplete )
end

myMap:nearestAddress( currentLatitude, currentLongitude )

Runtime:addEventListener( “mapAddress”, mapAddressHandler ) [import]uid: 39846 topic_id: 8548 reply_id: 30759[/import]

http://developer.anscamobile.com/content/mapview

i don’t know what else to tell you. i just ran it on iphone and ipad and ipad2 and alert shows

c. [import]uid: 24 topic_id: 8548 reply_id: 30818[/import]

Thanks Carlos. Appreciate the help. It would mean that my Iphone might have a problem. That tells a lot already. I will probably double check it with other devices. [import]uid: 39846 topic_id: 8548 reply_id: 30880[/import]

Hi, I’m using iOs 5.0 and this event listener is not working.

I already tried use it on Runtime or in my map.

There are any work around?

Thanks. [import]uid: 9482 topic_id: 8548 reply_id: 58627[/import]

I can’t this to work as well (iOS5). Seems like mapAddressHandler is not triggered. [import]uid: 70056 topic_id: 8548 reply_id: 63321[/import]

I ran into the same issue. After some trial and error I figured out that some of the event properties were coming in nil which was causing the mapAddressHandler to look like it was doing nothing.

If you’re having trouble, try replacing your map address handler with the following code, and check the console for results.

  
local mapAddressHandler = function( event )  
 print("IN MAP ADDRESS HANDLER")  
 if event.street ~= nil then print( event.street ) end  
 if event.streetDetail ~= nil then print( event.streetDetail ) end  
 if currentLatitude ~= nil then print( currentLatitude ) end  
 if currentLongitude ~= nil then print( currentLongitude ) end  
 if event.city ~= nil then print( event.city ) end  
 if event.cityDetail ~= nil then print( event.cityDetail) end  
 if event.region ~= nil then print( event.region) end   
 if event.country ~= nil then print( event.country) end  
 if event.postalCode ~= nil then print( event.postalCode) end  
end  

[import]uid: 112807 topic_id: 8548 reply_id: 81117[/import]

How are you running the Console and seeing GPS events from the code running on the iPhone, PaulTech? [import]uid: 79415 topic_id: 8548 reply_id: 86361[/import]

Hi

I have the same problem, the mapaddress handler does not trigger. And yes PaulTech, how do you see the prints on the device? This has to run on the device not simulator. I am using iphone 3g. Or are you using xcode simulator? Does that allow you to see the prints?
There has to be an explanation why event listener not triggering.

Carlos, you say that it runs on iphone and ipad? So event listerner is working for you? Could you post the code used?

Here is my code that does not work:

module(..., package.seeall);  
  
function new()  
 local localGroup = display.newGroup();  
  
 local bg = display.newImageRect("images/elpasoatnight.png", \_W,\_H);  
 bg:setReferencePoint(display.CenterReferencePoint);  
 bg.x = \_W/2; bg.y = \_H/2;  
 localGroup:insert(bg);  
--  
-- display credits  
--  
 credits = display.newText ( "Xela Development", 0, 0, "Helvetica-Bold", 12)  
 credits:setTextColor (255,128,255)  
 credits.x = 400; credits.y = 300;  
 localGroup:insert(credits);  
--  
-- back button  
--  
 local back = ui.newButton{  
 default = "images/buttonBlueSmall.png",  
 over = "images/buttonBlueSmallOver.png",  
 onPress = "menu",  
 text = "back",  
 size = 10,  
 emboss=true  
 }  
  
 back.x = 35; back.y = display.statusBarHeight-1;  
 back.scene = "menu"  
 localGroup:insert(back);  
--  
-- check place to join  
--  
 local places = ui.newButton{  
 default = "images/buttonBlueSmall.png",  
 over = "images/buttonBlueSmallOver.png",  
 onPress = "mapAddressHandler",  
 text = "places to join",  
 size = 9,  
 emboss=true  
 }  
  
 places.x = 105; places.y = display.statusBarHeight-1;  
 places.scene = "mapAddressHandler"  
 localGroup:insert(places);  
--  
-- setup for finding my address  
--  
 myMap = native.newMapView( 20, 20, 300, 220 )  
 myMap.mapType = "standard"   
 myMap:nearestAddress( 38.898748, -77.037684)  
 myMap.x = display.contentWidth / 2  
 myMap.y = 120  
 localGroup:insert(myMap);   
--  
-- display my address  
--  
 local function mapAddressHandler( event )  
 -- handle mapAddress event here  
 print( "Check in address: " .. event.city .. ", " .. event.country )  
 end  
--  
-- event listener  
--  
  
Runtime:addEventListener( "mapAddress", mapAddressHandler )  
 return localGroup;  
end  

Thanks in advance [import]uid: 95689 topic_id: 8548 reply_id: 98655[/import]

And yes PaulTech, how do you see the prints on the device? This has to run on the device not simulator. I am using iphone 3g. Or are you using xcode simulator? Does that allow you to see the prints?

In the simulator you can look at the console app if you are on OSX.

edit:

If you don’t want to be bothered with the console, you can always print the value to the screen. Maybe try something like:

[code]
local address = display.newText("", 300, 100, null, 24)

–then later on in your mapAddressHandler

if event.city ~= nil then address.text = event.city end
[/code] [import]uid: 112807 topic_id: 8548 reply_id: 98661[/import]