腾讯地图定位demo
来源:互联网 发布:淘宝网店如何起步 编辑:程序博客网 时间:2024/05/16 16:58
最近做了一个使用腾讯地图,使用定位功能的demo。
功能实现:自动定位显示经纬度,设置marker点,长按地图显示经纬度并设置marker。
准备工作申请key,下载jar包等操作,腾讯地图的开发者平台说的很清楚,不用多说
MainActivity布局
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.wjx.lbsdemo.MainActivity"> <LinearLayout android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="默认:" /> <!--经度--> <EditText android:id="@+id/edit_lon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <!--经度--> <EditText android:id="@+id/edit_lat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:id="@+id/ll_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/ll" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="手动:" /> <!--经度--> <EditText android:id="@+id/ll_2_edit_lon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:hint="经度"/> <!--经度--> <EditText android:id="@+id/ll_2_edit_lat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:hint="纬度"/> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/ll_2"> <com.tencent.tencentmap.mapsdk.map.MapView android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/textview_shape" android:text="(手动)长按地图获得所选点的经纬度" android:textSize="18sp" android:gravity="center_vertical" /> </RelativeLayout></RelativeLayout>
首先实现自动定位功能
一:创建定位监听,也可以通过实现。
TencentLocationListener listener = new TencentLocationListener() { @Override public void onLocationChanged(TencentLocation tencentLocation, int i, String s) { if (i == TencentLocation.ERROR_OK) { //定位成功 } else { Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_SHORT).show(); } } @Override public void onStatusUpdate(String s, int i, String s1) { } };
二:创建定位请求
TencentLocationRequest request = TencentLocationRequest.create(); request.setRequestLevel(1); request.setInterval(60000);//每分钟重新定位一次 TencentLocationManager locationManager = TencentLocationManager.getInstance(this); int i = locationManager.requestLocationUpdates(request, listener);
这样一个没60s定位一次的功能即可实现,在定位成功的监听中添加代码,显示经纬度,以及设置marker。
lon1,lat1为两个editText,显示经纬度
TencentLocationListener listener = new TencentLocationListener() { @Override public void onLocationChanged(TencentLocation tencentLocation, int i, String s) { if (i == TencentLocation.ERROR_OK) { //定位成功 lon1.setText(tencentLocation.getLongitude() + ""); lat1.setText(tencentLocation.getLatitude() + ""); LatLng latLng = new LatLng(tencentLocation.getLatitude(), tencentLocation.getLongitude()); Bitmap bitmap = BitmapFactory.decodeResource(MainActivity.this.getResources(), R.mipmap.ic_launcher); marker2 = mTencentMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory .fromBitmap(bitmap)) .draggable(true)); animateTo(tencentLocation); } else { Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_SHORT).show(); } } @Override public void onStatusUpdate(String s, int i, String s1) { } };··· ···//animateTo()方法,功能为将定位的坐标作为地图的中心点显示,mapview为腾讯地图private void animateTo(TencentLocation location) { if (location == null) { return; } mapView.getController().animateTo(Utils.of(location)); mapView.getController().setCenter(Utils.of(location)); }
实现长按地图设置marker点,并显示经纬度功能。lon2,lat2为两个Edittext,显示经纬度
MapView mapView = (MapView) findViewById(R.id.map);TencentMap mTencentMap = mapView.getMap();mTencentMap.setZoom(12);//设置定图显示等级 mTencentMap.setOnMapLongClickListener(new TencentMap.OnMapLongClickListener() { @Override public void onMapLongClick(LatLng latLng) { lon2.setText(latLng.getLongitude() + ""); lat2.setText(latLng.getLatitude() + ""); if (marker!=null) { marker.remove(); } marker = mTencentMap.addMarker(new MarkerOptions().position(latLng).icon( defaultMarker()) .draggable(true)); } });
效果显示定位:
demo下载地址:
http://download.csdn.net/detail/qq_33474233/9834370
0 0
- 腾讯地图定位demo
- 百度地图定位demo
- 百度地图定位demo
- 腾讯地图 通过url展示定位地图
- google地图定位小Demo
- Android百度地图定位demo
- 关于地图定位的demo
- 百度地图定位Demo补充
- 百度地图的定位demo
- iOS LocateMap(地图定位)~demo
- 高德地图定位demo
- 9Rgoogle地图定位小Demo
- Android开发-----定位功能的实现----腾讯地图定位
- 利用高德地图和腾讯地图的详细地址定位
- Demo:地图展示,定位,地理编码,标注,离线地图等
- 运行高德地图定位与地图实例Demo
- androidstudio腾讯地图集成定位绘制行走过的路线
- 移动web端使用腾讯地图实现定位功能
- 学习编写shell脚本(一)
- 泣血关于mysql安装卸载安装卸载
- listview,gridview里面通过一个item控制所有item的使用方法
- 第七节:定长与变长Array
- JDK动态代理
- 腾讯地图定位demo
- 匿名函数
- DOM4J操作XML
- ROS(Robot Operating System)常用环境变量介绍
- vim编辑代码自动补全设置
- 欧拉函数的一些模板+注释
- Eclipse中SVN修改的*星号没了,解决方法
- 第八节:Array的遍历-转换以及-常规用法
- 来谈谈WebAssembly是个啥?为何说它会影响每一个Web开发者?