总结
现在新技术层出不穷,如果每次出新的技术,我们都深入的研究的话,很容易分散精力。新的技术可能很久之后我们才会在工作中用得上,当学的新技术无法学以致用,很容易被我们遗忘,到最后真的需要使用的时候,又要从头来过(虽然上手会更快)。
我觉得身为技术人,针对新技术应该是持拥抱态度的,入了这一行你就应该知道这是一个活到老学到老的行业,所以面对新技术,不要抵触,拥抱变化就好了。
Flutter 明显是一种全新的技术,而对于这个新技术在发布之初,花一个月的时间学习它,成本确实过高。但是周末花一天时间体验一下它的开发流程,了解一下它的优缺点、能干什么或者不能干什么。这个时间,并不是我们不能接受的。
如果有时间,其实通读一遍 Flutter 的文档,是最全面的一次对 Flutter 的了解过程。但是如果我们只有 8 小时的时间,我希望能关注一些最值得关注的点。
(跨平台开发(Flutter)、java基础与原理,自定义view、NDK、架构设计、性能优化、完整商业项目开发等)
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
放在停止定位按钮的下面,然后进入到MainActivity
//添加围栏
private Button btnAddFence;
//移除围栏
private Button btnRemoveFence;
然后在initView中
onClick中增加
在使用地理围栏时会用到广播,这里在com.llw.demo下右键创建一个receiver包,然后新建一个GeofenceEventReceiver继承BroadcastReceiver,然后重写onReceive方法,代码如下:
package com.llw.demo.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.widget.Toast;
public class GeofenceEventReceiver extends BroadcastReceiver {
private static final String ACTION_TRIGGER_GEOFENCE = “com.llw.demo.receiver.GeofenceEventReceiver”;
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null
|| !GeofenceEventReceiver.ACTION_TRIGGER_GEOFENCE.equals(intent
.getAction())) {
return;
}
String tag = intent.getStringExtra(“tag”);
double lng = intent.getDoubleExtra(“longitude”, 0);
double lat = intent.getDoubleExtra(“latitude”, 0);
// 进入围栏还是退出围栏
boolean enter = intent.getBooleanExtra(
LocationManager.KEY_PROXIMITY_ENTERING, true);
Toast.makeText(context,“是否进入围栏范围:”+ enter,Toast.LENGTH_SHORT).show();
}
}
然后广播有两种注册方式,静态和动态的,下面先来看静态的注册,打开AndroidManifest.xml
添加如下配置
然后还需要配置一个权限,这个权限不需要动态申请。
下面回到MainActivity,去定义一些变量,并且完成地理围栏的初始化配置。
//地理围栏管理
private TencentGeofenceManager mTencentGeofenceManager;
//围栏别名
private String geofenceName = “测试范围”;
//围栏
private TencentGeofence geofence;
//设置动作
private static final String ACTION_TRIGGER_GEOFENCE = “com.llw.demo.receiver.GeofenceEventReceiver”;
//PendingIntent
private PendingIntent pi;
然后新建一个初始化地理围栏的方法。
/**
- 初始化地理围栏
*/
private void initGeofence() {
//实例化
mTencentGeofenceManager = new TencentGeofenceManager(this);
//地理围栏构建
TencentGeofence.Builder builder = new TencentGeofence.Builder();
geofence = builder.setTag(geofenceName)
//设置圆心和半径,v 是 纬度,v1 是经度,v2 是半径 500米
.setCircularRegion(22.5, 113.9, 500)
//设置地理围栏有效期
.setExpirationDuration(3 * 3600 * 1000)
//完成构建
.build();
//构建Action和传递信息
Intent receiverIntent = new Intent(ACTION_TRIGGER_GEOFENCE);
receiverIntent.putExtra(“tag”, geofence.getTag());
receiverIntent.putExtra(“longitude”, geofence.getLongitude());
receiverIntent.putExtra(“latitude”, geofence.getLatitude());
// 随机产生的 requestCode, 避免冲突
int requestCode = (int) (Math.random() * 1E7);
//构建PendingIntent
pi = PendingIntent.getBroadcast(this, requestCode,
receiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
上述方法完成了对地理围栏的配置,并且对拦截器的配置和参数的传递,该方法在onCreate中调用。
然后就是在添加围栏和移除围栏中触发
case R.id.btn_add_fence:
//添加围栏
mTencentGeofenceManager.addFence(geofence,pi);
showMsg(“地理围栏已添加,请在附近溜达一下”);
break;
case R.id.btn_remove_fence:
//移除围栏
//指定围栏对象移除
mTencentGeofenceManager.removeFence(geofence);
//通过tag移除
//mTencentGeofenceManager.removeFence(geofenceName);
showMsg(“地理围栏已移除,撒有那拉!”);
break;
最后就是在页面销毁的时候,移除所有围栏并且销毁围栏管理。
@Override
protected void onDestroy() {
super.onDestroy();
//移除所有围栏
mTencentGeofenceManager.removeAllFences();
//销毁围栏管理
mTencentGeofenceManager.destroy();
}
下面来看看动态广播
//广播接收器
private GeofenceEventReceiver geofenceEventReceiver;
然后在initGeofence方法中完成动态广播的配置和注册。同时记得去掉AndroidManifest.xml的静态广播配置
//实例化
geofenceEventReceiver = new GeofenceEventReceiver();
//添加动作拦截
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_TRIGGER_GEOFENCE);
//注册动态广播 记得去掉AndroidManifest.xml的静态广播配置
registerReceiver(geofenceEventReceiver,intentFilter);
OK,代码写完了,自行去测试吧,这里我就不放效果图了。那么到这里腾讯的定位SDK就大概讲述完毕,下面进入地图SDK环节。
说到地图SDK,分为3D地图SDK、2D地图SDK。这里我使用3D的地图SDK,这个看起来技术含量就高一点。在文档中选择Android地图SDK,然后点击下载,下载3D版地图SDK。
下载后解压依然放在D盘的根目录,注意需要的东西都在libs里面。
打开libs,然后先复制jar包到lib下
右键点击这个jar包,然后选择Add As Library…
然后会出现这样一个弹窗
就是把这个依赖添加到当前的app模块中,点击OK。然后会进行同步,同步完你就会发现你的这个jar包可以打开了,这也是一种添加jar包的方式。
下面添加需要的so文件。打开解压文件的jniLibs文件夹,将里面的6个文件夹全选,然后复制,之后粘贴到你项目的jniLibs文件夹中,会自行合并的,你不用担心。
现在我们就已经将地图SDK也添加到了项目中,下面就该来使用了,在此之前,重新编译一下项目,点击Build → Rebuild Project,编译通过之后,重新运行在手机上,为什么要这么做呢?为了排错,每次当你对项目中的依赖库或者SDK有更改时,最好都去重新编译一下你的项目,之后运行一次,如果这个时候报错你是不是一下就能知道错误的原因呢?千万不要等到你把所有代码写完然后最后来运行,到时候报错的话,你排错就要花很多时间,得不偿失,当然这只是我的一个小建议,你开心你就想怎样就怎样。
OK,很显然这个地图SDK和与定位SDK没有起冲突,可以一起使用。
① 基础地图
既然要显示基础地图,那么先修改一下activity_main.xml,保险起见我这里贴一下完整的布局代码:
<?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”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
tools:context=“.MainActivity”>
<androidx.core.widget.NestedScrollView
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<LinearLayout
android:orientation=“vertical”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:padding=“8dp”
android:text=“定位”
android:textSize=“18sp” />
<Button
android:id=“@+id/btn_continuous_positioning”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“连续定位” />
<Button
android:id=“@+id/btn_single_positioning”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“单次定位” />
<Button
android:id=“@+id/btn_background_positioning”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“后台定位” />
<Button
android:id=“@+id/btn_stop_positioning”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“停止定位” />
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:padding=“8dp”
android:text=“地理围栏”
android:textSize=“18sp” />
<Button
android:id=“@+id/btn_add_fence”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“添加围栏” />
<Button
android:id=“@+id/btn_remove_fence”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“移除围栏” />
<TextView
android:id=“@+id/tv_location_info”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“center_horizontal”
android:padding=“8dp”
android:text=“位置信息”
android:textColor=“#000”
android:textSize=“18sp” />
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:padding=“8dp”
android:text=“地图”
android:textSize=“18sp” />
<Button
android:id=“@+id/btn_base_map”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“基础地图” />
</androidx.core.widget.NestedScrollView>
根布局中加了一个滚动布局,滚动布局中增加一个线性布局,线性布局中放置之前的控件,并且增加一个按钮,用于跳转到新的Activity,在com.llw.demo下新建一个map包,右键点击,选择New →Activity→Empty Activity,命名为BaseMapActivity,布局为activity_base_map.xml。
然后修改MainActivity中的代码,
//基础地图
private Button btnBaseMap;
initView中
点击事件中
下面在activity_base_map.xml中增加一个地图控件。
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout 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”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“.map.BaseMapActivity”>
<com.tencent.tencentmap.mapsdk.maps.MapView
android:id=“@+id/mapView”
android:layout_width=“match_parent”
android:layout_height=“match_parent” />
</androidx.constraintlayout.widget.ConstraintLayout>
然后进入BaseMapActivity。
//基础地图
private MapView mapView;
//腾讯地图
private TencentMap tencentMap;
onCreate中,
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_map);
//地图
mapView = findViewById(R.id.mapView);
//创建tencentMap地图对象
tencentMap = mapView.getMap();
}
同时还要管理map的生命周期,
/**
- mapview的生命周期管理
*/
@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
protected void onRestart() {
super.onRestart();
mapView.onRestart();
}
下面来运行一下:
这样地图就显示出来了,其实刚才页面的代码还可以简化一下,因为map的声明周期与Activity的生命周期有关,这里就很适合使用LifeCycle来单独管理这个Activity的生命周期,同时对map进行管理。
下面在map包下新建一个MapLifecycle类,里面代码如下
package com.llw.demo.map;
import android.util.Log;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import com.tencent.tencentmap.mapsdk.maps.MapView;
/**
-
地图生命周期
-
@author llw
*/
public class MapLifecycle implements LifecycleObserver {
public static final String TAG = “MapLifecycle”;
private MapView mapView;
public MapLifecycle(MapView mapView) {
this.mapView = mapView;
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
private void create(){
Log.d(TAG,“onCreate”);
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
private void start(){
Log.d(TAG,“onStart”);
mapView.onStart();
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
private void resume(){
Log.d(TAG,“onResume”);
mapView.onResume();
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
private void pause(){
Log.d(TAG,“onPause”);
mapView.onPause();
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
private void stop(){
Log.d(TAG,“onStop”);
mapView.onStop();
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
private void destroy(){
Log.d(TAG,“onDestroy”);
mapView.onDestroy();
}
}
然后再来看看BaseMapActivity。
//地图生命周期
private MapLifecycle mapLifecycle;
然后新写一个initView方法,初始化页面并且将MapLifecycle与BaseMapActivity绑定起来。
/**
- 页面初始化
*/
private void initView() {
//地图
mapView = findViewById(R.id.mapView);
//创建tencentMap地图对象
tencentMap = mapView.getMap();
mapLifecycle = new MapLifecycle(mapView);
//将观察者与被观察者绑定起来
getLifecycle().addObserver(mapLifecycle);
}
然后在onCreate中调用initView。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_map);
//页面初始化
initView();
}
下面你重新运行一下,然后在主页面点击基础地图进入,再点击返回键。
然后你看一下日志是否会打印。
很明显,这里打印了,说明可以通过这样一种方式将Activity的声明周期单独进行管理,从而减少它里面的一些业务逻辑的处理,最重要的是降低耦合。
刚才可以看到基础地图的确是显示出来了,而且可以通过手势放大、缩小、旋转。但是有一个最重要的问题,那就是显示的位置不对,我人在深圳,你显示在北京,这肯定不对呀。
所以还需要进一步配置才行,现在不配置则是显示默认的地址,因此是北京,当然其他的第三方SDK地图也是这样的,你不配置就是默认显示在北京。
下面来配置一下,既然是要定位到当前,那肯定少不了定位SDK了,那么在前面我已经详细的讲了定位SDK的使用,下面它只是给地图SDK进行辅助了,我就不详细讲了。
BaseMapActivity中增加
//定位管理
private TencentLocationManager locationManager;
//定位请求
private TencentLocationRequest locationRequest;
//定位数据源监听
private LocationSource.OnLocationChangedListener locationChangedListener;
然后实现LocationSource和TencentLocationListener。分别是定位数据源和定位监听。
然后重写下卖弄四个方法。
/**
- 接收定位结果
*/
@Override
public void onLocationChanged(TencentLocation tencentLocation, int code, String reason) {
}
/**
- 用于接收GPS、WiFi、Cell状态码
*/
@Override
public void onStatusUpdate(String name, int status, String desc) {
}
/**
-
启用
-
@param onLocationChangedListener 数据源更改监听
*/
@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
}
/**
- 停用
*/
@Override
public void deactivate() {
}
暂时不管它们,先进行定位的一些配置,新增方法initLocation
/**
- 初始化定位
*/
private void initLocation() {
//用于访问腾讯定位服务的类, 周期性向客户端提供位置更新
locationManager = TencentLocationManager.getInstance(this);
//设置坐标系
locationManager.setCoordinateType(TencentLocationManager.COORDINATE_TYPE_GCJ02);
//创建定位请求
locationRequest = TencentLocationRequest.create();
//设置定位周期(位置监听器回调周期)为3s
locationRequest.setInterval(3000);
//地图上设置定位数据源
tencentMap.setLocationSource(this);
//设置当前位置可见
tencentMap.setMyLocationEnabled(true);
}
在这个方法中,重点在于给地图设置定位数据源,这样地图才能在你当前定位数据源改变时定位过去,并且重新绘制地图。
下面就把注意力方法这四个重写的方法中,其中最重要的是onLocationChanged和activate这两个方法。下面先来看activate方法
/**
-
启用
-
@param onLocationChangedListener 数据源更改监听
*/
@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
locationChangedListener = onLocationChangedListener;
int error = locationManager.requestLocationUpdates(locationRequest, this, Looper.myLooper());
switch (error) {
case 1:
showMsg(“设备缺少使用腾讯定位服务需要的基本条件”);
break;
case 2:
showMsg(“AndroidManifest 中配置的 key 不正确”);
break;
case 3:
showMsg(“自动加载libtencentloc.so失败”);
break;
default:
break;
}
}
在这里进行了locationChangedListener 的实例化,并且启用了连续定位,获得一个发起连续定位的请求结果码,并根据这个结果码判断异常原因,这里的showMsg你应该不会陌生吧。
/**
-
Toast提示
-
@param msg
*/
private void showMsg(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
下面来看onLocationChanged方法。
/**
- 接收定位结果
*/
@Override
public void onLocationChanged(TencentLocation tencentLocation, int code, String reason) {
if (code == TencentLocation.ERROR_OK && locationChangedListener != null) {
//重新构建一个定位对象
Location location = new Location(tencentLocation.getProvider());
//设置经纬度以及精度
location.setLatitude(tencentLocation.getLatitude());
location.setLongitude(tencentLocation.getLongitude());
location.setAccuracy(tencentLocation.getAccuracy());
//更改位置定位,此时地图上就会显示当前定位到位置
locationChangedListener.onLocationChanged(location);
}
}
在上面的activate方法中发起了定位,在这里判断定位是否成功并且locationChangedListener 不为空,因为在上面已经实例化了没所以不会空,下面则通过返回的tencentLocation.getProvider()重新构建一个Location对象,并且设置经纬度和精度信息,之后将这个Location放到locationChangedListener.onLocationChanged中,更改数据源,而数据源一更改,地图自然就会跟着更改。此时,你的地图就会定位到你当前的位置。索德斯呢!当然还有一些事情没有办完,还剩下两个方法没有进行处理呢。
/**
- 用于接收GPS、WiFi、Cell状态码
*/
@Override
public void onStatusUpdate(String name, int status, String desc) {
//GPS, WiFi, Radio 等状态发生变化
Log.v(“State changed”, name + “===” + desc);
}
/**
- 停用
*/
@Override
public void deactivate() {
locationManager.removeUpdates(this);
locationManager = null;
locationRequest = null;
locationChangedListener = null;
}
OK,这两个方法我就不解释了,相信你能明白。现在所有的方法都写好了,但是你要调用initLocation才行啊。
OK,运行一下吧。
这个定位没有毛病,是我当前的位置。
② 地图类型
腾讯地图SDK可以根据不同的业务需要切换不同的地图类型,下面来看看吧。新建一个MapTypeActivity,布局为activity_map_type.xml,下面修改布局。
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“.map.MapTypeActivity”>
<RadioGroup
android:id=“@+id/rp_map_type”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“horizontal”>
<RadioButton
android:id=“@+id/rb_normal”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:buttonTint=“#1296db”
android:buttonTintMode=“src_atop”
android:checked=“true”
android:text=“普通图” />
<RadioButton
android:id=“@+id/rb_satellite”
android:buttonTint=“#1296db”
android:buttonTintMode=“src_atop”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“卫星图” />
<RadioButton
android:id=“@+id/rb_dark”
android:buttonTint=“#1296db”
android:buttonTintMode=“src_atop”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“暗色图” />
<RadioButton
android:id=“@+id/rb_traffic”
android:buttonTint=“#1296db”
android:buttonTintMode=“src_atop”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“路况” />
<RadioButton
android:visibility=“gone”
android:id=“@+id/rb_style_map”
android:buttonTint=“#1296db”
android:buttonTintMode=“src_atop”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“个性化” />
<com.tencent.tencentmap.mapsdk.maps.MapView
android:layout_below=“@+id/rp_map_type”
android:id=“@+id/mapView”
android:layout_width=“match_parent”
android:layout_height=“match_parent” />
进入MapTypeActivity,实现RadioGroup.OnCheckedChangeListener。
private MapView mapView;
private RadioGroup radioGroup;
private TencentMap tencentMap;
//地图生命周期
private MapLifecycle mapLifecycle;
onCreate中,进行初始化控件,并且绑定生命周期,MapLifecycle可以与多个Activity进行绑定。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_type);
mapView = findViewById(R.id.mapView);
radioGroup = findViewById(R.id.rp_map_type);
radioGroup.setOnCheckedChangeListener(this);
tencentMap = mapView.getMap();
mapLifecycle = new MapLifecycle(mapView);
getLifecycle().addObserver(mapLifecycle);
}
最后重写onCheckedChanged方法,对不同的RadioButton进行处理。
/**
-
类型选中监听
-
@param group
-
@param checkedId
*/
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb_normal: //普通地图-默认地图类型
tencentMap.setTrafficEnabled(false);
tencentMap.setMapType(TencentMap.MAP_TYPE_NORMAL);
break;
case R.id.rb_satellite: //卫星地图
tencentMap.setTrafficEnabled(false);
tencentMap.setMapType(TencentMap.MAP_TYPE_SATELLITE);
break;
case R.id.rb_dark: //暗色地图
tencentMap.setTrafficEnabled(false);
tencentMap.setMapType(TencentMap.MAP_TYPE_DARK);
break;
case R.id.rb_traffic://路况图
tencentMap.setTrafficEnabled(true);
break;
default:
break;
}
}
地图类型的切换就写好了,比较的简单。然后在activity_main.xml中增加一个按钮,用于点击进入MapTypeActivity。
<Button
android:id=“@+id/btn_map_type”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“地图类型” />
然后在MainActivity中。
//地图类型
private Button btnMapType;
initView中
onClick中
case R.id.btn_map_type:
//地图类型
startActivity(new Intent(this, MapTypeActivity.class));
break;
下面运行一下看看效果:
实际上,这个卫星图的效果是不对的,不过我已经和腾讯的技术服务人员反映了,等待结果,其他的都正常。
③ 个性化地图
个性化地图需要先选择地图的样式,点击样式选择
注意,右上角有皇冠的是企业客户级别专享使用,需要完成企业认证才行。
这里我选择翠烟样式,然后点击直接使用。
选择要使用该样式的AppKey,然后设置应用类型,最后提交。
设置成功会给你一个弹窗的提示。
点击确定之后,进入到我的样式里面,然后可以看到刚才选择的样式。
在项目中,创建一个新的PersonalizedMapActivity布局为activity_personalized_map.xml。布局代码如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“.map.PersonalizedMapActivity”>
<com.tencent.tencentmap.mapsdk.maps.MapView
android:id=“@+id/mapView”
android:layout_width=“match_parent”
android:layout_height=“match_parent” />
然后在进入PersonalizedMapActivity。
private MapView mapView;
private TencentMap tencentMap;
private MapLifecycle mapLifecycle;
onCreate中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_personalized_map);
mapView = findViewById(R.id.mapView);
tencentMap = mapView.getMap();
mapLifecycle = new MapLifecycle(mapView);
getLifecycle().addObserver(mapLifecycle);
}
然后同样要在activity_main.xml中增加一个按钮。
<Button
android:id=“@+id/btn_personalized_map”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“个性化地图” />
别忘了在MainActivity中增加相应的绑定id和增加点击,跳转代码:
//个性化地图
startActivity(new Intent(this, PersonalizedMapActivity.class));
运行一下
嗯,样式的确是改变了,而此时你返回到主页面,然后你点击基础地图或者地图类型按钮进入相应的页面你会发现一个问题,那就是都变成了这个个性化的样式了,不设置了就是默认的经典样式,而设置之后就是使用设置的样式。而一个Key可以关联三个样式,也就是说我还可以添加两个样式。
这里我添加两个新的样式,然后提交。
这里这里的样式序号,1是烟翠、2是经典、3是墨渊
下面进入BaseMapActivity,在initView中将地图样式设置为经典样式。
同样也在MapTypeActivity的initView这样设置。
之后再来运行一下:
可以看到这样改了之后就符合我之前的想法了,那么个性化地图只有一个样式明显不合适,下面来简单的切换一下吧,先修改activity_personalized_map.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“.map.PersonalizedMapActivity”>
<RadioGroup
android:id=“@+id/rp_style”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“horizontal”>
<RadioButton
android:id=“@+id/rb_normal”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:buttonTint=“#1296db”
android:buttonTintMode=“src_atop”
android:text=“经典” />
<RadioButton
android:id=“@+id/rb_style_one”
android:buttonTint=“#1296db”
android:checked=“true”
android:buttonTintMode=“src_atop”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“烟翠” />
<RadioButton
android:id=“@+id/rb_style_two”
android:buttonTint=“#1296db”
android:buttonTintMode=“src_atop”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“墨渊” />
<com.tencent.tencentmap.mapsdk.maps.MapView
android:layout_below=“@+id/rp_style”
android:id=“@+id/mapView”
android:layout_width=“match_parent”
android:layout_height=“match_parent” />
修改PersonalizedMapActivity
private RadioGroup radioGroup;
继承RadioGroup.OnCheckedChangeListener
onCreate方法中增加
radioGroup = findViewById(R.id.rp_style);
radioGroup.setOnCheckedChangeListener(this);
然后重写onCheckedChanged方法
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb_normal://经典
tencentMap.setMapStyle(2);
break;
case R.id.rb_style_one://烟翠
tencentMap.setMapStyle(1);
break;
case R.id.rb_style_two://墨渊
tencentMap.setMapStyle(3);
break;
default:
break;
}
}
下面重新运行
OK,个性化地图就到这里了。
④ 地图覆盖物
下面还是单独新建一个MarkerActivity,布局为activity_marker.xml来演示这个覆盖物的内容,
1. 绘制点标记
点标记,是在地图上用来标记一个经纬度坐标的覆盖物。
右键点击res→ New → Android Resource File
然后在drawable下新建一个ic_add.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?><vector xmlns:android=“http://schemas.android.com/apk/res/android”
android:width=“24dp”
android:height=“24dp”
android:tint=“#FFFFFF”
android:viewportWidth=“24.0”
android:viewportHeight=“24.0”>
<path
android:fillColor=“@android:color/white”
android:pathData=“M18,13h-5v5c0,0.55 -0.45,1 -1,1s-1,-0.45 -1,-1v-5H6c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1h5V6c0,-0.55 0.45,-1 1,-1s1,0.45 1,1v5h5c0.55,0 1,0.45 1,1s-0.45,1 -1,1z” />
这是一个图标。
下面来看marker_menu.xml
<?xml version="1.0" encoding="utf-8"?>xmlns:app=“http://schemas.android.com/apk/res-auto”>
<item
android:icon=“@drawable/ic_add”
android:title=“添加”
app:showAsAction=“always”>
<item
android:id=“@+id/menu_add_default_marker”
android:title=“添加默认标注”
app:showAsAction=“always” />
<item
android:id=“@+id/menu_remove_marker”
android:title=“移除标注”
app:showAsAction=“always” />
当点击这个菜单时会出现两个选项,添加默认标注和移除标注,下面先在activity_marker.xml中放置一个地图。
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout 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”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“.map.MarkerActivity”>
<com.tencent.tencentmap.mapsdk.maps.MapView
android:id=“@+id/mapView”
android:layout_width=“match_parent”
android:layout_height=“match_parent”/>
</androidx.constraintlayout.widget.ConstraintLayout>
然后进入MarkerActivity。
private MapView mapView;
private TencentMap tencentMap;
private MapLifecycle mapLifecycle;
//默认标注
private Marker defaultMarker;
onCreate中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_marker);
mapView = findViewById(R.id.mapView);
tencentMap = mapView.getMap();
mapLifecycle = new MapLifecycle(mapView);
getLifecycle().addObserver(mapLifecycle);
}
然后设置菜单
/**
-
创建选项菜单
-
@param menu
-
@return
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.marker_menu, menu);
return super.onCreateOptionsMenu(menu);
}
菜单的选中
/**
-
菜单选择
-
@param item
-
@return
*/
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add_default_marker:
//添加默认标注点
addDefaultMarker();
break;
case R.id.menu_remove_marker:
//移除标注
removeMarker();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
添加默认标注点
/**
- 添加默认标注
*/
private void addDefaultMarker() {
LatLng latLng = new LatLng(22.540893, 113.949082);
defaultMarker = tencentMap.addMarker(new MarkerOptions(latLng));
//移动地图
tencentMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
移除标注
/**
- 移除标注
*/
private void removeMarker() {
if (defaultMarker != null) {
defaultMarker.remove();
defaultMarker = null;
}
}
然后修改一下Activity的顶部栏文字,打开AndroidManifest.xml。
每个Activity我都修改了它的label值
下面运行
可以看到默认的标注点也是灰不溜秋的。下面来使用自定义的标注看看。
看这个棒棒糖。放到drawable下,命名为marker.png,然后打开marker_menu.xml,增加一个item。
<item
android:id=“@+id/menu_add_custom_marker”
android:title=“添加自定义标注”
app:showAsAction=“always” />
然后回到MarkerActivity中
//自定义标注
private Marker customMarker;
在onOptionsItemSelected方法,添加一个case
case R.id.menu_add_custom_marker:
//添加自定义标注
addCustomMarker();
break;
添加自定义标注
/**
- 添加自定义标注
*/
private void addCustomMarker() {
//创建Marker对象之前,设置属性
LatLng latLng = new LatLng(40.011313,116.391907);
BitmapDescriptor custom = BitmapDescriptorFactory.fromResource(R.drawable.marker);
customMarker = tencentMap.addMarker(new MarkerOptions(latLng)
//设置图标
.icon(custom)
//设置图标透明度
.alpha(0.8f));
//移动地图
tencentMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
在removeMarker中
if (customMarker != null) {
customMarker.remove();
customMarker = null;
}
下面运行
2. 绘制信息窗口
信息窗口,是依附于Marker之上的展现元素,用于对Marker进行详细描述,腾讯地图SDK默认提供的InfoWindow分标题和简述两部分,开发者也可以自定义InfoWindow,满足个性化场景的要求。
在marker_menu.xml中增加
<item
android:id=“@+id/menu_add_default_info_window”
android:title=“添加默认信息窗口”
app:showAsAction=“always” />
MarkerActivity中
//自定义标注 显示默认信息窗口
private Marker customMarkerDefaultInfoWindow;
onOptionsItemSelected中增加case
case R.id.menu_add_default_info_window:
//添加默认信息窗口
addDefaultInfoWindow();
break;
添加默认信息窗口
/**
- 添加默认信息窗口
*/
private void addDefaultInfoWindow(){
//通过MarkerOptions配置
LatLng latLng = new LatLng(39.908710,116.397499);
MarkerOptions options = new MarkerOptions(latLng);
options.title(“天安门”)//标注的InfoWindow的标题
.snippet(“地址: 北京市东城区东长安街”)//标注的InfoWindow的内容
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));//设置自定义Marker图标
customMarkerDefaultInfoWindow = tencentMap.addMarker(options);
//开启信息窗口
customMarkerDefaultInfoWindow.setInfoWindowEnable(true);
//移动地图
tencentMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
removeMarker中
if (customMarkerDefaultInfoWindow != null) {
customMarkerDefaultInfoWindow.remove();
customMarkerDefaultInfoWindow = null;
}
运行
可以看到,当我点击标注时,会弹出一个小信息窗口,再点击则会关闭窗口。
当然这个窗口也是可以自定义的,下面来演示一下,首先在marker_menu.xml中添加item
<item
android:id=“@+id/menu_add_custom_info_window”
android:title=“添加自定义信息窗口”
app:showAsAction=“always” />
然后在layout下新建一个custom_infowindow.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:orientation=“vertical”
android:layout_width=“match_parent”
android:background=“@color/colorPrimary”
android:layout_height=“match_parent”>
<TextView
android:padding=“12dp”
android:id=“@+id/tv_content”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“center_horizontal”
android:textColor=“#ffffff”
android:textSize=“14sp”/>
下面进入MarkerActivity中
//自定义标注 显示自定义信息窗口
private Marker customMarkerCustomInfoWindow;
在onOptionsItemSelected中增加case
case R.id.menu_add_custom_info_window:
//添加自定义信息窗口
addCustomInfoWindow();
break;
添加自定义信息窗口,以及窗口的点击事件
/**
- 添加自定义信息窗口
*/
private void addCustomInfoWindow() {
//上海虹桥机场经纬度
LatLng latLng = new LatLng(31.19590, 121.34113);
MarkerOptions options = new MarkerOptions(latLng);
options.title(“上海市”)//标注的InfoWindow的标题
.snippet(“地址: 虹桥机场”)//标注的InfoWindow的内容
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
customMarkerCustomInfoWindow = tencentMap.addMarker(options);
//移动地图
tencentMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//设置自定义信息窗口
tencentMap.setInfoWindowAdapter(new TencentMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return createCustomInfoView(marker);
}
@Override
public View getInfoContents(Marker marker) {
return createCustomInfoView(marker);
}
private View createCustomInfoView(Marker marker) {
LinearLayout mCustomInfoWindowView = (LinearLayout) View.inflate(
getApplicationContext(), R.layout.custom_infowindow, null);
TextView tvContent = mCustomInfoWindowView.findViewById(R.id.tv_content);
//设置自定义信息窗的内容
tvContent.setText(“自定义信息窗口:”);
tvContent.append(“\n” + marker.getTitle());
tvContent.append(“\n” + marker.getSnippet());
return mCustomInfoWindowView;
}
});
//窗口点击事件
tencentMap.setOnInfoWindowClickListener(new TencentMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Toast.makeText(getApplicationContext(), “信息窗被点击\n” + marker.getTitle(), Toast.LENGTH_SHORT).show();
}
@Override
public void onInfoWindowClickLocation(int i, int i1, int i2, int i3) {
Toast.makeText(getApplicationContext(),
“尺寸:” + i + “x” + i1 + " 位置:" + i2 + “,” + i3, Toast.LENGTH_SHORT).show();
}
});
}
removeMarker中
//移除自定义标注 显示自定义信息窗口
if (customMarkerCustomInfoWindow != null) {
customMarkerCustomInfoWindow.remove();
customMarkerCustomInfoWindow = null;
}
运行一下:
OK,运行效果很明显了。
⑤ 地图设置
下面来说明一些地图设置
1. 设置地图缩放等级
腾讯地图的文档在这方面的讲述还是不够详细。地图的设置,就用BaseMapActivity来演示。在initView中
//设置最大缩放等级 最大值 20 最小值 1
tencentMap.setMaxZoomLevel(20);
//设置最小缩放等级 最大值 20 最小值 1
tencentMap.setMinZoomLevel(20);
这里设置最大和最小的缩放等级一致,此时的地图再进行放大和缩小的,运行一下,看看是什么样子。
20已经是最大的缩放了,最小是1
下面来改变一下最小缩放等级为12
tencentMap.setMinZoomLevel(12);
运行一下
你会发现这和你不设置最大和最小时的缩放是一样说的,这说明了12就是默认的缩放等级,而这个地图在设置了最小缩放等级之后以最小的为准,下面改成16看看
tencentMap.setMinZoomLevel(16);
从这个图就可以验证我上面说的。
地图的缩放就说到这里了,更多的自行尝试。
2. 设置3D地图
之前我下载的是3D的地图SDK,下面就来设置3D效果。
//设置最小缩放等级 最大值 20 最小值 1
tencentMap.setMinZoomLevel(18);
//启用3D视图
tencentMap.setBuilding3dEffectEnable(true);
运行一下:
设置为最小缩放为18看到的3D效果会比较好。
3. 地图logo设置
在使用地图 SDK 时,按照腾讯地图开放API服务协议要求始终保持logo 是可见的,不允许对腾讯地图的 logo 进行遮盖、修改等弱化地图品牌的行为。虽然无法隐藏但是可以放大和缩小,比例范围(0.7~1.3)。
//获取地图UI设置对象
UiSettings uiSettings = tencentMap.getUiSettings();
//设置logo的大小 比例范围(0.7~1.3)
uiSettings.setLogoScale(0.7f);
通过tencentMap.getUiSettings()获取UiSettings 对象实例,然后设置logg的缩放,先看看最小的。
然后看看最大的
uiSettings.setLogoScale(1.3f);
最好自己设置看一下实际的效果。
设置地图logo的显示位置。
Android核心知识点
面试成功其实是必然的,因为我做足了充分的准备工作,包括刷题啊,看一些Android核心的知识点,看一些面试的博客吸取大家面试的一些经验。
下面这份PDF是我翻阅了差不多3个月左右一些Android大博主的博客从他们那里取其精华去其糟泊所整理出来的一些Android的核心知识点,全部都是精华中的精华,我能面试到现在2-2资深开发人员跟我整理的这本Android核心知识点有密不可分的关系,在这里本着共赢的心态分享给各位朋友。
不管是Android基础还是Java基础以及常见的数据结构,这些是无原则地必须要熟练掌握的,尤其是非计算机专业的同学,面试官一上来肯定是问你基础,要是基础表现不好很容易被扣上基础不扎实的帽子,常见的就那些,只要你平时认真思考过基本上面试是没太大问题的。
最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上我搜集整理的2019-2021BAT 面试真题解析,我把大厂面试中常被问到的技术点整理成了PDF,包知识脉络 + 诸多细节。
节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
i2 + “,” + i3, Toast.LENGTH_SHORT).show();
}
});
}
removeMarker中
//移除自定义标注 显示自定义信息窗口
if (customMarkerCustomInfoWindow != null) {
customMarkerCustomInfoWindow.remove();
customMarkerCustomInfoWindow = null;
}
运行一下:
OK,运行效果很明显了。
⑤ 地图设置
下面来说明一些地图设置
1. 设置地图缩放等级
腾讯地图的文档在这方面的讲述还是不够详细。地图的设置,就用BaseMapActivity来演示。在initView中
//设置最大缩放等级 最大值 20 最小值 1
tencentMap.setMaxZoomLevel(20);
//设置最小缩放等级 最大值 20 最小值 1
tencentMap.setMinZoomLevel(20);
这里设置最大和最小的缩放等级一致,此时的地图再进行放大和缩小的,运行一下,看看是什么样子。
20已经是最大的缩放了,最小是1
下面来改变一下最小缩放等级为12
tencentMap.setMinZoomLevel(12);
运行一下
你会发现这和你不设置最大和最小时的缩放是一样说的,这说明了12就是默认的缩放等级,而这个地图在设置了最小缩放等级之后以最小的为准,下面改成16看看
tencentMap.setMinZoomLevel(16);
从这个图就可以验证我上面说的。
地图的缩放就说到这里了,更多的自行尝试。
2. 设置3D地图
之前我下载的是3D的地图SDK,下面就来设置3D效果。
//设置最小缩放等级 最大值 20 最小值 1
tencentMap.setMinZoomLevel(18);
//启用3D视图
tencentMap.setBuilding3dEffectEnable(true);
运行一下:
设置为最小缩放为18看到的3D效果会比较好。
3. 地图logo设置
在使用地图 SDK 时,按照腾讯地图开放API服务协议要求始终保持logo 是可见的,不允许对腾讯地图的 logo 进行遮盖、修改等弱化地图品牌的行为。虽然无法隐藏但是可以放大和缩小,比例范围(0.7~1.3)。
//获取地图UI设置对象
UiSettings uiSettings = tencentMap.getUiSettings();
//设置logo的大小 比例范围(0.7~1.3)
uiSettings.setLogoScale(0.7f);
通过tencentMap.getUiSettings()获取UiSettings 对象实例,然后设置logg的缩放,先看看最小的。
然后看看最大的
uiSettings.setLogoScale(1.3f);
最好自己设置看一下实际的效果。
设置地图logo的显示位置。
Android核心知识点
面试成功其实是必然的,因为我做足了充分的准备工作,包括刷题啊,看一些Android核心的知识点,看一些面试的博客吸取大家面试的一些经验。
下面这份PDF是我翻阅了差不多3个月左右一些Android大博主的博客从他们那里取其精华去其糟泊所整理出来的一些Android的核心知识点,全部都是精华中的精华,我能面试到现在2-2资深开发人员跟我整理的这本Android核心知识点有密不可分的关系,在这里本着共赢的心态分享给各位朋友。
[外链图片转存中…(img-jQ4h2PF7-1715586024810)]
不管是Android基础还是Java基础以及常见的数据结构,这些是无原则地必须要熟练掌握的,尤其是非计算机专业的同学,面试官一上来肯定是问你基础,要是基础表现不好很容易被扣上基础不扎实的帽子,常见的就那些,只要你平时认真思考过基本上面试是没太大问题的。
最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上我搜集整理的2019-2021BAT 面试真题解析,我把大厂面试中常被问到的技术点整理成了PDF,包知识脉络 + 诸多细节。
节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!