尝试索引本地'myMap'(零值)

时间:2014-02-22 08:18:29

标签: android lua corona

我刚开始使用corona sdk.

我正在尝试添加引脚到地图视图,当我在代码中添加它时,我一直收到此错误

  

尝试索引本地'myMap'(零值)

我看了一些东西,有人有类似的问题,他们只需要将本地myMap添加到顶部。我试过了,但没用。我希望有人能帮助我找出问题所在。

感谢您的帮助=)

local widget = require( "widget" )
local myMap

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 shadow = display.newRect( 7, 7, 306, 226 )
shadow.anchorX = 0.0        -- TopLeft anchor
shadow.anchorY = 0.0        -- TopLeft anchor
shadow:setFillColor( 0, 0, 0, 120/255 )

local simulatorMessage = "Maps not supported in Corona Simulator.\n\nYou must build for iOS or Android to test MapView support."
local label = display.newText( simulatorMessage, 20, 70, shadow.contentWidth - 10, 0, native.systemFont, 14 )
label.anchorX = 0.0     -- TopLeft anchor
label.anchorY = 0.0     -- TopLeft anchor


local myMap = native.newMapView( 20, 20, display.contentWidth, display.actualContentHeight*.7 )

if myMap then
    myMap.mapType = "normal"
    myMap.x = display.contentWidth / 2
    myMap.y = 400
    myMap:setCenter( 37.331692, -122.030456 )
end

local function markerListener(event)
    print("type: ", event.type) -- event
    print("markerId: ", event.markerId) -- if of the marker that was touched
    print("lat: ", event.latitude) -- latitude of the marker
    print("long: ", event.longitude) -- longitude of the marker
end

local options = 
{ 
    title = "Displayed Title", 
    subtitle = "subtitle text", 
    listener = markerListener, 
    imageFile = 
    {
        filename = "someImage.png",
        baseDir = system.TemporaryDirectory
    }
}

local options1 = 
{ 
    title = "Displayed Title", 
    subtitle = "subtitle text", 
    listener = markerListener, 
    imageFile =  "someImage.png",
}

local result, errorMessage = myMap:addMarker( 37.331692, -122.030456, options )
if result then
    print("everything went well")
else
    print(errorMessage)
end

-- A function to handle the "mapAddress" event (also known as "reverse geocoding", ie: coordinates -> string).
local mapAddressHandler = function( event )
    if event.isError then
        -- Failed to receive location information.
        native.showAlert( "Error", event.errorMessage, { "OK" } )
    else
        -- Location information received. Display it.
        local locationText =
                "Latitude: " .. currentLatitude .. 
                ", Longitude: " .. currentLongitude ..
                ", Address: " .. ( event.streetDetail or "" ) ..
                " " .. ( event.street or "" ) ..
                ", " .. ( event.city or "" ) ..
                ", " .. ( event.region or "" ) ..
                ", " .. ( event.country or "" ) ..
                ", " .. ( event.postalCode or "" )
        native.showAlert( "You Are Here", locationText, { "OK" } )
    end
end

-- A function to handle the "mapLocation" event (also known as "forward geocoding", ie: string -> coordinates).
local mapLocationHandler = function( event )
    if event.isError then
        -- Location name not found.
        native.showAlert( "Error", event.errorMessage, { "OK" } )
    else
        -- 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( event.latitude, event.longitude, true )

        -- Add a pin to the map at the new location
        markerTitle = "Location " .. locationNumber
        locationNumber = locationNumber + 1
        myMap:addMarker( event.latitude, event.longitude, { title=markerTitle, subtitle=inputField.text } )
    end
end

1 个答案:

答案 0 :(得分:0)

可能是local myMap = native.newMapView( 20, 20, display.contentWidth, display.actualContentHeight*.7 )返回nil或者函数将myMap设置为nil。

我注意到在两个地方你引用myMap而不先检查nil,不像函数的顶部:

local result, errorMessage = myMap:addMarker( 37.331692, -122.030456, options )

myMap:setCenter( event.latitude, event.longitude, true )

这两个地方之一可能会爆炸。我会检查local myMap = native.newMapView( 20, 20, display.contentWidth, display.actualContentHeight*.7 )是否失败,或者你的某个函数是否将myMap设置为nil。