diff --git a/android/app/build.gradle b/android/app/build.gradle index f84aa7b..b774827 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -78,7 +78,7 @@ import com.android.build.OutputFile */ project.ext.react = [ - enableHermes: false, // clean and rebuild if changing + enableHermes: true, // clean and rebuild if changing ] apply from: "../../node_modules/react-native/react.gradle" @@ -91,7 +91,7 @@ apply from: "../../node_modules/react-native/react.gradle" * Upload all the APKs to the Play Store and people will download * the correct one based on the CPU architecture of their device. */ -def enableSeparateBuildPerCPUArchitecture = false +def enableSeparateBuildPerCPUArchitecture = true /** * Run Proguard to shrink the Java bytecode in release builds. @@ -114,22 +114,22 @@ def jscFlavor = 'org.webkit:android-jsc:+' /** * Whether to enable the Hermes VM. * - * This should be set on project.ext.react and mirrored here. If it is not set + * This should be set on project.ext.react and that value will be read here. If it is not set * on project.ext.react, JavaScript will not be compiled to Hermes Bytecode * and the benefits of using Hermes will therefore be sharply reduced. */ def enableHermes = project.ext.react.get("enableHermes", false); +/** + * Architectures to build native code for in debug. + */ +def nativeArchitectures = project.getProperties().get("reactNativeDebugArchitectures") + android { ndkVersion rootProject.ext.ndkVersion compileSdkVersion rootProject.ext.compileSdkVersion - compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 - } - defaultConfig { applicationId "com.rnamapgeolocation" minSdkVersion rootProject.ext.minSdkVersion @@ -156,6 +156,11 @@ android { buildTypes { debug { signingConfig signingConfigs.debug + if (nativeArchitectures) { + ndk { + abiFilters nativeArchitectures.split(',') + } + } } release { // Caution! In production, you need to generate your own keystore file. @@ -191,7 +196,7 @@ dependencies { implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0" debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") { - exclude group:'com.facebook.fbjni' + exclude group:'com.facebook.fbjni' } debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") { @@ -215,7 +220,7 @@ dependencies { // Run this once to be able to run the application with BUCK // puts all compile dependencies into folder libs for BUCK to use task copyDownloadableDepsToLibs(type: Copy) { - from configurations.compile + from configurations.implementation into 'libs' } diff --git a/android/app/src/main/res/drawable/rn_edit_text_material.xml b/android/app/src/main/res/drawable/rn_edit_text_material.xml new file mode 100644 index 0000000..f35d996 --- /dev/null +++ b/android/app/src/main/res/drawable/rn_edit_text_material.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + diff --git a/android/app/src/main/res/values/styles.xml b/android/app/src/main/res/values/styles.xml index 9fab0be..7ba83a2 100644 --- a/android/app/src/main/res/values/styles.xml +++ b/android/app/src/main/res/values/styles.xml @@ -3,7 +3,7 @@ diff --git a/android/build.gradle b/android/build.gradle index 93232f5..ca5830a 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -2,18 +2,18 @@ buildscript { ext { - buildToolsVersion = "29.0.3" + buildToolsVersion = "30.0.2" minSdkVersion = 21 - compileSdkVersion = 29 - targetSdkVersion = 29 - ndkVersion = "20.1.5948944" + compileSdkVersion = 30 + targetSdkVersion = 30 + ndkVersion = "21.1.6352462" } repositories { google() - jcenter() + mavenCentral() } dependencies { - classpath("com.android.tools.build:gradle:4.1.0") + classpath("com.android.tools.build:gradle:4.2.2") // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } @@ -21,7 +21,6 @@ buildscript { allprojects { repositories { - mavenLocal() maven { // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm url("$rootDir/../node_modules/react-native/android") @@ -30,9 +29,14 @@ allprojects { // Android JSC is installed from npm url("$rootDir/../node_modules/jsc-android/dist") } - + mavenCentral { + // We don't want to fetch react-native from Maven Central as there are + // older versions over there. + content { + excludeGroup "com.facebook.react" + } + } google() - jcenter() maven { url 'https://www.jitpack.io' } } } diff --git a/android/gradle.properties b/android/gradle.properties index d21d03f..11d6e02 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -9,7 +9,7 @@ # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. -# Default value: -Xmx10248m -XX:MaxPermSize=256m +# Default value: -Xmx1024m -XX:MaxPermSize=256m # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 # When configured, Gradle will run in incubating parallel mode. @@ -25,4 +25,4 @@ android.useAndroidX=true android.enableJetifier=true # Version of flipper SDK to use with React Native -FLIPPER_VERSION=0.75.1 +FLIPPER_VERSION=0.99.0 diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties index be52383..ffed3a2 100644 --- a/android/gradle/wrapper/gradle-wrapper.properties +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/index.js b/index.tsx similarity index 96% rename from index.js rename to index.tsx index bd3e0f7..b62437f 100644 --- a/index.js +++ b/index.tsx @@ -1,20 +1,22 @@ -import React from "react"; +import * as React from "react"; import { AppRegistry, Button, + PermissionsAndroid, + Platform, + ScrollView, StyleSheet, Text, View, - ScrollView, - PermissionsAndroid, - Platform, } from "react-native"; import { - init, Geolocation, + init, + Position, + PositionError, setInterval, - setNeedAddress, setLocatingWithReGeocode, + setNeedAddress, } from "react-native-amap-geolocation"; const style = StyleSheet.create({ @@ -40,6 +42,7 @@ const style = StyleSheet.create({ class App extends React.Component { state = { location: null }; + watchId?: number | null; async componentDidMount() { if (Platform.OS === "android") { @@ -55,13 +58,8 @@ class App extends React.Component { }); } - componentWillUnmount() { - stop(); - } - - updateLocationState(location) { + updateLocationState(location: Position | PositionError) { if (location) { - location.updateTime = new Date().toLocaleString(); this.setState({ location }); console.log(location); } diff --git a/license b/license index 23153f1..b63749f 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2020 7c00 +Copyright (c) 2022 7c00 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/package.json b/package.json index a631827..f93460c 100644 --- a/package.json +++ b/package.json @@ -21,10 +21,8 @@ "lib/android/src", "lib/android/build.gradle" ], - "main": "lib/js", + "main": "src", "scripts": { - "dev": "tsc -w", - "build": "tsc", "build-docs": "npx typedoc --out docs/api --excludeNotExported --module commonjs --mode file src", "start": "react-native start", "reload": "adb reverse tcp:8081 tcp:8081 && adb shell input text rr", @@ -33,8 +31,9 @@ "ios": "react-native run-ios" }, "devDependencies": { - "react": "17.0.1", - "react-native": "0.64.0" + "@types/react-native": "^0.66.16", + "react": "17.0.2", + "react-native": "0.67.2" }, "prettier": { "printWidth": 100 diff --git a/readme.md b/readme.md index b3e14ea..17b1dff 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -# react-native-amap-geolocation [![npm version][version-badge]][npm] [![build status][build-badge]][build] +# react-native-amap-geolocation [![][version-badge]][npm] [![][build-badge]][build] React Native 高德地图定位模块,支持 Android + iOS,提供尽可能完善的原生接口, 同时提供符合 Web 标准的 Geolocation API。 @@ -18,7 +18,7 @@ await PermissionsAndroid.requestMultiple([ await init({ ios: "9bd6c82e77583020a73ef1af59d0c759", - android: "043b24fe18785f33c491705ffe5b6935" + android: "043b24fe18785f33c491705ffe5b6935", }); Geolocation.getCurrentPosition(({ coords }) => { diff --git a/src/geolocation.ts b/src/geolocation.ts index d5ad300..fd23ea6 100644 --- a/src/geolocation.ts +++ b/src/geolocation.ts @@ -1,5 +1,5 @@ import { EmitterSubscription } from "react-native"; -import { start, stop, addLocationListener, _options } from "./amap-geolocation"; +import { addLocationListener, start, stop, _options } from "./amap-geolocation"; import { Location } from "./types"; /** @@ -81,12 +81,12 @@ export default class Geolocation { */ static getCurrentPosition( success: (position: Position) => void, - error?: (error: PositionError) => void, - options: PositionOptions = {} + error?: (error: PositionError) => void + // options: PositionOptions = {} ) { const listener = addLocationListener((location) => { if (location.errorCode) { - error && error(new PositionError(location.errorCode, location.errorInfo, location)); + error && error(new PositionError(location.errorCode, location.errorInfo ?? "", location)); stop(); return listener.remove(); } @@ -107,12 +107,12 @@ export default class Geolocation { */ static watchPosition( success: (position: Position) => void, - error?: (error: PositionError) => void, - options?: PositionOptions + error?: (error: PositionError) => void + // options?: PositionOptions ) { watchMap[++watchId] = addLocationListener((location) => { if (location.errorCode) { - error && error(new PositionError(location.errorCode, location.errorInfo, location)); + error && error(new PositionError(location.errorCode, location.errorInfo ?? "", location)); } else { success(toPosition(location)); } @@ -140,12 +140,12 @@ function toPosition(location: Location) { coords: { latitude: location.latitude, longitude: location.longitude, - altitude: location.altitude, + altitude: location.altitude ?? 0, accuracy: location.accuracy, - altitudeAccuracy: null, // 高德定位接口没有找到对应的数据 - heading: location.heading, - speed: location.speed, + altitudeAccuracy: 0, // 高德定位接口没有找到对应的数据 + heading: location.heading ?? 0, + speed: location.speed ?? 0, }, - timestamp: location.timestamp, + timestamp: location.timestamp ?? 0, }; } diff --git a/tsconfig.json b/tsconfig.json index 5ff4c54..0e3a361 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,8 +1,12 @@ { "compilerOptions": { - "declaration": true, - "lib": ["es2015"], - "outDir": "lib/js" + "strict": true, + "lib": ["esnext"], + "jsx": "react-native", + "baseUrl": ".", + "paths": { + "react-native-amap-geolocation": ["src"] + } }, - "files": ["src/index.ts"] + "files": ["index.tsx"] }