调整mapapi

This commit is contained in:
squallzhjch 2023-03-24 15:34:22 +08:00
parent 5e8b75005f
commit 97a48237ba
12 changed files with 212 additions and 14 deletions

View File

@ -23,10 +23,23 @@ class MainActivity : BaseActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main) binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
//关联生命周期
binding.lifecycleOwner = this binding.lifecycleOwner = this
viewModel.initMap(this, binding.mainActivityMap) //给xml转递对象
binding.mainActivity = this
//给xml传递viewModel对象
binding.viewModel = viewModel
//初始化地图
viewModel.initMap(this, binding.mapView.mainActivityMap)
//让viewModel监听activity生命周期
lifecycle.addObserver(viewModel) lifecycle.addObserver(viewModel)
// val navController = findNavController(R.id.nav_host_fragment_content_main) }
/**
* 打开个人中菜单
*/
fun openMenu() {
binding.mainActivityDrawer.open()
} }
} }

View File

@ -8,8 +8,14 @@ import com.navinfo.collect.library.map.NIMapController
import com.navinfo.collect.library.map.NIMapView import com.navinfo.collect.library.map.NIMapView
class MainViewModel : ViewModel(), DefaultLifecycleObserver { class MainViewModel : ViewModel(), DefaultLifecycleObserver {
/**
* 地图控制器
*/
private lateinit var mapController: NIMapController private lateinit var mapController: NIMapController
/**
* 初始化地图
*/
fun initMap(context: Context, mapView: NIMapView) { fun initMap(context: Context, mapView: NIMapView) {
mapController = NIMapController(context = context, mapView = mapView) mapController = NIMapController(context = context, mapView = mapView)
@ -17,6 +23,7 @@ class MainViewModel : ViewModel(), DefaultLifecycleObserver {
override fun onStart(owner: LifecycleOwner) { override fun onStart(owner: LifecycleOwner) {
super.onStart(owner) super.onStart(owner)
//开启定位
mapController.locationLayerHandler.startLocation() mapController.locationLayerHandler.startLocation()
} }
@ -26,6 +33,7 @@ class MainViewModel : ViewModel(), DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) { override fun onDestroy(owner: LifecycleOwner) {
mapController.mMapView.onDestroy() mapController.mMapView.onDestroy()
//结束定位
mapController.locationLayerHandler.stopLocation() mapController.locationLayerHandler.stopLocation()
} }
@ -33,4 +41,11 @@ class MainViewModel : ViewModel(), DefaultLifecycleObserver {
mapController.mMapView.onResume() mapController.mMapView.onResume()
} }
/**
* 点击我的位置回到我的位置
*/
fun onClickLocationButton() {
mapController.locationLayerHandler.animateToCurrentPosition()
}
} }

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#18FD00"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,8c-2.21,0 -4,1.79 -4,4s1.79,4 4,4 4,-1.79 4,-4 -1.79,-4 -4,-4zM20.94,11c-0.46,-4.17 -3.77,-7.48 -7.94,-7.94L13,1h-2v2.06C6.83,3.52 3.52,6.83 3.06,11L1,11v2h2.06c0.46,4.17 3.77,7.48 7.94,7.94L11,23h2v-2.06c4.17,-0.46 7.48,-3.77 7.94,-7.94L23,13v-2h-2.06zM12,19c-3.87,0 -7,-3.13 -7,-7s3.13,-7 7,-7 7,3.13 7,7 -3.13,7 -7,7z"/>
</vector>

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#18FD00"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,12c2.21,0 4,-1.79 4,-4s-1.79,-4 -4,-4 -4,1.79 -4,4 1.79,4 4,4zM12,14c-2.67,0 -8,1.34 -8,4v2h16v-2c0,-2.66 -5.33,-4 -8,-4z"/>
</vector>

View File

@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android" <layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools"
tools:context=".ui.activity.LoginActivity">
<data> <data>
@ -17,7 +18,7 @@
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".ui.activity.LoginActivity"> >
<ImageView <ImageView
android:id="@+id/login_fragment_logo" android:id="@+id/login_fragment_logo"

View File

@ -1,22 +1,41 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android" <layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools"
tools:context=".ui.activity.MainActivity">
<data> <data>
<variable
name="mainActivity"
type="com.navinfo.omqs.ui.activity.MainActivity" />
<variable
name="viewModel"
type="com.navinfo.omqs.ui.activity.MainViewModel" />
</data> </data>
<androidx.coordinatorlayout.widget.CoordinatorLayout <androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/main_activity_drawer"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" android:fitsSystemWindows="true">
tools:context=".ui.activity.MainActivity">
<com.navinfo.collect.library.map.NIMapView <include
android:id="@+id/main_activity_map" android:id="@+id/map_view"
layout="@layout/map_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent"
app:mainActivity="@{mainActivity}"
app:viewModel="@{viewModel}"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout> <com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
</layout> </layout>

View File

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="mainActivity"
type="com.navinfo.omqs.ui.activity.MainActivity" />
<variable
name="viewModel"
type="com.navinfo.omqs.ui.activity.MainViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.activity.MainActivity">
<com.navinfo.collect.library.map.NIMapView
android:id="@+id/main_activity_map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageButton
android:id="@+id/main_activity_person_center"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:onClick="@{()->mainActivity.openMenu()}"
android:src="@drawable/baseline_person_24"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/main_activity_location"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="80dp"
android:onClick="@{()->viewModel.onClickLocationButton()}"
android:src="@drawable/baseline_my_location_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@color/default_blue"
android:gravity="bottom"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/nav_header_desc"
android:paddingTop="@dimen/nav_header_vertical_spacing"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@string/nav_header_title"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nav_header_subtitle" />
</LinearLayout>

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/icon_map_zoom_in"
android:title="menu_home" />
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/icon_map_zoom_in"
android:title="menu_gallery" />
<item
android:id="@+id/nav_slideshow"
android:icon="@drawable/icon_map_zoom_in"
android:title="menu_slideshow" />
</group>
</menu>

View File

@ -1,3 +1,8 @@
<resources> <resources>
<dimen name="fab_margin">16dp</dimen> <dimen name="fab_margin">16dp</dimen>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="nav_header_vertical_spacing">8dp</dimen>
<dimen name="nav_header_height">176dp</dimen>
</resources> </resources>

View File

@ -48,4 +48,13 @@
<string name="login">登录</string> <string name="login">登录</string>
<string name="logon">注册</string> <string name="logon">注册</string>
<string name="imagedescription">imageDescription</string> <string name="imagedescription">imageDescription</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>
<string name="nav_header_title">Android Studio</string>
<string name="nav_header_subtitle">android.studio@android.com</string>
<string name="nav_header_desc">Navigation header</string>
<string name="menu_home">Home</string>
<string name="menu_gallery">Gallery</string>
<string name="menu_slideshow">Slideshow</string>
</resources> </resources>

View File

@ -16,12 +16,14 @@ class LocationLayerHandler(context: Context, mapView: NIMapView) :
BaseHandler(context, mapView) { BaseHandler(context, mapView) {
private var mCurrentLocation: BDLocation? = null private var mCurrentLocation: BDLocation? = null
private var bFirst = true
private val mLocationLayer: LocationLayer = LocationLayer(mMapView.vtmMap) private val mLocationLayer: LocationLayer = LocationLayer(mMapView.vtmMap)
private lateinit var locationClient: LocationClient private lateinit var locationClient: LocationClient
init { init {
///添加定位图层到地图,[NIMapView.LAYER_GROUPS.NAVIGATION] 是最上层layer组
addLayer(mLocationLayer, NIMapView.LAYER_GROUPS.NAVIGATION) addLayer(mLocationLayer, NIMapView.LAYER_GROUPS.NAVIGATION)
//初始化定位
initLocationOption() initLocationOption()
} }
@ -57,6 +59,10 @@ class LocationLayerHandler(context: Context, mapView: NIMapView) :
it.longitude, it.longitude,
it.radius it.radius
) )
//第一次定位成功显示当前位置
if (this.bFirst) {
animateToCurrentPosition()
}
} }
//注册监听函数 //注册监听函数
@ -102,7 +108,9 @@ class LocationLayerHandler(context: Context, mapView: NIMapView) :
} }
} }
/**
* 开启定位
*/
fun startLocation() { fun startLocation() {
//开始定位 //开始定位
if (!locationClient.isStarted) { if (!locationClient.isStarted) {
@ -110,11 +118,26 @@ class LocationLayerHandler(context: Context, mapView: NIMapView) :
} }
} }
/**
* 停止定位
*/
fun stopLocation() { fun stopLocation() {
if (locationClient.isStarted) { if (locationClient.isStarted) {
locationClient.stop() locationClient.stop()
} }
} }
/**
* 回到当前位置
*/
fun animateToCurrentPosition()
{
mCurrentLocation?.run {
val mapPosition = mMapView.vtmMap.mapPosition;
mapPosition.setPosition(this.latitude, this.longitude);
mMapView.vtmMap.animator().animateTo(300, mapPosition);
}
}
} }
/** /**