iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >怎么在Android中利用marker自定义一个弹框窗口
  • 490
分享到

怎么在Android中利用marker自定义一个弹框窗口

2023-06-14 19:06:39 490人浏览 薄情痞子
摘要

怎么在Android中利用marker自定义一个弹框窗口?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。Android是什么Android是一种基于linux内核的自由及开放源代

怎么在Android中利用marker自定义一个弹框窗口?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

Android是什么

Android是一种基于linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发

gradle里添加高德地图依赖

implementation 'com.amap.api:map2d:latest.integration'//2d地图功能 implementation 'com.amap.api:location:latest.integration'//定位功能

如果要用到定位的话,就首先到高德控制台里面加入本应用的信息获取到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>

创建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

@Overrideprotected 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();//测试点位    }        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(); //管理地图的生命周期    }  }

添加弹框自定义布局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"/>

记得设置布局最小高度和宽度,不然窗口会默认宽度高度,会使布局显示不完整

添加自定义弹框窗口adapter

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;        }     }}

地图绑定adapter

//重要 创建自定义适配器MapInfoWinAdapter adapter = new MapInfoWinAdapter(this);mMap.setInfoWindowAdapter(adapter);//设置自定义窗口adapter

现在点击marker就会弹出我们自定义的布局了

点击地图或弹框关闭弹框窗口

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();//测试点位    }        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(); //管理地图的生命周期    }  }

看完上述内容,你们掌握怎么在Android中利用marker自定义一个弹框窗口的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注编程网精选频道,感谢各位的阅读!

--结束END--

本文标题: 怎么在Android中利用marker自定义一个弹框窗口

本文链接: https://www.lsjlt.com/news/274404.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C++ 生态系统中流行库和框架的贡献指南
    作为 c++++ 开发人员,通过遵循以下步骤即可为流行库和框架做出贡献:选择一个项目并熟悉其代码库。在 issue 跟踪器中寻找适合初学者的问题。创建一个新分支,实现修复并添加测试。提交...
    99+
    2024-05-15
    框架 c++ 流行库 git
  • C++ 生态系统中流行库和框架的社区支持情况
    c++++生态系统中流行库和框架的社区支持情况:boost:活跃的社区提供广泛的文档、教程和讨论区,确保持续的维护和更新。qt:庞大的社区提供丰富的文档、示例和论坛,积极参与开发和维护。...
    99+
    2024-05-15
    生态系统 社区支持 c++ overflow 标准库
  • c++中if elseif使用规则
    c++ 中 if-else if 语句的使用规则为:语法:if (条件1) { // 执行代码块 1} else if (条件 2) { // 执行代码块 2}// ...else ...
    99+
    2024-05-15
    c++
  • c++中的继承怎么写
    继承是一种允许类从现有类派生并访问其成员的强大机制。在 c++ 中,继承类型包括:单继承:一个子类从一个基类继承。多继承:一个子类从多个基类继承。层次继承:多个子类从同一个基类继承。多层...
    99+
    2024-05-15
    c++
  • c++中如何使用类和对象掌握目标
    在 c++ 中创建类和对象:使用 class 关键字定义类,包含数据成员和方法。使用对象名称和类名称创建对象。访问权限包括:公有、受保护和私有。数据成员是类的变量,每个对象拥有自己的副本...
    99+
    2024-05-15
    c++
  • c++中优先级是什么意思
    c++ 中的优先级规则:优先级高的操作符先执行,相同优先级的从左到右执行,括号可改变执行顺序。操作符优先级表包含从最高到最低的优先级列表,其中赋值运算符具有最低优先级。通过了解优先级,可...
    99+
    2024-05-15
    c++
  • c++中a+是什么意思
    c++ 中的 a+ 运算符表示自增运算符,用于将变量递增 1 并将结果存储在同一变量中。语法为 a++,用法包括循环和计数器。它可与后置递增运算符 ++a 交换使用,后者在表达式求值后递...
    99+
    2024-05-15
    c++
  • c++中a.b什么意思
    c++kquote>“a.b”表示对象“a”的成员“b”,用于访问对象成员,可用“对象名.成员名”的语法。它还可以用于访问嵌套成员,如“对象名.嵌套成员名.成员名”的语法。 c++...
    99+
    2024-05-15
    c++
  • C++ 并发编程库的优缺点
    c++++ 提供了多种并发编程库,满足不同场景下的需求。线程库 (std::thread) 易于使用但开销大;异步库 (std::async) 可异步执行任务,但 api 复杂;协程库 ...
    99+
    2024-05-15
    c++ 并发编程
  • 如何在 Golang 中备份数据库?
    在 golang 中备份数据库对于保护数据至关重要。可以使用标准库中的 database/sql 包,或第三方包如 github.com/go-sql-driver/mysql。具体步骤...
    99+
    2024-05-15
    golang 数据库备份 mysql git 标准库
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作