object:nearestAddress()

Type Function
Object Map
Library native.*
Return value none
Revision Release 2024.3703
Keywords nearestAddress
See also mapAddress

Overview

Returns the nearest address based on the given latitude and longitude values, returned as a mapAddress event.

Syntax

object:nearestAddress( latitude, longitude, resultHandler )
latitude (required)

Number. The latitude in degrees of the desired location.

longitude (required)

Number. The longitude in degrees of the desired location.

resultHandler (optional)

Listener. The listener function to be invoked for the mapAddress event.

Gotchas

Android

On Android, you must add the INTERNET permission to the build.settings file.

settings =
{
   android =
   {
      usesPermissions =
      {
         "android.permission.INTERNET",
      },
   },
}

iOS

Starting with iOS 8, you must add the NSLocationWhenInUseUsageDescription key to the plist section of the build.settings file which contains the reason why you need access to location services.

settings =
{
    iphone =
    {
        plist =
        {
            NSLocationWhenInUseUsageDescription = "A description of why the app needs access to location services."
        },
    },
}

Example

-- Create a native map view
local myMap = native.newMapView( 20, 20, 280, 360 )
myMap.x = display.contentCenterX
myMap.y = display.contentCenterY

local function locationHandler( event )

    if ( event.isError ) then
        print( "Map Error: " .. event.errorMessage )
    else
        print( "Nearest location: " .. event.city .. ", " .. event.country )
    end

end

-- Initialize map to a real location
myMap:setCenter( 37.331692, -122.030456 )

myMap:nearestAddress( 38.898748, -77.037684, locationHandler )