android高德地图网络路径实现自定义marker并点击弹出自定义窗口
android中使用地图的地方随处可见,今天记录一下网络路径生成自定义marker,点击标记弹出自定义的窗口(在这里使用的是高德地图)
在这里我们使用Grilde去加载网络图片,因为这个简直太方便了!
在android studio下,在app/build.gradle文件当中添加如下依赖:
compile 'com.github.bumptech.glide:glide:3.7.0'
接下来就是代码的实现部分
代码注释的比较详细
/**
* Created by Yyyyq on 2018/4/20 0009.
* 根据网络路径生成自定义marker,点击弹出自定义窗体
*/
public class CustomMapActivity extends AppCompatActivity implements AMap.OnMarkerClickListener,
AMap.InfoWindowAdapter,AMap.OnMapClickListener{
//标题头
private TextView textView;
//返回按钮
private ImageView back;
private MapView mMapView;
private AMap aMap;
Marker marker;
//指向当前的marker(用于控制infowindow显示与消失)
Marker nowMarker;
SimpleVO tempSimpleVO;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custommap);
mMapView = (MapView) findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
init();
back=(ImageView)findViewById(R.id.back);
textView = (TextView) findViewById(R.id.toolbar_title);
Toolbar toolbar = (Toolbar) findViewById(R.id.activity_main_toolbar);
textView.setText("生成网络图片Marker");
toolbar.setTitle("");
setSupportActionBar(toolbar);
setTranslucentBar();
//返回按钮
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
private void init() {
if (aMap == null) {
aMap = mMapView.getMap();
setUpMap();
//获取后台数据源
getDataSource();
}
}
/**
* 设置aMap属性
*/
private void setUpMap() {
aMap.setOnMarkerClickListener(this);// 设置点击marker事件监听器
aMap.setInfoWindowAdapter(this);//自定义弹出窗体
aMap.setOnMapClickListener(this);//地图点击事件
}
/**
* @Description:沉浸式标题
*/
protected void setTranslucentBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = getWindow();
// Translucent status bar
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
/**
*在这里模拟方法请求到数据源,可根据实际
* 我们用自定义实体类SimpleVO接收数据源
*/
private void getDataSource(){
//网络请求 得到List
List<SimpleVO> list="后台传递的数据源";
if(list.size()>0){
//根据第一条数据经纬度经地图移动到所视区域
LatLng curLatlng = new LatLng(Double.parseDouble(list.get(0).getLatitude()),Double.parseDouble(list.get(0).getLongitude()));
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(curLatlng, 14f));
for(int i=0;i<list.size();i++){
final int j = i;
//利用强大的Glide加载图片,可放占位符等,可百度Glide属性
Glide.with(CustomMapActivity.this)
.load(list.get(i).getUrl())
.into(new SimpleTarget<GlideDrawable>(){
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
//展示图片
View view = LayoutInflater.from(CustomMapActivity.this).inflate(
R.layout.map_userimg, null);
RoundImageView imageView = (RoundImageView) view.findViewById(R.id.user_img);
tempSimpleVO=list.get(j);
imageView.setImageDrawable(resource);
Bitmap bitmap = convertViewToBitmap(view);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.anchor(1.5f, 3.5f);
markerOptions.position(new LatLng(Double.parseDouble(tempSimpleVO.getLatitude()),Double.parseDouble(tempSimpleVO.getLongitude())));
markerOptions.draggable(false);
markerOptions.title(tempSimpleVO.getName());
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(bitmap));
marker = aMap.addMarker(markerOptions);
//把相应的对象赋给marker,adapter中通过这个对象给控件赋值
marker.setObject(tempSimpleVO);
}
});
}
}
}
/**
* 点击marker弹出窗口
* 返回true 地图不移动中心点
*/
@Override
public boolean onMarkerClick(Marker marker) {
nowMarker=marker;
nowMarker.showInfoWindow();
return true;
}
/**
* 自定义窗口布局
*/
@Override
public View getInfoWindow(Marker marker) {
View infoContent = LayoutInflater.from(CustomMapActivity.this).inflate(
R.layout.item_map_window, null);
render(marker, infoContent,2);
return infoContent;
}
/**
* 对窗口信息赋值
*/
public void render(Marker marker, View view,int mark) {
LinearLayout layout = (LinearLayout) view.findViewById(R.id.window_linearlayout);
//设置透明度
layout.getBackground().setAlpha(240);
TextView name = (TextView) view.findViewById(R.id.window_name);
TextView info = (TextView) view.findViewById(R.id.window_info);
SimpleVO simpleVO = (SimpleVO) marker.getObject();
name.setText(simpleVO.getName());
info.setText(simpleVO.getInfo());
}
/**
* 因自定义窗口 返回null
*/
@Override
public View getInfoContents(Marker marker) {
return null;
}
/**
* 重写地图点击事件,点击地图任意点隐藏infowindow窗口
*/
@Override
public void onMapClick(LatLng latLng) {
//隐藏infowindow窗口
nowMarker.hideInfoWindow();
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
/**
* view转bitmap
*/
private static Bitmap convertViewToBitmap(View view){
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}
}
接下来就是主页面的布局 activity_custommap
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--标题栏-->
<android.support.v7.widget.Toolbar
android:id="@+id/activity_main_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:paddingTop="16dp"
android:minHeight="?attr/actionBarSize"
android:background="@color/bluestyle_button">
<ImageView
android:id="@+id/back"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginLeft="10dp"
android:layout_gravity="center|left"
android:src="@drawable/jiantouzuo"/>
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#fff"
android:textSize="18sp"/>
</android.support.v7.widget.Toolbar>
<com.amap.api.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
代码中应用的实力类 SimpleVO
public class SimpleVO {
private String id;
private String name;
private String latitude;
private String longitude;
private String url;
private String info;
//省略get,set,toString方法,需自己导入
}
之后需要展示我们的自定义头像 map_userimg 因为展示的是头像,我们用到了圆形工具类,若没有该工具类可查看复制上一篇随笔,可直接粘贴复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<!--自定义圆形工具类-->
<!--若没有该工具类可赋值上一篇代码,Ctrl+c Ctrl+v可用-->
<com.bc.yyyyq.util.RoundImageView
android:id="@+id/user_img"
android:scaleType="fitCenter"
android:layout_width="30dp"
android:layout_height="30dp"/>
</LinearLayout>
最后我们需要展示我们自定义的marker的弹出窗体 item_map_window
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/window_linearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/brokermap_layout"
android:padding="10dp"
android:orientation="vertical">
<!--姓名-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="姓名:"
android:textColor="#515151"
android:textSize="14sp"/>
<TextView
android:id="@+id/window_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="center|left"
android:text=""
android:textColor="#515151"
android:textSize="14sp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#f2f2f2" />
<!--个人信息-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="个人信息:"
android:gravity="center"
android:textColor="#515151"
android:textSize="14sp"/>
<TextView
android:id="@+id/window_info"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|left"
android:layout_marginLeft="5dp"
android:text=""
android:textColor="#515151"
android:textSize="14sp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#f2f2f2" />
</LinearLayout>
就这样就完成了自定义网络图片的marker和点击标识弹出我们自定义的窗口,想要的效果就展示出来了
android高德地图网络路径实现自定义marker并点击弹出自定义窗口的更多相关文章
- Android高德地图自定义Markers的例子
下文为各位重点介绍关于Android高德地图自定义Markers的例子,希望这篇文章能够让各位理解到Android高德地图自定义Markers的方法. 之前的博客里说了地图的嵌入和定位,今天就说说在地 ...
- android 高德地图出现【定位失败key鉴权失败】
如题:android 高德地图出现[定位失败key鉴权失败] 原因:使用的是debug模式下的SHA1,发布的版本正确获取SHA1的方式见: 方法二使用 keytool(jdk自带工具),按照如下步骤 ...
- android 高德地图API 之 java.lang.UnsatisfiedLinkError: Couldn't load amapv3: findLibrary returned null错误
错误场景: 运行android app时,在运行到调用高德地图API时,出现 “java.lang.UnsatisfiedLinkError: Couldn't load amapv3: findLi ...
- 实现android手机来电拦截系统页面弹出自定义页面特效
如何实现android手机来电拦截系统页面弹出自定义页面特效, 首先: 我们需要注册一个监听来电的广播PhoneStateReceiver 类:其次: 在onReceive里面我们获取an ...
- Android高德地图开发具体解释
这段时间开发的时候用到了高德地图,对高德地图开发有心得体会,如今分享给大家.对我开发过百度地图的我来说,整体来说高德地图Demo,没有百度解说的具体 个人更偏向于使用百度地图,可是没办发,项目须要使用 ...
- Android 高德地图使用小记
感谢大佬:https://www.cnblogs.com/devilmaycry812839668/p/8727569.html 高德地图 Android编程中 如何设置使 标记 marker 能够被 ...
- 实例源码--Android高德地图实例源码
下载源码 技术要点: 1.高德地图 API的使用 2.定位 ,查询路线,公交查询等地图相关技术 3.源码带有非常详 细的中文注释 ...... 详细介绍: 1. 高德地图API的使用 本套实例采 ...
- Android 高德地图 java.lang.UnsatisfiedlinkError Native method not found: com.autonavi.amap.mapcore.MapCore.nativeNewInstance:(Ljava/lang/String;)
在Android项目中引用高德地图,程序运行时出现上述问题,如果引用了Map3D的jar包,则需要在引入Jar文件的同时引入so文件,在高德地图的demo中,找到so文件: 然后将其复制到jniLib ...
- Android 高德地图No implementation found for long com.autonavi.amap.mapcore.MapCore
此篇博客最后更新时间写自2016.5.18.当下高德地图jar版本为3.3.1. 使用高德地图碰到此问题,纠结许久(接近4个多小时). 记录在此,希望遇到相同问题的读者可以有所借鉴. 错误截图: 导致 ...
随机推荐
- python之celery的使用(一)
前段时间需要使用rabbitmq做写缓存,一直使用pika+rabbitmq的组合,pika这个模块虽然可以很直观地操作rabbitmq,但是官方给的例子太简单,对其底层原理了解又不是很深,遇到很多坑 ...
- 敏捷项目需求拆解&发现用户故事
需求文档和敏捷中的Epic,User Story, Task之间是什么关系以及如何将需求文档转换成敏捷方式的描述,指导开发人员. 一直是很多公司团队比较困扰的问题,那么最近笔者为了解决这些问题,上了一 ...
- kafka---broker 保存消息
1 .存储方式 物理上把 topic 分成一个或多个 patition(对应 server.properties 中的 num.partitions=3 配置),每个 patition 物理上对应一个 ...
- DevExpress 控件中GridControl的使用
近期开发用到了DevExpress系列的控件,GridControl是我用到的Dev系列控件最多的一个控件.现在先来总结一下: 首先先写一个简单的小例子来简单介绍一下GridControl的用法: 1 ...
- spring7——AOP之通知和顾问
通知和顾问都是切面的实现形式,其中通知可以完成对目标对象方法简单的织入功能. 而顾问包装了通知,可以让我们对通知实现更加精细化的管理,让我们可以指定具体的切入点. 通知分为前置通知,环绕通知及后置通知 ...
- mysql基础练习题
一.表关系 请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 /* Navicat MySQL Data Transfer Source Server : mysql5.7.1 Sour ...
- POJ-3723 Conscription---最大权森林---最小生成树
题目链接: https://vjudge.net/problem/POJ-3723 题目大意: 需要征募女兵N人, 男兵M人. 每征募一个人需要花费10000美元. 带式如果已经征募的人中有一些关系亲 ...
- 数据库性能优化(database tuning)性能优化绝不仅仅只是索引
一毕业就接触优化方面的问题,专业做优化也有至少5年之多的时间了,可现在还是经常听到很多人认为优化很简单,就是建索引的问题,这确实不能怪大家,做这行20多年的时间里,在职业生涯的每个阶段,几乎都能听到这 ...
- C# QQ邮箱授权码发送邮件
using System.Net;using System.Web.Mail; public class SendMail { /// <summary> /// 发送Email /// ...
- [LeetCode] Coin Change 2 硬币找零之二
You are given coins of different denominations and a total amount of money. Write a function to comp ...