object:nearestAddress()

类型 函数
对象 Map
native.*
返回值
修订 版本 2024.3703
关键字 nearestAddress
另请参阅 mapAddress

概述

根据给定的纬度和经度值返回最近的地址,以 mapAddress 事件的形式返回。

语法

object:nearestAddress( latitude, longitude, resultHandler )
latitude (必需)

Number. 所需位置的纬度,以度为单位。

longitude (必需)

Number. 所需位置的经度,以度为单位。

resultHandler (可选)

Listener.mapAddress 事件调用的侦听器函数。

注意点

Android

在 Android 上,你必须向 build.settings 文件添加 INTERNET 权限。

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

iOS

从 iOS 8 开始,你必须向 build.settings 文件的 plist 部分添加 NSLocationWhenInUseUsageDescription 键,其中包含你需要访问定位服务的理由。

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

示例

-- 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 )