| | |
| | | import android.util.Log |
| | | import android.view.View |
| | | import androidx.core.content.ContextCompat |
| | | import cn.sinata.rxnetty.LogHelper |
| | | import cn.sinata.xldutils.utils.SPUtils |
| | | import com.driver.igotrip.R |
| | | import com.driver.igotrip.base.Const |
| | |
| | | import com.google.android.gms.maps.model.Polyline |
| | | import com.google.android.gms.maps.model.PolylineOptions |
| | | import com.google.gson.Gson |
| | | import com.tbruyelle.rxpermissions2.RxPermissions |
| | | import io.reactivex.Observable |
| | | import io.reactivex.android.schedulers.AndroidSchedulers |
| | | import io.reactivex.schedulers.Schedulers |
| | | import okhttp3.MediaType.Companion.toMediaTypeOrNull |
| | | import okhttp3.OkHttpClient |
| | | import okhttp3.Request |
| | |
| | | |
| | | |
| | | fun initMap(mapView: SupportMapFragment, function: (GoogleMap) -> Unit) { |
| | | mapView.getMapAsync { p0 -> |
| | | val mMyLocationStyle = GoogleMapOptions() |
| | | p0.uiSettings.isMyLocationButtonEnabled = false |
| | | p0.setMinZoomPreference(6.0f); |
| | | p0.setMaxZoomPreference(16.0f); |
| | | // mMyLocationStyle.interval(5000) |
| | | // mMyLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER) |
| | | // mMyLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.mipmap.gps_point)) |
| | | // p0.setLocationSource() = mMyLocationStyle |
| | | // p0.isMyLocationEnabled = false |
| | | // p0.animateCamera(CameraUpdateFactory.zoomTo(aMap.maxZoomLevel - 3)) |
| | | // 设置定位的类型为定位模式,有定位、跟随或地图根据面向方向旋转几种 |
| | | // p0.setMyLocationType(GoogleMap.LOCATION_TYPE_LOCATE) |
| | | // val uiSettings: UiSettings = aMap.uiSettings |
| | | // uiSettings.isZoomControlsEnabled = false |
| | | function(p0) |
| | | } |
| | | |
| | | val subscribe = |
| | | RxPermissions(mapView.requireActivity()).request(Manifest.permission.ACCESS_FINE_LOCATION) |
| | | .subscribe { |
| | | if (it) { |
| | | mapView.getMapAsync { p0 -> |
| | | val mMyLocationStyle = GoogleMapOptions() |
| | | p0.uiSettings.isMyLocationButtonEnabled = false |
| | | p0.setMinZoomPreference(6.0f); |
| | | p0.setMaxZoomPreference(16.0f); |
| | | // mMyLocationStyle.interval(5000) |
| | | // mMyLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER) |
| | | // mMyLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.mipmap.gps_point)) |
| | | // p0.setLocationSource() = mMyLocationStyle |
| | | // p0.isMyLocationEnabled = false |
| | | // p0.animateCamera(CameraUpdateFactory.zoomTo(aMap.maxZoomLevel - 3)) |
| | | // 设置定位的类型为定位模式,有定位、跟随或地图根据面向方向旋转几种 |
| | | // p0.setMyLocationType(GoogleMap.LOCATION_TYPE_LOCATE) |
| | | // val uiSettings: UiSettings = aMap.uiSettings |
| | | // uiSettings.isZoomControlsEnabled = false |
| | | function(p0) |
| | | } |
| | | } else { |
| | | Log.e("map", "没有权限") |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | onClick: (latLngs: MutableList<LatLng>, lineTance: Float, lineTime: Long) -> Unit |
| | | ) { |
| | | //https://maps.googleapis.com/maps/api/directions/json?origin=39.99709957757345,116.31184045225382&destination=39.949158391497214,116.4154639095068&sensor=false&mode=driving |
| | | var language = SPUtils.getString("language") |
| | | if (language.isEmpty()) { |
| | | language = "en" |
| | | } |
| | | var language = SPUtils.getString(Const.language) |
| | | var centerStr = getPointStr(centerPoint ?: arrayListOf()) |
| | | var url = "https://maps.googleapis.com/maps/api/directions/json" + |
| | | "?origin=" + start.latitude.toString() + "," + start.longitude.toString() + |
| | |
| | | return false |
| | | } |
| | | |
| | | fun getClient(url: String, function: (String) -> Unit) { |
| | | fun getClient(url: String, retryCount: Int = 3, function: (String) -> Unit) { |
| | | val client = OkHttpClient().newBuilder().build() |
| | | val mediaType = "text/plain".toMediaTypeOrNull() |
| | | val body = RequestBody.create(mediaType, "") |
| | | val request: Request = Request.Builder() |
| | | .url(url) |
| | | .method("GET", null) |
| | | .build() |
| | | Thread { |
| | | try { |
| | | val response = client.newCall(request).execute() |
| | | Handler(Looper.getMainLooper()).post { |
| | | function(response.body?.string() ?: "") |
| | | LogHelper.logRecord("请求谷歌服务器 $url") |
| | | val subscribe = Observable.just(request) |
| | | .map { request -> client.newCall(request).execute().body?.string() ?: "" } |
| | | .retry(retryCount.toLong()) // 最多额外重试 3 次(加上首次共 4 次尝试) |
| | | .subscribeOn(Schedulers.io()) |
| | | .observeOn(AndroidSchedulers.mainThread()) |
| | | .subscribe({ |
| | | try { |
| | | if (it.isEmpty()) { |
| | | LogHelper.logRecord("请求谷歌服务器成功 获取结果异常为空") |
| | | return@subscribe |
| | | } |
| | | function(it) |
| | | LogHelper.logRecord("请求谷歌服务器成功") |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | LogHelper.logRecord("请求谷歌服务器成功,获取结果异常报错 message:${e.message},stackTrace:${e.stackTraceToString()}") |
| | | } |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | } |
| | | }.start() |
| | | |
| | | }, { throwable -> |
| | | throwable.printStackTrace() |
| | | LogHelper.logRecord("请求谷歌服务器异常报错 message:${throwable.message},stackTrace:${throwable.stackTraceToString()}") |
| | | }) |
| | | } |
| | | |
| | | /*** |
| | |
| | | lng: Double, |
| | | function: (List<GoogleNearBean2.ResultsBean>) -> Unit |
| | | ) { |
| | | var language = SPUtils.getString("language") |
| | | if (language.isEmpty()) { |
| | | language = "en" |
| | | } |
| | | var language = SPUtils.getString(Const.language) |
| | | var url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json" + |
| | | "?location=" + lat.toString() + "," + lng.toString() + |
| | | "&radius=1500" + |
| | |
| | | // */ |
| | | // fun searchSite(context: Context, content: String?, cityName: String, function: (List<AutocompletePrediction>) -> Unit) { |
| | | // val placesClient = Places.createClient(context) |
| | | // var language = SPUtils.getString("language") |
| | | // var language = SPUtils.getString(Const.language) |
| | | // if (language.isEmpty()){ |
| | | // language = "en" |
| | | // language = Const.Language.ENGLISH |
| | | // } |
| | | // val token = AutocompleteSessionToken.newInstance() |
| | | //// val bounds = RectangularBounds.newInstance( |
| | |
| | | * 逆地址编码 |
| | | */ |
| | | fun getAddress(context: Context, lat: Double, lon: Double, function: (String) -> Unit) { |
| | | var language = SPUtils.getString("language") |
| | | if (language.isEmpty()) { |
| | | language = "en" |
| | | } |
| | | var language = SPUtils.getString(Const.language) |
| | | var url = "https://maps.googleapis.com/maps/api/geocode/json" + |
| | | "?latlng=" + lat.toString() + "," + lon.toString() + |
| | | "&language=" + language + |
| | |
| | | } |
| | | |
| | | fun getAddressName(context: Context, lat: Double, lon: Double, function: (String) -> Unit) { |
| | | var language = SPUtils.getString("language") |
| | | if (language.isEmpty()) { |
| | | language = "en" |
| | | } |
| | | var language = SPUtils.getString(Const.language) |
| | | var url = "https://maps.googleapis.com/maps/api/geocode/json" + |
| | | "?latlng=" + lat.toString() + "," + lon.toString() + |
| | | "&language=" + language + |