一个提供对 Android 上的 Google 地图和 iOS 上的 Apple 地图的访问的库。
Expo Maps 目前处于 alpha 阶段,可能会发生重大变化。它在 Expo Go 应用中不可用。
¥Installation
-
npx expo install expo-maps
If you are installing this in an existing React Native app, start by installing expo
in your project. Then, follow the additional instructions as mentioned by the library's README under "Installation in bare React Native projects" section.
¥Configuration
Expo Maps 提供对 Android 和 iOS 上平台原生地图 API 的访问。
¥Expo Maps provides access to the platform native map APIs on Android and iOS.
Apple Maps(仅适用于
¥Apple Maps (available on
Google 地图(仅在
¥Google Maps (available on
¥Google Cloud API setup
在你可以在 Android 上使用 Google 地图之前,你需要注册一个 Google Cloud API 项目,启用 Android 版 Maps SDK,并将相关配置添加到你的 Expo 项目。
¥Before you can use Google Maps on Android, you need to register a Google Cloud API project, enable the Maps SDK for Android, and add the associated configuration to your Expo project.
如果你已经在 Android 上注册了另一个 Google 服务的项目(例如 Google Sign In),则可以在你的项目上启用 Maps SDK for Android 并跳转到步骤 4。
¥If you have already registered a project for another Google service on Android, such as Google Sign In, you enable the Maps SDK for Android on your project and jump to step 4.
1
注册 Google Cloud API 项目并启用适用于 Android 的 Maps SDK
¥Register a Google Cloud API project and enable the Maps SDK for Android
打开浏览器到 谷歌 API 管理器 并创建一个项目。
¥Open your browser to the Google API Manager and create a project.
创建完成后,转到该项目并启用适用于 Android 的 Maps SDK。
¥Once it's created, go to the project and enable the Maps SDK for Android.
2
复制应用的 SHA-1 证书指纹
¥Copy your app's SHA-1 certificate fingerprint
如果你要将应用部署到 Google Play 商店,则需要至少 将你的应用二进制文件上传到 Google Play 控制台 一次。这是 Google 生成你的应用签名凭据所必需的。
¥If you are deploying your app to the Google Play Store, you'll need to upload your app binary to Google Play console at least once. This is required for Google to generate your app signing credentials.
转至 谷歌游戏控制台 >(你的应用)> 发布 > 设置 > 应用完整性 > 应用签名。
¥Go to the Google Play Console > (your app) > Release > Setup > App integrity > App Signing.
复制 SHA-1 证书指纹的值。
¥Copy the value of SHA-1 certificate fingerprint.
如果你已经创建了 开发构建,你的项目将使用调试密钥库进行签名。
¥If you have already created a development build, your project will be signed using a debug keystore.
构建完成后,转到 项目的仪表板,然后在配置 > 单击凭据下。
¥After the build is complete, go to your project's dashboard, then, under Configure > click Credentials.
在“应用标识符”下,单击项目的包名称,然后在“Android 密钥库”下复制 SHA-1 证书指纹的值。
¥Under Application Identifiers, click your project's package name and under Android Keystore copy the value of SHA-1 Certificate Fingerprint.
3
创建 API 密钥
¥Create an API key
转到 谷歌云凭证管理器 并单击“创建凭证”,然后单击“API 密钥”。
¥Go to Google Cloud Credential manager and click Create Credentials, then API Key.
在模式中,单击“编辑 API 密钥”。
¥In the modal, click Edit API key.
在关键限制 > 应用限制下,选择 Android 应用。
¥Under Key restrictions > Application restrictions, choose Android apps.
在限制使用 Android 应用下,单击添加项目。
¥Under Restrict usage to your Android apps, click Add an item.
将 app.json 中的 android.package
(例如:com.company.myapp
)添加到包名称字段。
¥Add your android.package
from app.json (for example: com.company.myapp
) to the package name field.
然后,添加步骤 2 中的 SHA-1 证书指纹值。
¥Then, add the SHA-1 certificate fingerprint's value from step 2.
单击“完成”,然后单击“保存”。
¥Click Done and then click Save.
4
将 API 密钥添加到你的项目中
¥Add the API key to your project
将你的 API 密钥复制到 android.config.googleMaps.apiKey
字段下的 app.json 中。
¥Copy your API Key into your app.json under the android.config.googleMaps.apiKey
field.
创建一个新的开发版本,现在你可以使用 Android 上的 Google Maps API 和 expo-maps
。
¥Create a new development build, and you can now use the Google Maps API on Android with expo-maps
.
¥Permissions
要在地图上显示用户的位置,你需要事先声明并请求位置权限。如果你在项目中使用配置插件(EAS 构建 或 npx expo run:[android|ios]
),则可以使用内置 配置插件 进行配置。该插件允许你配置无法在运行时设置的各种属性,并且需要构建新的应用二进制文件才能生效。
¥To display the user's location on the map, you need to declare and request location permission beforehand. You can configure this using the built-in config plugin if you use config plugins in your project (EAS Build or npx expo run:[android|ios]
). The plugin allows you to configure various properties that cannot be set at runtime and require building a new app binary to take effect.
{
"expo": {
"plugins": [
[
"expo-maps",
{
"requestLocationPermission": "true",
"locationPermission": "Allow $(PRODUCT_NAME) to use your location"
}
]
]
}
}
Name | Default | Description |
---|---|---|
requestLocationPermission | false | A boolean to add permissions to AndroidManifest.xml and Info.plist. |
locationPermission | "Allow $(PRODUCT_NAME) to use your location" | Only for: iOS A string to set the |
¥Usage
import { AppleMaps, GoogleMaps } from 'expo-maps';
import { Platform, Text } from 'react-native';
export default function App() {
if (Platform.OS === 'ios') {
return <AppleMaps.View style={{ flex: 1 }} />;
} else if (Platform.OS === 'android') {
return <GoogleMaps.View style={{ flex: 1 }} />;
} else {
return <Text>Maps are only available on Android and iOS</Text>;
}
}
import { AppleMaps, GoogleMaps } from 'expo-maps';
// ApplesMaps.View and GoogleMaps.View are the React components
Type: React.Element<AppleMapsViewProps>
(event: {
bearing: number,
coordinates: Coordinates,
tilt: number,
zoom: number
}) => void
Lambda invoked when the map was moved by the user.
event: {
bearing: number,
coordinates: Coordinates,
tilt: number,
zoom: number
}
(event: {
coordinates: Coordinates
}) => void
Lambda invoked when the user clicks on the map. It won't be invoked if the user clicks on POI or a marker.
event: {
coordinates: Coordinates
}
(event: AppleMapsMarker) => void
Lambda invoked when the marker is clicked
event: AppleMapsMarker
StyleProp<ViewStyle>
AppleMapsUISettings
The MapUiSettings
to be used for UI-specific settings on the map.
Type: React.Element<Omit<GoogleMapsViewProps, 'ref'>>
(event: {
bearing: number,
coordinates: Coordinates,
tilt: number,
zoom: number
}) => void
Lambda invoked when the map was moved by the user.
event: {
bearing: number,
coordinates: Coordinates,
tilt: number,
zoom: number
}
(event: {
coordinates: Coordinates
}) => void
Lambda invoked when the user clicks on the map. It won't be invoked if the user clicks on POI or a marker.
event: {
coordinates: Coordinates
}
(event: {
coordinates: Coordinates
}) => void
Lambda invoked when the user long presses on the map.
event: {
coordinates: Coordinates
}
(event: GoogleMapsMarker) => void
Lambda invoked when the marker is clicked
event: GoogleMapsMarker
(event: {
coordinates: Coordinates,
name: string
}) => void
Lambda invoked when a POI is clicked.
event: {
coordinates: Coordinates,
name: string
}
Ref<GoogleMapsViewType>
StyleProp<ViewStyle>
GoogleMapsUISettings
The MapUiSettings
to be used for UI-specific settings on the map.
GoogleMapsUserLocation
User location, overrides default behavior.
Type: React.Element<GoogleStreetViewProps>
Coordinates
StyleProp<ViewStyle>
Parameter | Type |
---|---|
options(optional) | PermissionHookOptions<object> |
Check or request permissions to access the location.
This uses both requestPermissionsAsync
and getPermissionsAsync
to interact with the permissions.
[null | PermissionResponse, RequestPermissionMethod<PermissionResponse>, GetPermissionMethod<PermissionResponse>]
Example
const [status, requestPermission] = useLocationPermissions();
Checks user's permissions for accessing location.
Promise<PermissionResponse>
A promise that fulfills with an object of type PermissionResponse
.
Asks the user to grant permissions for location.
Promise<PermissionResponse>
A promise that fulfills with an object of type PermissionResponse
.
Type: AppleMapsMarker
extended by:
Property | Type | Description |
---|---|---|
backgroundColor(optional) | string | The background color of the annotation. |
icon(optional) | SharedRefType<'image'> | The custom icon to display in the annotation. |
text(optional) | string | The text to display in the annotation. |
textColor(optional) | string | The text color of the annotation. |
Property | Type | Description |
---|---|---|
coordinates(optional) | Coordinates | The coordinates of the marker. |
systemImage(optional) | string | The SF Symbol to display for the marker. |
tintColor(optional) | string | The tint color of the marker. |
title(optional) | string | The title of the marker, displayed in the callout when the marker is clicked. |
Property | Type | Description |
---|---|---|
isTrafficEnabled(optional) | boolean | Whether the traffic layer is enabled on the map. |
mapType(optional) | AppleMapsMapType | Defines which map type should be used. |
selectionEnabled(optional) | boolean | If true, the user can select a location on the map to get more information. |
Property | Type | Description |
---|---|---|
compassEnabled(optional) | boolean | Whether the compass is enabled on the map. If enabled, the compass is only visible when the map is rotated. |
myLocationButtonEnabled(optional) | boolean | Whether the my location button is visible. |
scaleBarEnabled(optional) | boolean | Whether the scale bar is displayed when zooming. |
togglePitchEnabled(optional) | boolean | Whether the user is allowed to change the pitch type. |
Property | Type | Description |
---|---|---|
coordinates(optional) | Coordinates | The middle point of the camera. |
zoom(optional) | number | The zoom level of the camera. For some view sizes, lower zoom levels might not be available. |
Property | Type | Description |
---|---|---|
latitude(optional) | number | The latitude of the coordinate. |
longitude(optional) | number | The longitude of the coordinate. |
Property | Type | Description |
---|---|---|
coordinates(optional) | Coordinates | The coordinates of the marker. |
draggable(optional) | boolean | Whether the marker is draggable. |
icon(optional) | SharedRefType<'image'> | The custom icon to display for the marker. |
showCallout(optional) | boolean | Whether the callout should be shown when the marker is clicked. |
snippet(optional) | string | The snippet of the marker, Displayed in the callout when the marker is clicked. |
title(optional) | string | The title of the marker, displayed in the callout when the marker is clicked. |
Property | Type | Description |
---|---|---|
isBuildingEnabled(optional) | boolean | Whether the building layer is enabled on the map. |
isIndoorEnabled(optional) | boolean | Whether the indoor layer is enabled on the map. |
isMyLocationEnabled(optional) | boolean | Whether finding the user's location is enabled on the map. |
isTrafficEnabled(optional) | boolean | Whether the traffic layer is enabled on the map. |
mapType(optional) | GoogleMapsMapType | Defines which map type should be used. |
maxZoomPreference(optional) | number | The maximum zoom level for the map. |
minZoomPreference(optional) | number | The minimum zoom level for the map. |
selectionEnabled(optional) | boolean | If true, the user can select a location on the map to get more information. |
Property | Type | Description |
---|---|---|
compassEnabled(optional) | boolean | Whether the compass is enabled on the map. If enabled, the compass is only visible when the map is rotated. |
indoorLevelPickerEnabled(optional) | boolean | Whether the indoor level picker is enabled . |
mapToolbarEnabled(optional) | boolean | Whether the map toolbar is visible. |
myLocationButtonEnabled(optional) | boolean | Whether the my location button is visible. |
rotationGesturesEnabled(optional) | boolean | Whether rotate gestures are enabled. |
scaleBarEnabled(optional) | boolean | Whether the scale bar is displayed when zooming. |
scrollGesturesEnabled(optional) | boolean | Whether the scroll gestures are enabled. |
scrollGesturesEnabledDuringRotateOrZoom(optional) | boolean | Whether the scroll gestures are enabled during rotation or zoom. |
tiltGesturesEnabled(optional) | boolean | Whether the tilt gestures are enabled. |
togglePitchEnabled(optional) | boolean | Whether the user is allowed to change the pitch type. |
zoomControlsEnabled(optional) | boolean | Whether the zoom controls are visible. |
zoomGesturesEnabled(optional) | boolean | Whether the zoom gestures are enabled. |
Property | Type | Description |
---|---|---|
coordinates | Coordinates | User location coordinates. |
followUserLocation | boolean | Should the camera follow the users' location. |
Property | Type | Description |
---|---|---|
setCameraPosition | (config: SetCameraPositionConfig) => void | Update camera position. config: SetCameraPositionConfig New camera position config. |
Type: CameraPosition
extended by:
Property | Type | Description |
---|---|---|
duration(optional) | number | The duration of the animation in milliseconds. |
The type of map to display.
AppleMapsMapType.HYBRID = "HYBRID"
A satellite image of the area with road and road name layers on top.
The type of map to display.
GoogleMapsMapType.HYBRID = "HYBRID"
Satellite imagery with roads and points of interest overlayed.
¥Permissions
¥Android
要在地图上显示用户的位置,expo-maps
库需要以下权限:
¥To show the user's location on the map, the expo-maps
library requires the following permissions:
ACCESS_COARSE_LOCATION
:获取设备的大概位置
¥ACCESS_COARSE_LOCATION
: for approximate device location
ACCESS_FINE_LOCATION
:用于精确的设备定位
¥ACCESS_FINE_LOCATION
: for precise device location
Android Permission | Description |
---|---|
Allows an app to access approximate location.
| |
Allows an app to access precise location.
| |
Allows a regular application to use Service.startForeground.
| |
Allows a regular application to use Service.startForeground with the type "location".
| |
Allows an app to access location in the background.
|
¥iOS
该库使用以下用法描述键:
¥The following usage description keys are used by this library: