object:addMarker()

Type Function
Object Map
Library native.*
Return value Number
Revision 2014.2524
Keywords addMarker
See also object:removeMarker()
object:removeAllMarkers()

Overview

Adds a pin to the map at the specified location. The optional title and subtitle will appear on a small popup when the pin is touched. If a custom image is specified then the bottom center of the image will be the pinned location.

This function returns an identification number for the marker added, or nil if there was a failure. This number can be used along with object:removeMarker() to remove a marker.

Syntax

object:addMarker( latitude, longitude )
object:addMarker( latitude, longitude, options )
latitude (required)

Number. The marker's latitude in degrees.

longitude (required)

Number. The marker's longitude in degrees.

options (optional)

Table. A table of options for the marker — see the next section for details.

Options Reference

options.title (optional)

String. The title to display on the popup that appears when the user taps the marker.

options.subtitle (optional)

String. The subtitle to display on the popup that appears when the user taps the marker.

options.listener (optional)

Function. The listener that will be called when the marker is tapped.

options.imageFile (optional)

String. The file name of the image to be used for the marker. The default base directory is the resource directory. It can also be a Table of the form { baseDir=, filename= }. This will use dynamic image selection. The coordinates are returned as a mapMarker.

Example

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

local myMap = native.newMapView( 0, 0, 300, 220 )
myMap.x = display.contentCenterX
myMap.y = display.contentCenterY

-- This will look in the resources directory for the image file.
local options = 
{ 
    title = "Displayed Title", 
    subtitle = "subtitle text", 
    listener = markerListener, 
    imageFile =  "someImage.png",
}

-- Alternatively, this looks in the specified directory for the image file.
--[[
local options = 
{ 
    title = "Displayed Title", 
    subtitle = "subtitle text", 
    listener = markerListener, 
    imageFile = 
    {
        filename = "someImage.png",
        baseDir = system.TemporaryDirectory
    }
}
--]]

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