object:requestLocation()

Type Function
Object Map
Library native.*
Return value none
Revision Release 2022.3683
Keywords requestLocation
See also mapLocation

Overview

This is a replacement for the deprecated object:getAddressLocation().

Returns the numerical latitude and longitude values of the given location string. The coordinates are returned as a mapLocation event. The coordinates can then be used to place a marker on the map, re-center the map to the desired location, or perform any of the other functions that use a latitude and longitude pair.

This function will accept virtually any address or intersection format as input, along with the names of some famous landmarks.

Syntax

object:requestLocation( location, resultHandler )
location (required)

String. The address, intersection, or landmark.

resultHandler (optional)

Listener. The listener function to be invoked for the mapLocation 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( "The specified string is at: " .. event.latitude .. "," .. event.longitude )
        myMap:setCenter( event.latitude, event.longitude )
    end

end

myMap:requestLocation( "1900 Embarcadero Road, Palo Alto, CA", locationHandler )