object:requestLocation()

类型 函数
对象 地图
native.*
返回值
修订 发行版 2024.3703
关键词 requestLocation
另请参见 mapLocation

概览

这是已弃用的替代品 object:getAddressLocation()

返回给定地址字符串的数字纬度和经度值。坐标作为 mapLocation 事件返回。然后可以使用坐标在地图上放置标记、重新居中地图到所需位置,或者执行使用纬度和经度对的任何其他功能。

此函数将接受几乎任何地址或交叉路口格式作为输入,以及一些著名地标的名称。

语法

object:requestLocation( location, resultHandler )
location (必需)

字符串 地址、交叉路口或地标。

resultHandler (可选)

侦听器mapLocation 事件调用的侦听器函数。

## 需要特别注意的

安卓

在安卓上,必须向 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( "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 )