Commit f3f02b96 authored by wanglei's avatar wanglei

init

parent 9d6c8b7e
package com.base.locationsharewhite.map
import android.annotation.SuppressLint
import android.content.Context
import android.location.Criteria
import android.location.Location
import android.location.LocationManager
import android.view.View
import com.base.locationsharewhite.utils.BitmapUtils.getBitmapDescriptorFromLayoutResId
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.LatLngBounds
import com.google.android.gms.maps.model.MarkerOptions
import kotlin.math.abs
object MapUtils {
@SuppressLint("MissingPermission")
fun Context.getLastKnowLatLng(): LatLng {
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager?
if (locationManager != null) {
val provider: String? = locationManager.getBestProvider(Criteria(), true)
if (provider != null) {
val lastKnownLocation: Location? = locationManager.getLastKnownLocation(provider)
val latitude: Double = lastKnownLocation?.latitude ?: 0.0//维度
val longitude: Double = lastKnownLocation?.longitude ?: 0.0//经度
return LatLng(latitude, longitude)
}
}
return LatLng(0.0, 0.0)
}
/**
* 添加view为Marker
*/
fun GoogleMap.addLocationMarker(
marker: View,
pair: Pair<Double, Double>,
latLng: LatLng,
tittle: String = "",
) {
val latLng = LatLng(pair.first, pair.second)
val bitmapDescriptor = getBitmapDescriptorFromLayoutResId(marker)
addMarker(
MarkerOptions()
......@@ -26,11 +53,11 @@ object MapUtils {
}
fun GoogleMap.moveCameraZoom(
pair: Pair<Double, Double>,
zoom: Float = 15f
) {
val latLng = LatLng(pair.first, pair.second)
moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom))
fun GoogleMap.cameraMovePointsBounds(point1: LatLng, point2: LatLng, padding: Int) {
// 创建包含两个点的边界
val bounds = LatLngBounds.Builder().include(point1).include(point2).build()
moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding))
}
}
\ No newline at end of file
package com.base.locationsharewhite.ui.locationmap
import android.graphics.Color
import android.view.LayoutInflater
import androidx.activity.addCallback
import com.base.locationsharewhite.R
import com.base.locationsharewhite.databinding.ActivityLocationMapBinding
import com.base.locationsharewhite.helper.BaseActivity
import com.base.locationsharewhite.map.MapUtils.addLocationMarker
import com.base.locationsharewhite.map.MapUtils.cameraMovePointsBounds
import com.base.locationsharewhite.map.MapUtils.getLastKnowLatLng
import com.base.locationsharewhite.utils.BarUtils
import com.base.locationsharewhite.utils.LogEx
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>() {
class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapReadyCallback {
private val TAG = "LocationMapActivity"
private var map: GoogleMap? = null
override val binding: ActivityLocationMapBinding by lazy {
ActivityLocationMapBinding.inflate(layoutInflater)
......@@ -18,6 +31,10 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>() {
BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
// binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
val mapFragment =
supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.getMapAsync(this)
}
override fun initListener() {
......@@ -30,4 +47,31 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>() {
}
}
override fun onMapReady(googleMap: GoogleMap) {
map = googleMap
val sydney = LatLng(-33.87365, 151.20689)
showMeAndFriendLocation(sydney)
val chunxilu = LatLng(30.655782251943407, 104.07701072527895)
// showMeAndFriendLocation(chunxilu)
}
private fun showMeAndFriendLocation(friendLatLng: LatLng) {
val myPair: LatLng = getLastKnowLatLng()
LogEx.logDebug(TAG, "getLastKnowLocation ${myPair.latitude} ${myPair.longitude}")
val myAvatar = LayoutInflater.from(this).inflate(R.layout.avatar_me, null)
val friendAvatar = LayoutInflater.from(this).inflate(R.layout.avatar_friend, null)
map?.addLocationMarker(myAvatar, myPair, "my")
map?.addLocationMarker(friendAvatar, friendLatLng, "friend")
val padding = resources.getDimensionPixelOffset(R.dimen.dp_150)
map?.cameraMovePointsBounds(myPair, friendLatLng, padding)
}
}
\ No newline at end of file
......@@ -11,21 +11,17 @@ import com.base.locationsharewhite.databinding.ActivityMainBinding
import com.base.locationsharewhite.helper.BaseActivity
import com.base.locationsharewhite.helper.MyApplication
import com.base.locationsharewhite.map.MapUtils.addLocationMarker
import com.base.locationsharewhite.map.MapUtils.moveCameraZoom
import com.base.locationsharewhite.map.MapUtils.getLastKnowLatLng
import com.base.locationsharewhite.ui.howuse.HowUseActivity
import com.base.locationsharewhite.ui.locationmap.LocationMapActivity
import com.base.locationsharewhite.ui.set.SettingActivity
import com.base.locationsharewhite.utils.BarUtils
import com.base.locationsharewhite.utils.LocationUtils.getLastKnowLocation
import com.base.locationsharewhite.utils.LogEx
import com.base.locationsharewhite.utils.PermissionUtils.checkLocationPermission
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
class MainActivity : BaseActivity<ActivityMainBinding>(), OnMapReadyCallback {
......@@ -103,12 +99,12 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnMapReadyCallback {
override fun onMapReady(googleMap: GoogleMap) {
map = googleMap
val pair = getLastKnowLocation()
LogEx.logDebug(TAG, "getLastKnowLocation ${pair.first} ${pair.second}")
val latLng = getLastKnowLatLng()
LogEx.logDebug(TAG, "getLastKnowLocation ${latLng.latitude} ${latLng.longitude}")
val myAvatar = LayoutInflater.from(this).inflate(R.layout.avatar_me, null)
map?.moveCameraZoom(pair)
map?.addLocationMarker(myAvatar, pair, "my")
map?.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15f))
map?.addLocationMarker(myAvatar, latLng, "my")
}
}
\ No newline at end of file
package com.base.locationsharewhite.utils
import android.annotation.SuppressLint
import android.content.Context
import android.location.Criteria
import android.location.Location
import android.location.LocationManager
import androidx.core.content.ContextCompat
object LocationUtils {
@SuppressLint("MissingPermission")
fun Context.getLastKnowLocation(): Pair<Double, Double> {
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager?
if (locationManager != null) {
val provider: String? = locationManager.getBestProvider(Criteria(), true)
if (provider != null) {
val lastKnownLocation: Location? = locationManager.getLastKnownLocation(provider)
val latitude: Double = lastKnownLocation?.latitude ?: 0.0//维度
val longitude: Double = lastKnownLocation?.longitude ?: 0.0//经度
return Pair(latitude, longitude)
}
}
return Pair(0.0, 0.0)
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@mipmap/dingweibg"
tools:ignore="ContentDescription" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
android:src="@mipmap/friend"
tools:ignore="ContentDescription" />
</FrameLayout>
\ No newline at end of file
......@@ -5,4 +5,8 @@
<dimen name="dp_5">5dp</dimen>
<dimen name="dp_10">10dp</dimen>
<dimen name="dp_320">320dp</dimen>
<dimen name="dp_30">30dp</dimen>
<dimen name="dp_100">100dp</dimen>
<dimen name="dp_200">200dp</dimen>
<dimen name="dp_150">150dp</dimen>
</resources>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment