-
2022-04-13 15:30:09
文章目录
一、地图弹窗
<el-dialog title="选择地图" :visible.sync="dialogFormVisibleMap" width="60%" :close-on-click-modal="false"> <iframe id="mapPage" width="100%" height="100%" frameborder=0 src="https://apis.map.qq.com/tools/locpicker?search=1&type=1&key=yourkey&referer=myapp"> </iframe> <div slot="footer" class="dialog-footer"> <el-button @click="dialogFormVisibleMap = false">关 闭</el-button> </div> </el-dialog>
二、监听地图搜索事件
window:addEventListener('message', function(event) { // 接收位置信息,用户选择确认位置点后选点组件会触发该事件,回传用户的位置信息 var loc = event.data; if (loc && loc.module == 'locationPicker') {//防止其他应用也会向该页面post信息,需判断module是否为'locationPicker' console.log('location', loc); mainVm.entity.longitude = loc.latlng.lng mainVm.entity.latitude = loc.latlng.lat mainVm.dialogFormVisibleMap=false; } }, false),
更多相关内容 -
vue+地图选点组件(百度地图)
2021-08-10 15:09:24本组件是用于在地图上选点、回显坐标等 -
微信小程序 地图选点 获取用户选择的定位信息 wx.chooseLocation
2021-01-08 11:19:47总之,一句话: 获取用户在微信小程序中,提交的地图选点的相关信息。 就比如下面的例子:在某个提交房屋登记的表单中,需要提交房屋的定位信息。我们不能通过获取用户的定位来实现,因为用户不一定在自己的房屋内... -
仿微信发送位置、地图选点(高德地图)
2017-12-29 11:46:07Android版仿微信发送位置、地图选点(高德地图). 前面上传过一个百度地图的, 这个也一起上传吧(捞点分, 现在下载都强制需要资源分), Key请自行申请, demo包含一个可运行apk和源码. -
百度地图选点定位demo
2017-05-27 09:55:10百度地图选点定位 -
地图选点google.zip
2021-07-19 17:57:20地图选点google.zip -
百度地图选点定位界面
2021-12-31 16:43:541.去申请app的Appkey,对百度sdk的引入和配置 2.xml 3Java代码 /** * 地图选点定位 */ open class MapNavigation : BaseActivity(), BaiduMap.OnMapClickListener, OnGetGeoCoderResultListener { private lateinit ...1.去申请app的Appkey,对百度sdk的引入和配置
2.xml
<?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="listener" type="android.view.View.OnClickListener" /> </data> <LinearLayout tools:context=".activity.MapNavigation" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:background="@drawable/home_top_gradient_bg" android:layout_height="48dp"> <TextView android:layout_width="match_parent" android:layout_height="@dimen/dp_48" android:gravity="center" android:text="选点定位" android:textColor="@color/white" android:textSize="18sp" /> <TextView android:id="@+id/tv_sure" android:layout_width="wrap_content" android:layout_height="@dimen/dp_48" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:gravity="center" android:onClick="@{listener}" android:text="确定选点" android:textColor="@color/white" android:textSize="12sp" /> </RelativeLayout> <com.baidu.mapapi.map.TextureMapView android:id="@+id/bmapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/line" /> <LinearLayout android:id="@+id/ll_location" android:visibility="gone" android:layout_width="match_parent" android:layout_height="@dimen/dp_60" android:background="@color/white" android:layout_gravity="bottom" android:orientation="vertical"> <com.hnwx.rhpk.view.MyValueTextView android:id="@+id/tv_address" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:maxLines="1" android:ellipsize="end" android:paddingLeft="@dimen/dp_10" android:gravity="left|center_vertical" android:text="" android:textColor="#000" android:textSize="12sp" /> <com.hnwx.rhpk.view.MyValueTextView android:id="@+id/tv_latlng" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="left|center_vertical" android:maxLines="1" android:ellipsize="end" android:text="位置:" android:paddingLeft="@dimen/dp_10" android:textColor="#000" android:textSize="12sp" /> </LinearLayout> </LinearLayout> </layout>
3Java代码
/** * 地图选点定位 */ open class MapNavigation : BaseActivity<ActivityMapNavigationBinding>(), BaiduMap.OnMapClickListener, OnGetGeoCoderResultListener { private lateinit var mBaiduMap: BaiduMap private var markList: ArrayList<MarkerOptions> = ArrayList() private var mClusterManager: ClusterManager<HomeFragmentMap.MyItem>? = null private var zoomTo = 13.0f private var mMapStatus: MapStatus? = null private lateinit var mSearch : GeoCoder private var lat = 28.234893 private var lng = 112.94547 private var viewId = 0 private var position = -1 companion object { @JvmStatic fun goMapNavigationActivity(context: Context, lon: Double, lat: Double,viewId: Int,position: Int) { val intent = Intent(context, MapNavigation().javaClass) intent.putExtra("lon", lon) intent.putExtra("lat", lat) intent.putExtra("viewId", viewId) intent.putExtra("position", position) ActivityUtils.startActivity(intent) } @JvmStatic fun goMapNavigationActivity(context: Context) { ActivityUtils.startActivity(Intent(context, MapNavigation().javaClass)) } } override fun init() { mBaiduMap = getBinding().bmapView.map //设置不让手势旋转地图 mBaiduMap.uiSettings.isRotateGesturesEnabled = false; mBaiduMap.uiSettings.isOverlookingGesturesEnabled = false; // 设置地图监听,当地图状态发生改变时,进行点聚合运算 mBaiduMap.setOnMapStatusChangeListener(mClusterManager) // 设置maker点击时的响应 mBaiduMap.setOnMarkerClickListener(mClusterManager) mSearch = GeoCoder.newInstance() mSearch.setOnGetGeoCodeResultListener(this) intent.getStringExtra("lat") lat = intent.getDoubleExtra("lat",28.234893) lng = intent.getDoubleExtra("lon",112.94547) viewId = intent.getIntExtra("viewId",0) position = intent.getIntExtra("position",-1) setMarks(lat,lng) getBinding().setListener { when (it.id) { R.id.tv_sure -> { initLocation() if(getBinding().tvLatlng.text.toString().isNotEmpty()){ EventBusUtil.post(MapSelectLocationEvent( getBinding().tvLatlng.text.toString(),getBinding().tvAddress.text.toString(), viewId,position)) } finish() } } } mBaiduMap.setOnMapClickListener(this) mBaiduMap.isMyLocationEnabled = true // 初始化定位 //地图加载完成回调,(只在初始化调一次) mBaiduMap.setOnMapLoadedCallback { mBaiduMap.setMaxAndMinZoomLevel(18.toFloat(), 11.toFloat()) // 初始化默认定位点 mMapStatus = MapStatus.Builder().target(LatLng(lat, lng)).zoom(zoomTo).build() mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(mMapStatus)) getBinding().bmapView.setMapCustomStyleEnable(true) } // 隐藏百度logo val child: View = getBinding().bmapView.getChildAt(1) if (child != null && (child is ImageView || child is ZoomControls)) { child.visibility = View.GONE } initListener() } /** * 设置mark点 */ private fun setMarks(lat: Double, lon: Double) { mBaiduMap.clear() markList.clear() val markView = LayoutInflater.from(this).inflate(R.layout.layout_baidu_mark, null) // val markIndex = markView.findViewById<TextView>(R.id.iv_mark) // val split = it.color.replace("rgb(", "").replace(")", "").split(",") val bundle = Bundle() val dataInfo = MapMarkBean.Data() dataInfo.site = "$lon,$lat" bundle.putSerializable("datainfo", dataInfo) val markerOptionsA = MarkerOptions() .extraInfo(bundle) // 经纬度 .position(LatLng(lat, lon)) // 设置 Marker 覆盖物的图标 .icon(BitmapDescriptorFactory.fromView(markView)) // .icon(BitmapDescriptorFactory.fromResource(R.drawable.dwd)) // 设置 marker 在地图的层级 .zIndex(9) // 设置Marker是否可点击 .clickable(false) // 设置 marker 是否允许拖拽,默认不可拖拽 .draggable(false) // 设置是否开启 marker 覆盖物近大远小效果,默认开启 .perspective(false) // 设置 marker 覆盖物的锚点比例,默认(0.5f, 1.0f)水平居中,垂直下对齐 .anchor(0.5f, 1.0f) // 设置多少帧刷新一次图片资源,Marker动画的间隔时间,值越小动画越快 默认为20,最小为1 .period(10) // // 掉下动画 // markerOptionsA.animateType(MarkerOptions.MarkerAnimateType.drop) // 生长动画 // markerOptionsC.animateType(MarkerOptions.MarkerAnimateType.grow) markList.add(markerOptionsA) mBaiduMap.addOverlay(markerOptionsA) } /** * 给marker点添加点击事件 */ private fun initListener() { // 设置Marker 点击事件监听 mBaiduMap.setOnMarkerClickListener { marker -> val dataInfo = marker.extraInfo.getSerializable("datainfo") as MapMarkBean.Data dataInfo.let { val list = ArrayList<MapMarkBean.Data>() list.add(dataInfo) // showMarkInfo(list) } true } // 设置地图监听,当地图状态发生改变时,进行点聚合运算 mBaiduMap.setOnMapStatusChangeListener(object : BaiduMap.OnMapStatusChangeListener { /** * status - 地图状态改变开始时的地图状态 */ override fun onMapStatusChangeStart(p0: MapStatus?) { // 手势操作地图,设置地图状态等操作导致地图状态开始改变。 } /** * status - 地图状态改变开始时的地图状态 * reason - 地图状态改变的原因 */ override fun onMapStatusChangeStart(p0: MapStatus?, p1: Int) { // 手势操作地图,设置地图状态等操作导致地图状态开始改变。 } /** * status - 当前地图状态 */ override fun onMapStatusChange(p0: MapStatus?) { // 地图状态变化中 } /** * status - 地图状态改变结束后的地图状态 */ override fun onMapStatusChangeFinish(p0: MapStatus?) { // 地图状态改变结束 // p0.zoom 地图缩放级别 4~21,室内图支持到22 LogUtil.d("zoom->", p0?.zoom.toString()) } }) } override fun onResume() { super.onResume() // MapView的生命周期与Activity同步,当activity onResume 时必须调用MapView.onResume() getBinding().bmapView.onResume() } override fun onPause() { super.onPause() // MapView的生命周期与Activity同步,当activity销毁时必须调用MapView.onPause() getBinding().bmapView.onPause() } override fun onDestroy() { super.onDestroy() //不允许图层定位 mBaiduMap.isMyLocationEnabled = false // 清除所有图层 mBaiduMap.clear() mSearch.destroy() getBinding().bmapView.setMapCustomStyleEnable(false) // MapView的生命周期与Activity同步,当activity销毁时必须调用MapView.destroy() getBinding().bmapView.onDestroy() } override fun isUserEventBus(): Boolean { return false } override fun onMapClick(p0: LatLng?) { if (p0 != null) { LogUtils.d("onMapClick"+p0.latitude+"lon"+p0.longitude) setMarks(p0.latitude, p0.longitude) mSearch.reverseGeoCode( ReverseGeoCodeOption() .location(p0) ) } } override fun onMapPoiClick(p0: MapPoi?) { if (p0 != null) { LogUtils.d("onMapClick"+p0.position.latitude+"lon"+p0.position.longitude) setMarks(p0.position.latitude, p0.position.longitude) mSearch.reverseGeoCode( ReverseGeoCodeOption() .location(p0.position) ) } } //正向编码,通过地址获得经纬度 override fun onGetGeoCodeResult(result: GeoCodeResult?) { } //反向编码,通过经纬度获得地址 override fun onGetReverseGeoCodeResult(result: ReverseGeoCodeResult?) { if (result == null || result.error !== SearchResult.ERRORNO.NO_ERROR) { Toast.makeText(this, "抱歉,未能找到结果", Toast.LENGTH_LONG) .show() getBinding().tvLatlng.text = "位置:" getBinding().tvAddress.text = "未获取到位置" return } if(getBinding().llLocation.visibility == View.GONE){ getBinding().llLocation.visibility = View.VISIBLE } // getBinding().tvLatlng.text = "位置:"+result.location.longitude+","+result.location.latitude // getBinding().tvLatlng.text = ""+result.location.longitude+","+result.location.latitude getBinding().tvLatlng.text = ""+ String.format("%.6f", result.location.longitude)+","+String.format("%.6f", result.location.latitude) getBinding().tvAddress.text = ""+result.address } }
-
Android 高德地图选点,定位;实现地图选点上车功能;
2019-12-05 11:07:21大部分打车软件的地图选点上车功能; 先上图: 实现的功能: 1、显示地图,以及当前位置的蓝点; 2、获取当前位置信息(定位); 3、获取地图中心点坐标,移动地图后获取地图中心点坐标; 4、逆地理编码,...实现的就是一个拖动地图,显示地图中心点的位置信息;大部分打车软件的地图选点上车功能;
先上图:
实现的功能:
1、显示地图,以及当前位置的蓝点;
2、获取当前位置信息(定位);
3、获取地图中心点坐标,移动地图后获取地图中心点坐标;
4、逆地理编码,根据当前经纬度获取附近的地理位置信息;
①、显示地图和蓝点,设置一些地图上面的显示信息:
if (aMap == null) { aMap = mMapView.getMap(); } //设置定位蓝点 MyLocationStyle myLocationStyle; myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)如果不设置myLocationType,默认也会执行此种模式。 myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER); myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。 myLocationStyle.strokeColor(Color.argb(0, 0, 0, 0));// 设置圆形的边框颜色 不显示范围圆圈 myLocationStyle.radiusFillColor(Color.argb(0, 0, 0, 0));// 设置圆形的填充颜色 不显示范围圆圈 aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style aMap.getUiSettings().setMyLocationButtonEnabled(true);//设置默认定位按钮是否显示,非必需设置。 aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点,false表示隐藏定位蓝点并不进行定位,默认是false。 //设置缩放级别 aMap.moveCamera(CameraUpdateFactory.zoomTo(15)); // 控件交互 缩放按钮、指南针、定位按钮、比例尺等 UiSettings mUiSettings;//定义一个UiSettings对象 mUiSettings = aMap.getUiSettings();//实例化UiSettings类对象 mUiSettings.setZoomControlsEnabled(false); mUiSettings.setMyLocationButtonEnabled(true); //显示默认的定位按钮 aMap.setMyLocationEnabled(true);// 可触发定位并显示当前位置 mUiSettings.setScaleControlsEnabled(true);//控制比例尺控件是否显示 mUiSettings.setLogoPosition(AMapOptions.LOGO_MARGIN_LEFT);//设置logo位置
②、获取当前位置,并把当前位置设置为地图中心点;
//获取位置信息 mlocationClient = new AMapLocationClient(this); //初始化定位参数 mLocationOption = new AMapLocationClientOption(); //设置定位监听 mlocationClient.setLocationListener(MapActivity.this); //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //设置定位间隔,单位毫秒,默认为2000ms mLocationOption.setInterval(-1000); mLocationOption.setOnceLocation(true); //设置定位参数 mlocationClient.setLocationOption(mLocationOption); //启动定位 mlocationClient.startLocation();
@Override public void onLocationChanged(AMapLocation amapLocation) { if (amapLocation != null && amapLocation.getErrorCode() == AMapLocation.LOCATION_SUCCESS) { L.cc(amapLocation.toStr()); setMapCenter(amapLocation); } else { //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。 L.cc("AmapError", "location Error, ErrCode:" + amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorInfo()); } mlocationClient.stopLocation(); mlocationClient.onDestroy(); } private void setMapCenter(AMapLocation amapLocation) { aMap.animateCamera(CameraUpdateFactory.newCameraPosition( new CameraPosition(new LatLng(amapLocation.getLatitude(), amapLocation.getLongitude()) , 15, 0, 0)), 300, null); //设置地图中心点 }
③、移动地图后,获取中心点位置经纬度,并获取该位置的位置信息;
//地图移动监听 aMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() { @Override public void onCameraChange(CameraPosition cameraPosition) { } @Override public void onCameraChangeFinish(CameraPosition cameraPosition) { L.cc(cameraPosition.toString()); // setMarker(cameraPosition.target); animTranslate(); getGeocodeSearch(cameraPosition.target); } });
private AnimatorSet animatorSet; public void animTranslate(){ if (animatorSet == null) { animatorSet = new AnimatorSet(); animatorSet.playTogether(ObjectAnimator.ofFloat(ivLocation, "scaleX", 1, 0.5f, 1).setDuration(300) , ObjectAnimator.ofFloat(ivLocation, "scaleY", 1, 0.5f, 1).setDuration(300)); }animatorSet.start(); } private GeocodeSearch geocoderSearch; //逆地理编码获取当前位置信息 private void getGeocodeSearch(LatLng targe) { if (geocoderSearch == null) geocoderSearch = new GeocodeSearch(this); geocoderSearch.setOnGeocodeSearchListener(this); // 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系 RegeocodeQuery query = new RegeocodeQuery(new LatLonPoint(targe.latitude, targe.longitude), 1000, GeocodeSearch.AMAP); geocoderSearch.getFromLocationAsyn(query); } @Override public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) { if (i != 1000) return; mAdapter.setNewData(regeocodeResult.getRegeocodeAddress().getPois()); tvLocationHeader.setText(regeocodeResult.getRegeocodeAddress().getProvince() + (TextUtils.equals(regeocodeResult.getRegeocodeAddress().getCity(), regeocodeResult.getRegeocodeAddress().getProvince()) ? "" : regeocodeResult.getRegeocodeAddress().getCity()) + regeocodeResult.getRegeocodeAddress().getDistrict()); tvLocation.setText("当前位置:" +regeocodeResult.getRegeocodeAddress().getFormatAddress()); } @Override public void onGeocodeSearched(GeocodeResult geocodeResult, int i) { }
OK,这样就完成了,上面的代码供参考;
下面是全部的代码:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="@color/main_white" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/mapLayout" android:layout_width="match_parent" android:layout_height="@dimen/dp260"> <com.amap.api.maps.MapView android:id="@+id/map" android:layout_width="match_parent" android:layout_height="260dp" /> <View android:id="@+id/vCenter" android:layout_width="1px" android:layout_height="1px" android:layout_centerInParent="true" android:background="@color/transparent" /> <android.support.v7.widget.AppCompatImageView android:id="@+id/iv_location" android:layout_width="16dp" android:layout_height="20dp" android:focusable="false" android:layout_alignBottom="@id/vCenter" android:layout_centerHorizontal="true" android:src="@mipmap/icon_location_hei" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/mapLayout" android:orientation="vertical"> <TextView android:id="@+id/tvLocation" android:layout_width="match_parent" android:layout_height="40dp" android:paddingLeft="@dimen/dp8" android:gravity="center_vertical" android:text="我的位置" android:textColor="@color/main_text" /> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/line_color" /> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> </LinearLayout> </RelativeLayout>
import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.graphics.Color; import android.os.Bundle; import android.support.v7.widget.AppCompatImageView; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.view.animation.AccelerateInterpolator; import android.view.animation.LinearInterpolator; import android.view.animation.TranslateAnimation; import android.widget.TextView; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationClient; import com.amap.api.location.AMapLocationClientOption; import com.amap.api.location.AMapLocationListener; import com.amap.api.maps.AMap; import com.amap.api.maps.AMapOptions; import com.amap.api.maps.CameraUpdateFactory; import com.amap.api.maps.MapView; import com.amap.api.maps.UiSettings; import com.amap.api.maps.model.CameraPosition; import com.amap.api.maps.model.LatLng; import com.amap.api.maps.model.Marker; import com.amap.api.maps.model.MarkerOptions; import com.amap.api.maps.model.MyLocationStyle; import com.amap.api.maps.model.animation.Animation; import com.amap.api.maps.model.animation.RotateAnimation; import com.amap.api.services.core.LatLonPoint; import com.amap.api.services.core.PoiItem; import com.amap.api.services.geocoder.GeocodeResult; import com.amap.api.services.geocoder.GeocodeSearch; import com.amap.api.services.geocoder.RegeocodeQuery; import com.amap.api.services.geocoder.RegeocodeResult; import com.chad.library.adapter.base.BaseQuickAdapter; import com.chad.library.adapter.base.BaseViewHolder; import com.example.admin.ccb.R; import www.ccb.com.common.base.BaseActivity; import www.ccb.com.common.utils.L; import www.ccb.com.common.utils.ToastUtils; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; public class MapActivity extends BaseActivity implements View.OnClickListener, AMapLocationListener, GeocodeSearch.OnGeocodeSearchListener { private BaseQuickAdapter<PoiItem, BaseViewHolder> mAdapter; @Override public int getContentViewResource() { return R.layout.activity_map; } private MapView mMapView; private AMap aMap = null; private TextView tvLocation, tvLocationHeader; private RecyclerView rv; private View rvHeadView; private AppCompatImageView ivLocation; //声明mlocationClient对象 public AMapLocationClient mlocationClient; //声明mLocationOption对象 public AMapLocationClientOption mLocationOption = null; @Override protected void initView() { UpTitle(null); mMapView = findViewById(R.id.map); mMapView.onCreate(savedInstanceState); rv = findViewById(R.id.rv); ivLocation = findViewById(R.id.iv_location); rv.setLayoutManager(new LinearLayoutManager(this)); mAdapter = new BaseQuickAdapter<PoiItem, BaseViewHolder>(R.layout.item_maplocation_msg) { @Override protected void convert(BaseViewHolder helper, PoiItem item) { helper.setText(R.id.tvTitle, item.getTitle()); helper.setText(R.id.tvContent, item.getSnippet()); helper.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ToastUtils.showToast(item.getTitle()); } }); } }; rv.setAdapter(mAdapter); rvHeadView = View.inflate(mContext, R.layout.item_maplocation_head, null); tvLocationHeader = rvHeadView.findViewById(R.id.tvTitle); mAdapter.setHeaderView(rvHeadView); } @Override protected void initData() { if (aMap == null) { aMap = mMapView.getMap(); } //设置定位蓝点 MyLocationStyle myLocationStyle; myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)如果不设置myLocationType,默认也会执行此种模式。 myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER); myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。 myLocationStyle.strokeColor(Color.argb(0, 0, 0, 0));// 设置圆形的边框颜色 不显示范围圆圈 myLocationStyle.radiusFillColor(Color.argb(0, 0, 0, 0));// 设置圆形的填充颜色 不显示范围圆圈 aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style aMap.getUiSettings().setMyLocationButtonEnabled(true);//设置默认定位按钮是否显示,非必需设置。 aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点,false表示隐藏定位蓝点并不进行定位,默认是false。 //设置缩放级别 aMap.moveCamera(CameraUpdateFactory.zoomTo(15)); // 控件交互 缩放按钮、指南针、定位按钮、比例尺等 UiSettings mUiSettings;//定义一个UiSettings对象 mUiSettings = aMap.getUiSettings();//实例化UiSettings类对象 mUiSettings.setZoomControlsEnabled(false); mUiSettings.setMyLocationButtonEnabled(true); //显示默认的定位按钮 aMap.setMyLocationEnabled(true);// 可触发定位并显示当前位置 mUiSettings.setScaleControlsEnabled(true);//控制比例尺控件是否显示 mUiSettings.setLogoPosition(AMapOptions.LOGO_MARGIN_LEFT);//设置logo位置 goLocation(); //地图移动监听 aMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() { @Override public void onCameraChange(CameraPosition cameraPosition) { } @Override public void onCameraChangeFinish(CameraPosition cameraPosition) { L.cc(cameraPosition.toString()); // setMarker(cameraPosition.target); animTranslate(); getGeocodeSearch(cameraPosition.target); } }); } private void goLocation() { //获取位置信息 mlocationClient = new AMapLocationClient(this); //初始化定位参数 mLocationOption = new AMapLocationClientOption(); //设置定位监听 mlocationClient.setLocationListener(MapActivity.this); //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //设置定位间隔,单位毫秒,默认为2000ms mLocationOption.setInterval(-1000); mLocationOption.setOnceLocation(true); //设置定位参数 mlocationClient.setLocationOption(mLocationOption); //启动定位 mlocationClient.startLocation(); } @Override protected void initList() { tvLocation = findViewById(R.id.tvLocation); tvLocation.setOnClickListener(this); } private Marker centerMarker; private void setMarker(LatLng target) { if (centerMarker != null) centerMarker.remove(); centerMarker = aMap.addMarker(new MarkerOptions().position(target).title("").snippet("")); Animation animation = new RotateAnimation(centerMarker.getRotateAngle() - 180, centerMarker.getRotateAngle(), 0, 0, 0); // Animation animation = new TranslateAnimation(target); animation.setDuration(360L); animation.setInterpolator(new LinearInterpolator()); centerMarker.setAnimation(animation); centerMarker.startAnimation(); } private AnimatorSet animatorSet; public void animTranslate(){ if (animatorSet == null) { animatorSet = new AnimatorSet(); animatorSet.playTogether(ObjectAnimator.ofFloat(ivLocation, "scaleX", 1, 0.5f, 1).setDuration(300) , ObjectAnimator.ofFloat(ivLocation, "scaleY", 1, 0.5f, 1).setDuration(300)); }animatorSet.start(); } private GeocodeSearch geocoderSearch; //逆地理编码获取当前位置信息 private void getGeocodeSearch(LatLng targe) { if (geocoderSearch == null) geocoderSearch = new GeocodeSearch(this); geocoderSearch.setOnGeocodeSearchListener(this); // 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系 RegeocodeQuery query = new RegeocodeQuery(new LatLonPoint(targe.latitude, targe.longitude), 1000, GeocodeSearch.AMAP); geocoderSearch.getFromLocationAsyn(query); } @Override public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) { if (i != 1000) return; mAdapter.setNewData(regeocodeResult.getRegeocodeAddress().getPois()); tvLocationHeader.setText(regeocodeResult.getRegeocodeAddress().getProvince() + (TextUtils.equals(regeocodeResult.getRegeocodeAddress().getCity(), regeocodeResult.getRegeocodeAddress().getProvince()) ? "" : regeocodeResult.getRegeocodeAddress().getCity()) + regeocodeResult.getRegeocodeAddress().getDistrict()); tvLocation.setText("当前位置:" +regeocodeResult.getRegeocodeAddress().getFormatAddress()); } @Override public void onGeocodeSearched(GeocodeResult geocodeResult, int i) { } @Override public void onClick(View view) { switch (view.getId()) { case R.id.tvLocation: break; } } @Override public void onLocationChanged(AMapLocation amapLocation) { if (amapLocation != null && amapLocation.getErrorCode() == AMapLocation.LOCATION_SUCCESS) { L.cc(amapLocation.toStr()); setMapCenter(amapLocation); } else { //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。 L.cc("AmapError", "location Error, ErrCode:" + amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorInfo()); } mlocationClient.stopLocation(); mlocationClient.onDestroy(); } private void setMapCenter(AMapLocation amapLocation) { aMap.animateCamera(CameraUpdateFactory.newCameraPosition( new CameraPosition(new LatLng(amapLocation.getLatitude(), amapLocation.getLongitude()) , 15, 0, 0)), 300, null); //设置地图中心点 } @Override protected void onDestroy() { super.onDestroy(); //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图 mMapView.onDestroy(); } @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制 mMapView.onPause(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态 mMapView.onSaveInstanceState(outState); } }
有很多的基类我就不贴上来了,如果你感觉要的是这样的效果,欢迎查看源码:https://github.com/CuiChenbo/CcMall
-
百度地图选点
2014-08-29 15:46:14利用百度地图开发接口 拖动地图获取中心坐标以及周围一些POI数据 -
微信小程序+腾讯地图 获取定位与地图选点插件
2022-01-03 15:46:26文章目录一、思路二、逆地址解析2.1. app.json2.2. 页面加入2.3. 后台代码三、地图插件...给个按钮让用户点击调用腾讯地图选点插件,自己选择位置修改 二、逆地址解析 2.1. app.json .小程序页面代码 app.json必须加.
腾讯位置服务官网: https://lbs.qq.com一、思路
通过 wx.getLocation 返回经纬度传到后台,后台调用腾讯地图提供的逆地址解析返回用户位置;
给个按钮让用户点击调用腾讯地图选点插件,自己选择位置修改二、逆地址解析
2.1. app.json
.小程序页面代码
app.json必须加入"permission": { "scope.userLocation": { "desc": "你的位置信息将用于小程序位置接口的效果展示" } }
2.2. 页面加入
onLoad: function (options) { let that = this; that.authodAdress(); } authodAdress() { //是否授权获取地址 let that = this; wx.getSetting({ success: (res) => { if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) { wx.showModal({ title: '是否获取当前位置', content: '需要获取您的地理位置,请确认授权,否则无法获取您所需数据', success: function (res) { if (res.cancel) { wx.showModal({ title: '授权失败', icon: 'success', duration: 1000 }) } else if (res.confirm) { wx.openSetting({ success: function (dataAu) { if (dataAu.authSetting["scope.userLocation"] == true) { wx.showModal({ title: '授权成功', icon: 'success', duration: 1000 }) that.getAddress(); } else { wx.showModal({ title: '授权失败', icon: 'success', duration: 1000 }) } } }) } } }) } else if (res.authSetting['scope.userLocation'] == undefined) { that.getAddress(); } else { that.getAddress(); } } }) }, getAddress() { //获取地址 let that = this; wx.getLocation({ type: 'wgs84', isHighAccuracy: true,//开启高精度定位 success(res) { console.log("获取地理位置----------") console.log(res) //这里改成自己封装好调用后台的api locationApi.getLocationConvert(res).then((apiRes) => { console.log("调用后台返回地址--------") console.log(apiRes) }) } }) },
2.3. 后台代码
//逆地址解析url private static final String locationUrl = "https://apis.map.qq.com/ws/geocoder/v1/"; /** * 逆地址解析 * * @param lat 纬度 * @param lng 经度 **/ public static HttpClientResult convertPosition(String lat, String lng) throws Exception { Map<String, String> param = new HashMap<>(16); param.put("location", String.format("%s,%s", lat, lng)); param.put("key", "腾讯地图开发密钥"); param.put("output", "json"); //改成自己封装好的调用接口 HttpClientResult httpClientResult = getHttpClientResult(locationUrl, param); if (!httpClientResult.getContent().isEmpty()) { String all = httpClientResult.getContent().toJSONString().replaceAll("(?<=\"lat\":\\s)(\\d+\\.\\d+)", "\"$1\"").replaceAll("(?<=\"lng\":\\s)(\\d+\\.\\d+)", "\"$1\""); httpClientResult.setContent(JSONObject.parseObject(all)); } return httpClientResult; }
注意:在微信开发工具里,点击取消授权之后在进来点击确定,可能无法进入用户权限设置页面。在真机上调试就没问题
成功实例:
三、地图插件调用
3.1. app.json加入
"plugins": { "chooseLocation": { "version": "1.0.5", "provider": "wx76a9a06e5b4e693e" } }
3.2. js页面加入
const chooseLocation = requirePlugin('chooseLocation');//导入插件 onShow: function () { let that = this; // 从地图选点插件返回后,在页面的onShow生命周期函数中能够调用插件接口,取得选点结果对象 const location = chooseLocation.getLocation();// 如果点击确认选点按钮,则返回选点结果对象,否则返回null } showMap() { //显示地图 const key = ""; //使用在腾讯位置服务申请的key const referer = '星火之志'; //调用插件的app的名称 wx.navigateTo({ url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer }); //老版本调用 // wx.chooseLocation({ // success: function(e) { // console.log(e) // t.setData({ // // now_location_name: e.address, // // now_location_lat: e.latitude, // // now_location_lng: e.longitude, // // now_detail_address: e.name // }); // }, // fail: function(t) {console.log(t)}, // complete: function(t) {console.log(t)} // }); }
3.3. wxml页面
<button bindtap="showMap" style="margin-top:10px">选择位置</button>
注:可能在微信开发者工具上调用时会报错,不过在真机上调试就没问题
成功截图:
-
uni-app中使用腾讯位置服务实现小程序地图选点功能
2022-01-01 12:14:44文章目录1. 官方文档2. 小程序添加插件3....技术选定(地图选点插件) (对应官网:https://lbs.qq.com/miniProgram/plugin/pluginGuide/locationPicker ) 2. 小程序添加插件 去微信小程序中-设置 ... -
微信小程序-微信小程序地图定位选点存储
2019-08-06 03:58:40微信小程序地图选点收藏Demo 本程序是用于测试微信小程序地图功能及与LeanCloud数据存储管理功能的Demo。 本程序目前未添加AppID无法进行真机调试,欢迎已经申请到资格的朋友测试反馈。 本程序数据管理功能基于... -
【无标题】vue-baidu-map地图选点(选点,搜索定位,通过经纬度展现)
2021-12-13 14:49:461.安装 1)安装 ... // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */ ak: 'YOUR_APP_KEY' }) 参考vue-baidu-map官方网址 2.组件封装 1)查询组件封装,新建ba -
uniapp之地图选点插件
2022-03-07 19:18:59先找到地图选点插件 2.插件申请接入(直接点击“点击浏览插件最新版本”即可添加,亲测好用) 3.将下方代码添加到uniapp项目的manifest.json中 { "plugins": { "chooseLocation": { "version": "1.0.9... -
地图选点-SDK 示例 | 高德地图API
2021-06-06 15:01:28用到产品核心类/接口类接口说明版本MAMapViewsetCenterCoordinate:animated:设置地图中心点。V2.0.0版本起AMapSearchAPIAMapReGoecodeSearch:逆地址编码查询接口。V3.0.0版本起AMapPOIAroundSearch:POI周边查询接口... -
uniapp调用腾讯位置服务地图选点插件
2022-04-17 13:14:30一、申请腾讯位置服务的key ...5、打开微信小程序插件,选择合适的插件类型,下面以地图选点插件作为例子。 6、在uniapp项目中加入以下代码引入插件 (1)打开manifest.json文件=》源码视.. -
微信小程序使用腾讯位置服务地图选点实现地址的选取|微信小程序腾讯位置服务地图选点请求来源未被授权
2022-03-24 17:31:03注意在配置的时候webapi的域名不要填写,不如然会有微信小程序腾讯位置服务地图选点请求来源未被授权的提示如下 出现这种情况也可能是你的key和应用名写错了 然后开始写代码 app.json代码 ... -
高德地图选点地图打点 vue+element ui
2021-12-31 15:03:13--高德地图js--> <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.14&key=2d5879db229ad4cca377758bc00ecd93&plugin=AMap.Driving"></script> 2.父页面引. -
地图选点功能
2019-04-29 10:56:55在详设阶段,需要实现地图选点,这个自己之前从来没有实现过,现在来实现一下,这个过程中参考了高德地图的官方开发文档和vue-amap文档,地址链接分别如下: 高德地图官方文档: ... vue-amap文档: ... -
【uni-app】腾讯地图选点
2020-10-10 17:34:33小程序实现地图选点 小程序前提配置appId和位置接口 h5和浏览器实现地图选点** 看官网即可https://lbs.qq.com/webApi/component/componentGuide/componentPicker 需要在官网申请key值 <web-... -
uniapp之APP的地图选点
2021-01-12 15:31:452.填写高德开放平台申请的key 3.代码 var that = this uni.chooseLocation({ success: function(res) { console.log('==='+JSON.stringify(res)) that.str_chu = res.name // this.str_mu=res.... -
腾讯地图选点组件使用示例
2021-01-18 12:09:531、选择地址发送(地图选点组件 ) 2、实时地址静态图 3、地图调起 ,手机查看 选址组件主要是选地点的信息,如下: 在js或者数据库中保存这些信息,静态图片和地图API的调起就要用到这些信息,主要还是经纬度。 ... -
高德地图web 输入提示+地图选点
2020-07-04 16:45:15<!... <... <head> ...meta charset="UTF-8">...高德地图</title> </head> <body> <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script> <bo. -
vue 中使用高德地图, 地图选点
2019-04-23 11:33:00功能简述: 网页版高德地图开发,主要支持地图选点(没做导航),搜索选点.(功能类似微信与好友聊天中的选择位置,发送位置,如下图) 第一步:先成为高德地图的开发者(获取高德地图的key);获取key 第二步:引入高德地图js... -
微信小程序 腾讯位置服务地图选点和路线规划
2020-07-14 14:18:34微信小程序使用 腾讯位置服务插件 实现 地图选点和路线规划 效果图: 实现过程 点击“开发指南”,根据对应打开网页中的接入指引进行开发实现。 腾讯位置服务地图选点:开发指南 腾讯位置服务路线规划:开发指南 ... -
腾讯地图选点组件
2019-12-23 16:13:38https://lbs.qq.com/tool/component-picker.html -
仿滴滴打车地图选点(包含地图选点,离线地图,导航路线)
2019-08-31 17:21:41根据项目的需要今天做一个百度地图得功能: 1、百度地图状态改变监听,用户拖动地图(可以设置只能在半径范围内活动)可以实时展示经纬度 2、接入全国各省市离线地图,下载离线地图后无网络情况下实时显示位置 3、... -
android百度地图选点/maker标记/显示默认位置
2021-01-12 15:44:13文档说明:使用百度地图,实现 设置默认地图中心点+地图选点+maker标记 功能(后面会增加对该点进行poi搜索功能,实现获取更详细的的位置信息,等一系列常用功能模块实现)。文中出现不对的地方,欢迎指正。后面持续... -
uni-app 微信小程序 腾讯地图选点插件
2021-12-02 09:49:50微信小程序插件 | 腾讯位置服务... 引入插件包和定位授权: "plugins": { "chooseLocation": { "version": "1.0.9", "provider": "wx76a9a06e5b4e693e" } ... "scope.userL -
腾讯位置服务地图选点
2021-08-08 06:32:54地图选点插件可以让用户快速、准确地选择并确认自己的当前位置,并将相关位置信息回传给开发者。同时我们还提供位置检索、关键词分类、POI主子点等辅助功能。1、分类筛选,地点信息精准呈现选点是地图应用中使用频率...
收藏数
2,331
精华内容
932