native.newMapView()

类型 函数
native.*
返回值 地图
修订 版本 2024.3703
关键词 地图,地图视图,地图对象
另请参阅 mapAddress
mapLocation
mapMarker

概述

在指定的边界内渲染地图视图,并返回一个显示对象包装器。在地图视图上,当您添加 mapLocation 事件监听器时,可以使用触摸事件。

注意事项

通用

  • 此功能仅在 Android 和 iOS 上受支持。

  • 原生地图视图不属于 OpenGL 画布,也不遵循显示对象层次结构,因此它们将始终显示在普通显示对象(包括图像、文本和矢量对象)的前面。

Android

Google 现在要求您提供 API 密钥才能在 Android 上使用地图。您可以在此处获取 API 密钥

在 Android 上,您必须将 INTERNET 权限添加到 build.settings 文件中才能显示地图。您可以选择添加位置权限以在地图中显示当前位置。

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

            --optional permission used to display current location via the GPS
            "android.permission.ACCESS_FINE_LOCATION",

            --optional permission used to display current location via WiFi or cellular service
            "android.permission.ACCESS_COARSE_LOCATION",
        },
        usesFeatures =
        {
            -- If you set permissions "ACCESS_FINE_LOCATION" and "ACCESS_COARSE_LOCATION" above,
            -- you may want to set up your app to not require location services as follows.
            -- Otherwise, devices that do not have location sevices (such as a GPS) will be unable
            -- to purchase this app in the app store.
            { name = "android.hardware.location", required = false },
            { name = "android.hardware.location.gps", required = false },
            { name = "android.hardware.location.network", required = false }
        },
    },
}

此外,您需要将 API 密钥添加到 config.lua 文件中。

application =
{
    license =
    {
        google =
        {
            mapsKey = "YOUR_MAPS_API_KEY",
        },
    },
}

iOS

在 iOS 上,您必须在 build.settingsplist 表中包含以下键/描述。当系统提示用户允许访问时,相关的描述将作为警报的一部分显示。请注意,这些描述可以根据您的喜好进行自定义,甚至可以进行本地化(指南)。

settings =
{
    iphone =
    {
        plist =
        {
            NSLocationAlwaysUsageDescription = "This app would like to use location services.",
            NSLocationWhenInUseUsageDescription = "This app would like to use location services.",
        },
    },
}

语法

native.newMapView( x, y, width, height )
x (必填)

数字. 对应于地图视图对象中心的 x 坐标。

y (必填)

数字. 对应于地图视图对象中心的 y 坐标。

width (必填)

数字. 地图视图对象的宽度。

height (必填)

数字. 地图视图对象的高度。

属性/方法

有关方法和属性列表,请参阅 地图 文档。

事件

有关与各种 地图 对象事件相关的属性,请参阅 mapAddressmapLocationmapMarker 事件文档。

示例

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

-- Display map as vector drawings of streets (other options are "satellite" and "hybrid")
myMap.mapType = "standard"

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