Map View

Features demonstrated:

The API for embedded maps and location data (currently iOS-only)

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
 
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 )
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 )
 
 
-- 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 )