Android高德地图marker自定义弹框窗口
作者:浪克oo 发布时间:2023-06-22 13:07:05
标签:Android,地图,窗口
本文实例为大家分享了Android高德地图marker自定义弹框窗口的具体代码,供大家参考,具体内容如下
最终效果:
1.gradle里添加高德地图依赖
implementation 'com.amap.api:map2d:latest.integration'//2d地图功能
implementation 'com.amap.api:location:latest.integration'//定位功能
2.如果要用到定位的话,就首先到高德控制台里面加入本应用的信息获取到key,再在Application里设置key,并在AndroidManifest.xml中应用MainApp
public class MainApp extends android.app.Application {
@Override
public void onCreate() {
super.onCreate();
//高德地图注册
AMapLocationClient.setApiKey("0f1d26a891783cc4d632965a7cc08443");
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hk.testapplication">
<uses-permission android:name="android.permission.INTERNET" /> <!-- 访问网络权限 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 用于访问GPS定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<application
android:name=".MainApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TestApplication">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3. 创建activity_main.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=".MainActivity">
<com.amap.api.maps2d.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.amap.api.maps2d.MapView>
</androidx.constraintlayout.widget.ConstraintLayout>
4. MainActivity里加载地图,添加marker
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMapView = findViewById(R.id.mapview);
mMapView.onCreate(savedInstanceState);// 此方法必须重写
mMap = mMapView.getMap();
initPoint(30.665534,104.070929); //地图中心点位
initMarker();//测试点位
}
/**
* 绘制marker
*/
private void initMarker() {
mMarkers = new ArrayList<>();
//绘制marker 实际使用时会循环创建marker并填入数据
Marker marker = mMap.addMarker(new MarkerOptions()
.anchor(0.5f, 0.5f)
.position(new LatLng(30.665534,104.070929))
.title("标题数据")
.snippet("消息数据")
.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(getResources(), R.mipmap.ic_launcher_round))));//点位图标
mMarkers.add(marker);
}
/**
* 加载地图中心点
*/
private void initPoint(double latitude, double Longitude) {
LatLng marker1 = new LatLng(latitude, Longitude);
mMap.moveCamera(CameraUpdateFactory.changeLatLng(marker1));
mMap.moveCamera(CameraUpdateFactory.zoomTo(12));
}
@Override
public void onResume() {
super.onResume();
if (mMapView != null)
mMapView.onResume(); //管理地图的生命周期
}
@Override
public void onPause() {
super.onPause();
if (mMapView != null)
mMapView.onPause(); //管理地图的生命周期
}
@Override
public void onDestroy() {
super.onDestroy();
if (mMapView != null)
mMapView.onDestroy(); //管理地图的生命周期
}
}
5.添加弹框自定义布局view_map_infowindow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:minHeight="50dp"
android:minWidth="100dp"
android:background="#ffff"
android:gravity="center"
>
<ImageView
android:id="@+id/iv_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="@+id/tv_msg"
android:text="自定义布局"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/iv_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
记得设置布局最小高度和宽度,不然窗口会默认宽度高度,会使布局显示不完整
6.添加自定义弹框窗口adapter
/**
*自定义地图弹框adapter
* @author hk
*/
public class MapInfoWinAdapter implements AMap.InfoWindowAdapter, View.OnClickListener {
private Context mContext;
private LatLng latLng;
private TextView mTvMsg;
private ImageView mIvLeft,mIvRight;
private String mSnippet,mTitle;
@Override
public View getInfoWindow(Marker marker) {
initData(marker);
View view = initView();
return view;
}
@Override
public View getInfoContents(Marker marker) {
return null; //因为是自定义的布局,返回null
}
public MapInfoWinAdapter(Context context) {
mContext = context;
}
private void initData(Marker marker) {
//当前点位经纬度
latLng = marker.getPosition();
//当前点位带的消息信息 也可通过这个传输数据把数据转成json
mSnippet = marker.getSnippet();
//当前点位带的标题信息
mTitle = marker.getTitle();
}
@NonNull
private View initView() {
//获取自定义的布局
View view = LayoutInflater.from(mContext).inflate(R.layout.view_map_infowindow, null);
mTvMsg = (TextView) view.findViewById(R.id.tv_msg);
mIvLeft= (ImageView) view.findViewById(R.id.iv_left);
mIvRight= (ImageView) view.findViewById(R.id.iv_right);
mTvMsg.setText("我是自定义布局弹框");
mIvLeft.setOnClickListener(this);
mIvRight.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.iv_left:
Toast.makeText(mContext,"我是左边按钮点击事件",Toast.LENGTH_SHORT).show();
break;
case R.id.iv_right:
Toast.makeText(mContext,"我是右边按钮点击事件",Toast.LENGTH_SHORT).show();
break;
}
}
}
7.地图绑定adapter
//重要 创建自定义适配器
MapInfoWinAdapter adapter = new MapInfoWinAdapter(this);
mMap.setInfoWindowAdapter(adapter);//设置自定义窗口adapter
现在点击marker就会弹出我们自定义的布局了
8.点击地图或弹框关闭弹框窗口
mMap.setOnInfoWindowClickListener(this);//弹框窗口点击事件
mMap.setOnMapClickListener(this);//地图点击事件
@Override
public void onMapClick(LatLng latLng) {
//点击地图区域关闭所有窗口
for (Marker marker : mMarkers) {
marker.hideInfoWindow();
}
}
@Override
public void onInfoWindowClick(Marker marker) {
if (marker.isInfoWindowShown()) {
marker.hideInfoWindow();//再次点击窗口就隐藏窗口
}
}
到此自定义弹框窗口就完成了,以下为完整MainActivity代码
public class MainActivity extends AppCompatActivity implements AMap.OnInfoWindowClickListener, AMap.OnMapClickListener {
private AMap mMap;
private List<Marker> mMarkers;
private MapView mMapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMapView = findViewById(R.id.mapview);
mMapView.onCreate(savedInstanceState);// 此方法必须重写
mMap = mMapView.getMap();
mMap.setOnMapClickListener(this);//地图点击事件
initPoint(30.665534,104.070929); //地图中心点位
initMarker();//测试点位
}
/**
* 绘制marker
*/
private void initMarker() {
mMarkers = new ArrayList<>();
//重要 创建自定义适配器
MapInfoWinAdapter adapter = new MapInfoWinAdapter(this);
mMap.setInfoWindowAdapter(adapter);//设置自定义窗口adapter
mMap.setOnInfoWindowClickListener(this);
//绘制marker 实际使用时会循环创建marker并填入数据
Marker marker = mMap.addMarker(new MarkerOptions()
.anchor(0.5f, 0.5f)
.position(new LatLng(30.665534,104.070929))
.title("标题数据")
.snippet("消息数据")
.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(getResources(), R.mipmap.ic_launcher_round))));//点位图标
mMarkers.add(marker);
}
/**
* 加载地图中心点
*/
private void initPoint(double latitude, double Longitude) {
LatLng marker1 = new LatLng(latitude, Longitude);
mMap.moveCamera(CameraUpdateFactory.changeLatLng(marker1));
mMap.moveCamera(CameraUpdateFactory.zoomTo(12));
}
@Override
public void onMapClick(LatLng latLng) {
//点击地图区域关闭所有窗口
for (Marker marker : mMarkers) {
marker.hideInfoWindow();
}
}
@Override
public void onInfoWindowClick(Marker marker) {
if (marker.isInfoWindowShown()) {
marker.hideInfoWindow();//再次点击窗口就隐藏窗口
}
}
@Override
public void onResume() {
super.onResume();
if (mMapView != null)
mMapView.onResume(); //管理地图的生命周期
}
@Override
public void onPause() {
super.onPause();
if (mMapView != null)
mMapView.onPause(); //管理地图的生命周期
}
@Override
public void onDestroy() {
super.onDestroy();
if (mMapView != null)
mMapView.onDestroy(); //管理地图的生命周期
}
}
来源:https://blog.csdn.net/weixin_35959554/article/details/115715979
0
投稿
猜你喜欢
- 所谓文件的断点续传,就是一个线程传输文件,另一个线程控制传输标识,以达到暂停文件效果、恢复文件上传的效果。本demo使用最基本的线程之间的通
- 本文实例为大家分享了unity实现翻页按钮功能的具体代码,供大家参考,具体内容如下效果图:UI子父级关系:代码中也都有加入注释,有不懂可私信
- 实例如下:ServletRequestAttributes aRequestAttributes=(ServletRequestAttrib
- 这篇文章主要介绍了Java String的intern用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 使用wait()与notify()实现线程间协作1. wait()与notify()/notifyAll()调用sleep()和yield(
- 本课程的目标是帮你更有效的使用Java。其中讨论了一些高级主题,包括对象的创建、并发、序列化、反射以及其他高级特性。本课程将为你的精通Jav
- 本文研究的主要是java fastdfs客户端使用实例的相关内容,具体实现如下。什么是FastDFS?FastDFS是用c语言编写的一款开源
- 传感器简单的介绍一下传感器: 就是设备用来感知周边环境变化的硬件。Android中的传感器包含在传感器框架中,属于android.hardw
- Android屏蔽软键盘并且显示光标的实例详解如果是android4.0以下,那么editText.setInputType(InputTy
- 本文实例讲述了Java基于余弦方法实现的计算相似度算法。分享给大家供大家参考,具体如下:(1)余弦相似性通过测量两个向量之间的角的余弦值来度
- 写在前面:可能是临近期末了,各种课程设计接踵而来,最近在csdn上看到2个一样问答,那就是编写一个基于socket的聊天程序,正好最近刚用s
- 一,功能效果二,实现过程1,先写xml文件:dialog_keyboard.xml 注意事项(1),密码
- 本文实例讲解了java遍历读取xml文件内容的详细代码,分享给大家供大家参考,具体内容如下package test;import java.
- java8的stream取max public static void main(String[] args) { &
- 目录前言示例参考:前言按需加载对象延迟加载实际是推迟进行创建对象,直到对其调用后才进行创建初始化,延迟(懒加载)的好处是提高系统性能,避免不
- 写在前面,在笔者完成这个demo的时候,笔者发现现在大家已经不用Ajax来完成联级菜单了,实际上笔者这个demo也并不是为了完成这个,笔者主
- spring-mybatis获取mapper方式汇总项目背景:pojo下面有一个user实体类Dao包下面写了usermapper.xml
- Jackson 是当前用的比较广泛的,用来序列化和反序列化 json 的 Java 的开源框架。Jackson 社 区相对比较活跃,更新速度
- 场景我们团队现在面临着多端数据接口对接的问题,为了解决这个问题我们定义了接口对接的规范,前端(安卓,Ios,web前端)和后端进行了数据的格
- What内含一个或多个maven模块的SpringBoot项目称为SpringBoot多模块项目Why便于维护 将一个大的单体项目分成多个子