【Android App】集成腾讯地图显示位置和地图面板讲解及实战(附源码和演示 超详细必看)
创始人
2024-03-06 19:58:09
0

需要源码请点赞关注收藏后评论区留言私信~~~

一、集成腾讯地图

之所以选用腾讯地图来讲解,是因为它的集成过程相对简单,无须通过App的签名鉴权,腾讯地图的开放平台网址为

腾讯地图

集成腾讯地图分为以下几步

(1)在腾讯地图开放平台的控制台界面创建新应用

(2)给App模块的build.gradle补充腾讯地图的依赖配置

(3)给AndroidManifest.xml添加相关的权限配置

(4)往AndroidManifest.xml的application节点下面添加名为TencentMapSDK的元数据配置,其值为第一步得到的Key密钥。

因为腾讯地图会通过HTTP接口访问网络,所以要修改application节点,增加如下一行属性配置

android:usesCleartextTraffic="true"

运行该App 结果如下

可以详细的显示地址 包括经纬度和具体地址 此处建议连接真机测试 模拟机可能会有一些问题 

 代码如下

Java类

package com.example.location;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;import com.example.location.util.DateUtil;
import com.tencent.map.geolocation.TencentLocation;
import com.tencent.map.geolocation.TencentLocationListener;
import com.tencent.map.geolocation.TencentLocationManager;
import com.tencent.map.geolocation.TencentLocationRequest;public class MapLocationActivity extends AppCompatActivity implements TencentLocationListener {private final static String TAG = "MapLocationActivity";private TencentLocationManager mLocationManager; // 声明一个腾讯定位管理器对象private TextView tv_location; // 声明一个文本视图对象@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_map_location);tv_location = findViewById(R.id.tv_location);initLocation(); // 初始化定位服务}// 初始化定位服务private void initLocation() {mLocationManager = TencentLocationManager.getInstance(this);// 创建腾讯定位请求对象TencentLocationRequest request = TencentLocationRequest.create();request.setInterval(30000).setAllowGPS(true);request.setRequestLevel(TencentLocationRequest.REQUEST_LEVEL_ADMIN_AREA);int error = mLocationManager.requestLocationUpdates(request, this); // 开始定位监听if (error == 0) {Log.d(TAG, "注册位置监听器成功!");} else {Log.d(TAG, "注册位置监听器失败!");}}@Overridepublic void onLocationChanged(TencentLocation location, int resultCode, String resultDesc) {if (resultCode == TencentLocation.ERROR_OK && location != null) { // 定位成功String desc = String.format("您当前的位置信息如下:\n定位时间:%s\n" +"纬度:%f\n经度:%f\n省份:%s\n城市:%s\n" +"区域:%s\n街道:%s\n门牌号:%s\n详细地址:%s",DateUtil.formatDate(location.getTime()),location.getLatitude(), location.getLongitude(),location.getProvince(), location.getCity(),location.getDistrict(), location.getStreet(),location.getStreetNo(), location.getAddress());tv_location.setText(desc);} else { // 定位失败Log.d(TAG, "定位失败,错误代码为"+resultCode+",错误描述为"+resultDesc);}}@Overridepublic void onStatusUpdate(String s, int i, String s1) {}@Overrideprotected void onDestroy() {super.onDestroy();mLocationManager.removeUpdates(this); // 移除定位监听}
}

二、显示地图面板

调用MapView的getMap方法获取腾讯地图对象TencentMap,再通过地图对象的下列方法操作地图:

setMapType:设置地图类型。

setTrafficEnabled:设置是否显示交通拥堵状况。

moveCamera:把相机视角移动到指定地点。

animateCamera:动态调整相机视角。

addMarker:往地图添加标记。

clearAllOverlays:清除所有覆盖物。

setOnMapClickListener:设置地图的点击监听器。

setOnMarkerClickListener:设置地图标记的点击监听器。

.运行测试App 可以在普通地图 卫星地图下切换 并且可以实时显示当地的交通情况 非常方便 

卫星地图显示如下 

 

代码如下

Java类 

package com.example.location;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.RadioGroup;import com.tencent.map.geolocation.TencentLocation;
import com.tencent.map.geolocation.TencentLocationListener;
import com.tencent.map.geolocation.TencentLocationManager;
import com.tencent.map.geolocation.TencentLocationRequest;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdate;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory;
import com.tencent.tencentmap.mapsdk.maps.MapView;
import com.tencent.tencentmap.mapsdk.maps.TencentMap;
import com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptor;
import com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptorFactory;
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;
import com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions;public class MapBasicActivity extends AppCompatActivity implements TencentLocationListener {private final static String TAG = "MapBasicActivity";private TencentLocationManager mLocationManager; // 声明一个腾讯定位管理器对象private MapView mMapView; // 声明一个地图视图对象private TencentMap mTencentMap; // 声明一个腾讯地图对象private boolean isFirstLoc = true; // 是否首次定位@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_map_basic);initLocation(); // 初始化定位服务initView(); // 初始化视图}// 初始化视图private void initView() {RadioGroup rg_type = findViewById(R.id.rg_type);rg_type.setOnCheckedChangeListener((group, checkedId) -> {if (checkedId == R.id.rb_common) {mTencentMap.setMapType(TencentMap.MAP_TYPE_NORMAL); // 设置普通地图} else if (checkedId == R.id.rb_satellite) {mTencentMap.setMapType(TencentMap.MAP_TYPE_SATELLITE); // 设置卫星地图}});CheckBox ck_traffic = findViewById(R.id.ck_traffic);ck_traffic.setOnCheckedChangeListener((buttonView, isChecked) -> {mTencentMap.setTrafficEnabled(isChecked); // 是否显示交通拥堵状况});}// 初始化定位服务private void initLocation() {mMapView = findViewById(R.id.mapView);mTencentMap = mMapView.getMap(); // 获取腾讯地图对象mLocationManager = TencentLocationManager.getInstance(this);// 创建腾讯定位请求对象TencentLocationRequest request = TencentLocationRequest.create();request.setInterval(30000).setAllowGPS(true);request.setRequestLevel(TencentLocationRequest.REQUEST_LEVEL_ADMIN_AREA);mLocationManager.requestLocationUpdates(request, this); // 开始定位监听}@Overridepublic void onLocationChanged(TencentLocation location, int resultCode, String resultDesc) {if (resultCode == TencentLocation.ERROR_OK) { // 定位成功if (location != null && isFirstLoc) { // 首次定位isFirstLoc = false;// 创建一个经纬度对象LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, 12);mTencentMap.moveCamera(update); // 把相机视角移动到指定地点// 从指定图片中获取位图描述BitmapDescriptor bitmapDesc = BitmapDescriptorFactory.fromResource(R.drawable.icon_locate);MarkerOptions ooMarker = new MarkerOptions(latLng).draggable(false) // 不可拖动.visible(true).icon(bitmapDesc).snippet("这是您的当前位置");mTencentMap.addMarker(ooMarker); // 往地图添加标记}} else { // 定位失败Log.d(TAG, "定位失败,错误代码为"+resultCode+",错误描述为"+resultDesc);}}@Overridepublic void onStatusUpdate(String s, int i, String s1) {}@Overrideprotected void onStart() {super.onStart();mMapView.onStart();}@Overrideprotected void onStop() {super.onStop();mMapView.onStop();}@Overridepublic void onPause() {super.onPause();mMapView.onPause();}@Overridepublic void onResume() {super.onResume();mMapView.onResume();}@Overrideprotected void onDestroy() {super.onDestroy();mLocationManager.removeUpdates(this); // 移除定位监听mMapView.onDestroy();}}

创作不易 觉得有帮助请点赞关注收藏~~~

相关内容

热门资讯

视频丨“粤车南下”驶入香港市区... 今天(12月23日),“粤车南下”驶入香港市区政策正式实施,符合条件的广东私家车可直接驶入香港市区,...
立白回应与经销商解约纠纷:个别... 12月23日,红星新闻报道《多地代理商称与立白集团解约后 对方未按约交接市场致严重损失,律师解读》一...
原创 宁... 据北京日报报道,12月中下旬的东亚地缘舞台格外热闹,日本首相高市早苗的一系列外交动作让外界看得眼花缭...
原创 阿... ATP针对大师赛和500赛事稳定参赛所设立的奖金,金额已可与大满贯奖金相媲美。目前的奖金榜显示,阿尔...
长春一女子打车遇“最炫出租车”... 极目新闻记者 王柳钦 近日,有网友发视频称,她和朋友在吉林长春乘坐出租车时,意外坐上了一辆改装有绚丽...
一次性信用修复政策落地,消金公... 一次性信用修复政策重磅落地,12月22日,中国人民银行发布一次性信用修复政策:明确对特定时期、特定金...
从“无”到“有”、从“有”到“... 浦东新区法律援助30周年暨《上海市法律援助若干规定》专题研讨会日前在浦东国际法律服务园举行。 本次研...
暴涨500%!19岁巴尔泰萨吉... 在意甲的舞台上,年轻球员的崛起往往令人瞩目,而19岁的巴尔泰萨吉正是近期最耀眼的新星之一。随着德转最...
完善幼儿园收费政策!三部门发文 来源 | 国家发展改革委微信,转自中国政府网微信 近日,国家发展改革委、教育部、财政部联合印发《关于...
老人“睡梦中”离世,警方靠共享... 孙大妈躺在床上 被子整整齐齐地盖在身上 就像睡着了一样 人却已经没有了呼吸 根据取款人的车辆骑行轨迹...