That doesn’t work - see my reworked mapview code below. You can test this in xcode simulator
–
– Abstract: MapView sample project
– Demonstrates the API for embedded maps and location data (currently iOS-only)
– Version: 1.0
– Sample code is MIT licensed, see http://developer.anscamobile.com/code/license
– Copyright © 2010 ANSCA Inc. All Rights Reserved.
display.setStatusBar( display.HiddenStatusBar )
– Load external Lua libraries (from project directory)
local ui = require( “ui” )
local locationNumber = 1 – a counter to display on location labels
local currentLocation, currentLatitude, currentLongitude
local background = display.newImage( “bkg_grass.png”, true )
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2
local label = display.newText( “Maps not supported in Corona Simulator.”, 20, 70, native.systemFont, 14 )
label:setTextColor( 255, 255, 255 )
local label2 = display.newText( “(Build for XCode Simulator or iOS device.)”, 20, 95, native.systemFont, 14 )
label2:setTextColor( 255, 255, 255 )
– 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 )
mymapgroup = display.newGroup()
mymapgroup:insert( myMap )
shadow = display.newRect( 7, 7, 306, 326 )
shadow:setFillColor( 0, 0, 0, 120 )
– 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 )
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 ) [import]uid: 31718 topic_id: 7039 reply_id: 24745[/import]