Skip to content

Commit

Permalink
init Baidu map
Browse files Browse the repository at this point in the history
  • Loading branch information
cczhr committed Mar 26, 2021
1 parent 018ca01 commit fb33872
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
repositories {
flatDir { dirs 'libs' }
}
}

dependencies {
implementation(name:'BaiduLBS_AndroidSDK_Lib', ext:'aar')
implementation 'com.google.android.material:material:1.3.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
Expand Down
Binary file added app/libs/BaiduLBS_AndroidSDK_Lib.aar
Binary file not shown.
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
android:theme="@style/Theme.OTGLocation">
<meta-data android:name="com.amap.api.v2.apikey" android:value="23185808a860d03642cabce72547a117"/>
<service android:name="com.amap.api.location.APSService" />

<meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="06WDfpIkPIgEZyLHkoymQdVhbwnuGnhN"/>
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"/>

<activity android:name="com.amap.api.maps.offlinemap.OfflineMapActivity"
android:screenOrientation="portrait" />
<activity android:name=".MainActivity" android:screenOrientation="portrait" >
Expand All @@ -53,6 +57,7 @@
</intent-filter>
</activity>
<activity android:name=".MapActivity" android:screenOrientation="portrait" />
<activity android:name=".BaiduMapActivity" android:screenOrientation="portrait" />
</application>

</manifest>
68 changes: 68 additions & 0 deletions app/src/main/java/com/cczhr/otglocation/BaiduMapActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.cczhr.otglocation

import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.mapapi.map.BaiduMap
import kotlinx.android.synthetic.main.activity_baidu_map.*

/**
* @author cczhr
* @description
* @since 2021/3/26 11:45
*/
class BaiduMapActivity :BaseActivity() {
override val layoutId: Int = R.layout.activity_baidu_map

lateinit var baiduMap: BaiduMap

lateinit var locationClient:LocationClient
override fun init() {
locationClient=LocationClient(this)
locationClient.locOption= LocationClientOption().apply {
isOpenGps=true
coorType="gcj02"
scanSpan=1000
}
locationClient.registerLocationListener(object :BDAbstractLocationListener(){
override fun onReceiveLocation(location: BDLocation?) {




}

})
baiduMap=map_view.map
baiduMap.isMyLocationEnabled=true


locationClient.start()
}


override fun onResume() {
map_view.onResume()
super.onResume()

}


override fun onPause() {
map_view.onPause()
super.onPause()

}


override fun onDestroy() {
locationClient.stop()
baiduMap.isMyLocationEnabled=false
map_view.onDestroy()
super.onDestroy()

}


}
2 changes: 1 addition & 1 deletion app/src/main/java/com/cczhr/otglocation/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class MainActivity : BaseActivity() {
fun selectLocation(view: View) {
val lat = latitude.text.toString().toDoubleOrNull()
val lon = longitude.text.toString().toDoubleOrNull()
val intent=Intent(this, MapActivity::class.java)
val intent=Intent(this, BaiduMapActivity::class.java)//MapActivity
if (lat != null && lon != null) {
intent.putExtra("lat",lat)
intent.putExtra("lon",lon)
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/cczhr/otglocation/utils/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package com.cczhr.otglocation.utils

import android.app.Application
import android.content.Context
import com.baidu.mapapi.CoordType
import com.baidu.mapapi.SDKInitializer

class Application:Application() {

override fun onCreate() {
super.onCreate()
SDKInitializer.initialize(this)
SDKInitializer.setCoordType(CoordType.GCJ02)
context=this
}
companion object {
Expand Down
128 changes: 128 additions & 0 deletions app/src/main/res/layout/activity_baidu_map.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?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="match_parent"
android:orientation="vertical">


<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:hint="@string/search"
>

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edit_query"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>


<com.baidu.mapapi.map.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:clickable="true"
android:layout_margin="8dp"
android:layout_weight="1" />


<TextView
android:id="@+id/location_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/text_input_layout"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:hint="@string/local_location"
app:errorEnabled="false"
app:helperTextEnabled="true">
<AutoCompleteTextView
android:id="@+id/select_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"/>
</com.google.android.material.textfield.TextInputLayout>

<Button
android:layout_marginEnd="8dp"
android:textColor="@color/white"
android:onClick="deleteLocalLocation"
android:text="@string/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:onClick="moveMap"
android:text="@string/move_map"
android:textColor="@color/white" />


<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:onClick="saveLocation"
android:text="@string/save_location"
android:textColor="@color/white" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:onClick="downloadOfflineMap"
android:text="@string/download_offline_map"
android:textColor="@color/white" />

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:onClick="confirm"
android:text="@string/confirm"
android:textColor="@color/white" />
</LinearLayout>


</LinearLayout>

0 comments on commit fb33872

Please sign in to comment.