Skip to content

Commit

Permalink
新增GPS定位获取,对接登录、注册、修改密码等接口
Browse files Browse the repository at this point in the history
新增GPS定位获取,对接登录、注册、修改密码等接口
  • Loading branch information
zlaazlaa committed Apr 30, 2022
1 parent c824c87 commit 3228af1
Show file tree
Hide file tree
Showing 25 changed files with 1,027 additions and 22 deletions.
461 changes: 461 additions & 0 deletions .idea/dbnavigator.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions .idea/modules/app/app.plantuml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
211 changes: 211 additions & 0 deletions app/src/main/java/com/example/meiyong/GPSGetClass.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package com.example.meiyong

import android.util.Log
import com.google.gson.Gson
import http.GetGPSReturnData.Coordinate
import http.GetGPSReturnData.GetGPSReturn
import http.LoginReturnData.LoginReturn
import okhttp3.Call
import okhttp3.Callback
import okhttp3.FormBody
import okhttp3.Response
import java.io.IOException


class GPSGetClass {
companion object {
lateinit var GPSLoginInformation: LoginReturn
var GPSCoordinate = Coordinate(100.0,100.0)
lateinit var GPSGetReturn: GetGPSReturn
}

fun LoginGPS() {
val LoginFormBody = FormBody.Builder()
.add("lang", "zh_CN")
.add("imei", "65302631125")
.add("password", "123456")
.build()
http.GPSOkHttp.post("/locator-app/imeiLoginVerification", LoginFormBody, object : Callback {
override fun onFailure(call: Call, e: IOException) {
Log.e("OKHTTP_GPS_LOGIN", "Fail")
}

override fun onResponse(call: Call, response: Response) {
val responseData = response.body?.string()
val gson = Gson()
GPSLoginInformation = gson.fromJson(responseData, LoginReturn::class.java)
Log.e("OKHTTP_GPS_LOGIN", "$responseData")
}

})
}

fun UpdateCoordinate() {
val GetGPSFormBody = FormBody.Builder()
.add("imei", "65302631125")
.add("coorType", "bd09ll")//BaiduMap
// .add("coorType", "GCJ02")//GoogleMap
.add("token", GPSLoginInformation.data.token)
.build()
http.GPSOkHttp.post("/locator-app/redis/getGps", GetGPSFormBody, object : Callback {
override fun onFailure(call: Call, e: IOException) {
Log.e("OKHTTP_GPS_GET_GPS", "Fail")
}

override fun onResponse(call: Call, response: Response) {
val responseData = response.body?.string()
Log.e("OKHTTP_GPS_GET_GPS", "$responseData")
val gson = Gson()
GPSGetReturn = gson.fromJson(responseData, GetGPSReturn::class.java)
GPSCoordinate.Latitude = GPSGetReturn.data.latitude
GPSCoordinate.Longitude = GPSGetReturn.data.longitude
}

})
}

fun getCoordiante(): Coordinate {
return GPSCoordinate
}


/**
* 各地图API坐标系统比较与转换;
* WGS84坐标系:即地球坐标系,国际上通用的坐标系。设备一般包含GPS芯片或者北斗芯片获取的经纬度为WGS84地理坐标系,
* 谷歌地图采用的是WGS84地理坐标系(中国范围除外);
* GCJ02坐标系:即火星坐标系,是由中国国家测绘局制订的地理信息系统的坐标系统。由WGS84坐标系经加密后的坐标系。
* 谷歌中国地图和搜搜中国地图采用的是GCJ02地理坐标系; BD09坐标系:即百度坐标系,GCJ02坐标系经加密后的坐标系;
* 搜狗坐标系、图吧坐标系等,估计也是在GCJ02基础上加密而成的。
*
*
* WGS-84 地心坐标系,即GPS原始坐标体系
* GCJ-02火星坐标系,国测局02年发布的坐标体系,它是一种对经纬度数据的加密算法,即加入随机的偏差。高德、腾讯、Google中国地图使用。国内最广泛使用的坐标体系;
* 一般都是由GCJ-02进过偏移算法得到的。这种体系就根据每个公司的不同,坐标体系都不一样了。
* 比如,百度的BD-09坐标、搜狗坐标等
*/
object GPSUtil {
var pi = 3.1415926535897932384626
var x_pi = 3.14159265358979324 * 3000.0 / 180.0
var a = 6378245.0
var ee = 0.00669342162296594323
fun transformLat(x: Double, y: Double): Double {
var ret =
-100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x))
ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0
ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0
ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0
return ret
}

fun transformLon(x: Double, y: Double): Double {
var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + (0.1
* Math.sqrt(Math.abs(x)))
ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0
ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0
ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(
x / 30.0
* pi
)) * 2.0 / 3.0
return ret
}

fun outOfChina(lat: Double, lon: Double): Boolean {
if (lon < 72.004 || lon > 137.8347) return true
return if (lat < 0.8293 || lat > 55.8271) true else false
}

/**
* 84 to 火星坐标系 (GCJ-02) World Geodetic System ==> Mars Geodetic System
*
* @param lat
* @param lon
* @return
*/
fun gps84_To_Gcj02(lat: Double, lon: Double): DoubleArray {
if (outOfChina(lat, lon)) {
return doubleArrayOf(lat, lon)
}
var dLat = transformLat(lon - 105.0, lat - 35.0)
var dLon = transformLon(lon - 105.0, lat - 35.0)
val radLat = lat / 180.0 * pi
var magic = Math.sin(radLat)
magic = 1 - ee * magic * magic
val sqrtMagic = Math.sqrt(magic)
dLat = dLat * 180.0 / (a * (1 - ee) / (magic * sqrtMagic) * pi)
dLon = dLon * 180.0 / (a / sqrtMagic * Math.cos(radLat) * pi)
val mgLat = lat + dLat
val mgLon = lon + dLon
return doubleArrayOf(mgLat, mgLon)
}

/**
* * 火星坐标系 (GCJ-02) to 84 * * @param lon * @param lat * @return
*/
fun gcj02_To_Gps84(lat: Double, lon: Double): DoubleArray {
val gps = gps84_To_Gcj02(lat, lon)
val lontitude = lon * 2 - gps[1]
val latitude = lat * 2 - gps[0]
return doubleArrayOf(latitude, lontitude)
}

/**
* 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 将 GCJ-02 坐标转换成 BD-09 坐标
*
* @param lat
* @param lon
*/
fun gcj02_To_Bd09(lat: Double, lon: Double): DoubleArray {
val z =
Math.sqrt(lon * lon + lat * lat) + 0.00002 * Math.sin(lat * x_pi)
val theta =
Math.atan2(lat, lon) + 0.000003 * Math.cos(lon * x_pi)
val tempLon = z * Math.cos(theta) + 0.0065
val tempLat = z * Math.sin(theta) + 0.006
return doubleArrayOf(tempLat, tempLon)
}

/**
* * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 * * 将 BD-09 坐标转换成GCJ-02 坐标 * * @param
* bd_lat * @param bd_lon * @return
*/
fun bd09_To_Gcj02(lat: Double, lon: Double): DoubleArray {
val x = lon - 0.0065
val y = lat - 0.006
val z =
Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi)
val theta =
Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi)
val tempLon = z * Math.cos(theta)
val tempLat = z * Math.sin(theta)
return doubleArrayOf(tempLat, tempLon)
}

/**将gps84转为bd09
* @param lat
* @param lon
* @return
*/
fun gps84_To_bd09(lat: Double, lon: Double): DoubleArray {
val gcj02 = gps84_To_Gcj02(lat, lon)
return gcj02_To_Bd09(gcj02[0], gcj02[1])
}

fun bd09_To_gps84(lat: Double, lon: Double): DoubleArray {
val gcj02 = bd09_To_Gcj02(lat, lon)
val gps84 = gcj02_To_Gps84(gcj02[0], gcj02[1])
//保留小数点后六位
gps84[0] = retain6(gps84[0])
gps84[1] = retain6(gps84[1])
return gps84
}

/**保留小数点后六位
* @param num
* @return
*/
private fun retain6(num: Double): Double {
val result = String.format("%.6f", num)
return java.lang.Double.valueOf(result)
}
}
}
17 changes: 12 additions & 5 deletions app/src/main/java/com/example/meiyong/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@ package com.example.meiyong

import android.os.Bundle
import android.util.Log
import android.widget.Button
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.meiyong.databinding.ActivityMainBinding
import http.OkHttp
import http.OkHttp.sendOkHttpRequestGET
import com.google.gson.Gson
import http.GetGPSReturnData.Coordinate
import http.GetGPSReturnData.GetGPSReturn
import http.LoginReturnData.LoginReturn
import okhttp3.Call
import okhttp3.Callback
import okhttp3.FormBody
import okhttp3.Response
import java.io.IOException

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding



override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -40,7 +45,10 @@ class MainActivity : AppCompatActivity() {
navView.setupWithNavController(navController)




val GPSSSS = GPSGetClass()
GPSSSS.LoginGPS()

// OkHttp.post("/User/loginIn")
//
//
Expand All @@ -58,7 +66,6 @@ class MainActivity : AppCompatActivity() {
// }



// val url2 = "http://192.168.0.127:80/Order/1"
// sendOkHttpRequestGET(url2, object : okhttp3.Callback {
// override fun onResponse(call: Call, response: Response) {
Expand Down
17 changes: 16 additions & 1 deletion app/src/main/java/com/example/meiyong/MapsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,23 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
mMap = googleMap

// Add a marker in Sydney and move the camera
val sydney = LatLng(32.1179807, 118.9269471)

val getGPS = GPSGetClass()
// getGPS.LoginGPS()
getGPS.UpdateCoordinate()
val GPS_84 = GPSGetClass.GPSCoordinate
val GPSCoordiante = GPSGetClass.GPSUtil.gps84_To_Gcj02(GPS_84.Latitude, GPS_84.Longitude)
val sydney = LatLng(GPSCoordiante[0], GPSCoordiante[1])
mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))


// val sydney = LatLng(39.54, 116.23)
// googleMap.addMarker(
// MarkerOptions()
// .position(sydney)
// .title("Marker in Sydney")
// )
// googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
}
}
16 changes: 16 additions & 0 deletions app/src/main/java/com/example/meiyong/MyApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.meiyong

import android.app.Application
import android.content.Context

class MyApplication : Application() {
companion object {
lateinit var context: Context
}

override fun onCreate() {
super.onCreate()
context = applicationContext
}

}
Loading

0 comments on commit 3228af1

Please sign in to comment.