Android 百度地图 SDK v3.0.0 (三) 加入覆盖Marker与InfoWindow使用
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/37737213
上篇博客已经实现了地图的定位以及结合了方向传感器用户路痴定位方向,假设你还不清楚,请查看:Android 百度地图 SDK v3.0.0 (二) 定位与结合方向传感器。本章会教大家怎样加入覆盖物,实现周边搜索,以及对覆盖物的点击出现介绍等效果。
效果图:
我们的需求是。当用户点击衣食住行,或者对对附近搜索是。从server返回数据(经纬度,商家信息,介绍等)。然后动态生成覆盖物,实现上述效果。关于图片,因为手机上的内存的有限性,全部的图片下载完毕都应该存入预设的缓存中。比如LruCache。然后须要的时候从缓存取。缓存没有,下载完毕放入缓存;即实现全部的图片所占的内存永远不会超过缓存预设的内存值,当然了本篇的重点不是这个,我直接拿了几张图片加入我们的项目中模拟。
1、承载数据的实体
我们从server返回的数据部分,终于可能是个Json数组。我们须要转换为实体集合,即以下的Info.java:
- package com.zhy.zhy_baidu_ditu_demo03;
- import java.io.Serializable;
- import java.util.ArrayList;
- import java.util.List;
- public class Info implements Serializable
- {
- private static final long serialVersionUID = -758459502806858414L;
- /**
- * 精度
- */
- private double latitude;
- /**
- * 纬度
- */
- private double longitude;
- /**
- * 图片ID,真实项目中可能是图片路径
- */
- private int imgId;
- /**
- * 商家名称
- */
- private String name;
- /**
- * 距离
- */
- private String distance;
- /**
- * 赞数量
- */
- private int zan;
- public static List<Info> infos = new ArrayList<Info>();
- static
- {
- infos.add(new Info(34.242652, 108.971171, R.drawable.a01, "英伦贵族小旅馆",
- "距离209米", 1456));
- infos.add(new Info(34.242952, 108.972171, R.drawable.a02, "沙井国际洗浴会所",
- "距离897米", 456));
- infos.add(new Info(34.242852, 108.973171, R.drawable.a03, "五环服装城",
- "距离249米", 1456));
- infos.add(new Info(34.242152, 108.971971, R.drawable.a04, "老米家泡馍小炒",
- "距离679米", 1456));
- }
- public Info()
- {
- }
- public Info(double latitude, double longitude, int imgId, String name,
- String distance, int zan)
- {
- super();
- this.latitude = latitude;
- this.longitude = longitude;
- this.imgId = imgId;
- this.name = name;
- this.distance = distance;
- this.zan = zan;
- }
- public double getLatitude()
- {
- return latitude;
- }
- public void setLatitude(double latitude)
- {
- this.latitude = latitude;
- }
- public double getLongitude()
- {
- return longitude;
- }
- public void setLongitude(double longitude)
- {
- this.longitude = longitude;
- }
- public String getName()
- {
- return name;
- }
- public int getImgId()
- {
- return imgId;
- }
- public void setImgId(int imgId)
- {
- this.imgId = imgId;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public String getDistance()
- {
- return distance;
- }
- public void setDistance(String distance)
- {
- this.distance = distance;
- }
- public int getZan()
- {
- return zan;
- }
- public void setZan(int zan)
- {
- this.zan = zan;
- }
- }
我直接在实体类中声明了一个静态列表集合,模拟从server返回的数据Info.infos。
2、地图中动态加入Overlay
为了方便,我把button都放在menu菜单中:
- @Override
- public boolean onOptionsItemSelected(MenuItem item)
- {
- switch (item.getItemId())
- {
- case R.id.id_menu_map_addMaker:
- addInfosOverlay(Info.infos);
- break;
- ...
- }
- }
- /**
- * 初始化图层
- */
- public void addInfosOverlay(List<Info> infos)
- {
- mBaiduMap.clear();
- LatLng latLng = null;
- OverlayOptions overlayOptions = null;
- Marker marker = null;
- for (Info info : infos)
- {
- // 位置
- latLng = new LatLng(info.getLatitude(), info.getLongitude());
- // 图标
- overlayOptions = new MarkerOptions().position(latLng)
- .icon(mIconMaker).zIndex(5);
- marker = (Marker) (mBaiduMap.addOverlay(overlayOptions));
- Bundle bundle = new Bundle();
- bundle.putSerializable("info", info);
- marker.setExtraInfo(bundle);
- }
- // 将地图移到到最后一个经纬度位置
- MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(latLng);
- mBaiduMap.setMapStatus(u);
- }
能够看到,我们迭代加入了Overlay。然后在返回的Marker中设置了商家的信息,用户用户对Marker的点击时,拿到商家数据生成具体信息布局。
3、为地图上的Marker加入点击事件:
- //对Marker的点击
- mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener()
- {
- @Override
- public boolean onMarkerClick(final Marker marker)
- {
- //获得marker中的数据
- Info info = (Info) marker.getExtraInfo().get("info");
- InfoWindow mInfoWindow;
- //生成一个TextView用户在地图中显示InfoWindow
- TextView location = new TextView(getApplicationContext());
- location.setBackgroundResource(R.drawable.location_tips);
- location.setPadding(30, 20, 30, 50);
- location.setText(info.getName());
- //将marker所在的经纬度的信息转化成屏幕上的坐标
- final LatLng ll = marker.getPosition();
- Point p = mBaiduMap.getProjection().toScreenLocation(ll);
- Log.e(TAG, "--!" + p.x + " , " + p.y);
- p.y -= 47;
- LatLng llInfo = mBaiduMap.getProjection().fromScreenLocation(p);
- //为弹出的InfoWindow加入点击事件
- mInfoWindow = new InfoWindow(location, llInfo,
- new OnInfoWindowClickListener()
- {
- @Override
- public void onInfoWindowClick()
- {
- //隐藏InfoWindow
- mBaiduMap.hideInfoWindow();
- }
- });
- //显示InfoWindow
- mBaiduMap.showInfoWindow(mInfoWindow);
- //设置具体信息布局为可见
- mMarkerInfoLy.setVisibility(View.VISIBLE);
- //依据商家信息为具体信息布局设置信息
- popupInfo(mMarkerInfoLy, info);
- return true;
- }
- });
依据商家的信息Info.java为具体信息布局中的控件加入数据(记得生成TextView的时候,先设置背景,再设置padding,不然可能会失效~~~)
- /**
- * 依据info为布局上的控件设置信息
- *
- * @param mMarkerInfo2
- * @param info
- */
- protected void popupInfo(RelativeLayout mMarkerLy, Info info)
- {
- ViewHolder viewHolder = null;
- if (mMarkerLy.getTag() == null)
- {
- viewHolder = new ViewHolder();
- viewHolder.infoImg = (ImageView) mMarkerLy
- .findViewById(R.id.info_img);
- viewHolder.infoName = (TextView) mMarkerLy
- .findViewById(R.id.info_name);
- viewHolder.infoDistance = (TextView) mMarkerLy
- .findViewById(R.id.info_distance);
- viewHolder.infoZan = (TextView) mMarkerLy
- .findViewById(R.id.info_zan);
- mMarkerLy.setTag(viewHolder);
- }
- viewHolder = (ViewHolder) mMarkerLy.getTag();
- viewHolder.infoImg.setImageResource(info.getImgId());
- viewHolder.infoDistance.setText(info.getDistance());
- viewHolder.infoName.setText(info.getName());
- viewHolder.infoZan.setText(info.getZan() + "");
- }
这里我们使用了一个ViewHoler进行控件的复用,让findViewById仅仅会运行一次
- /**
- * 复用弹出面板mMarkerLy的控件
- *
- * @author zhy
- *
- */
- private class ViewHolder
- {
- ImageView infoImg;
- TextView infoName;
- TextView infoDistance;
- TextView infoZan;
- }
最后加入地图的单击事件,隐藏出现的具体信息布局和InfoWindow
- mBaiduMap.setOnMapClickListener(new OnMapClickListener()
- {
- @Override
- public boolean onMapPoiClick(MapPoi arg0)
- {
- return false;
- }
- @Override
- public void onMapClick(LatLng arg0)
- {
- mMarkerInfoLy.setVisibility(View.GONE);
- mBaiduMap.hideInfoWindow();
- }
- });
最后看一下我们的布局文件:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <com.baidu.mapapi.map.MapView
- android:id="@+id/id_bmapView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:clickable="true" />
- <RelativeLayout
- android:id="@+id/id_marker_info"
- android:visibility="gone"
- android:layout_width="fill_parent"
- android:layout_height="220dp"
- android:layout_alignParentBottom="true"
- android:background="#CC4e5a6b"
- android:clickable="true" >
- <ImageView
- android:id="@+id/info_img"
- android:layout_width="fill_parent"
- android:layout_height="150dp"
- android:layout_marginBottom="10dp"
- android:layout_marginLeft="12dp"
- android:layout_marginRight="12dp"
- android:layout_marginTop="10dp"
- android:alpha="1.0"
- android:background="@drawable/map_image_border_white"
- android:clickable="true"
- android:scaleType="fitXY"
- android:src="@drawable/a04" />
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="50dp"
- android:layout_alignParentBottom="true"
- android:background="@drawable/bg_map_bottom" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_marginLeft="20dp"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/info_name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="老米家泡馍小炒"
- android:textColor="#FFF5EB" />
- <TextView
- android:id="@+id/info_distance"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="距离200米"
- android:textColor="#FFF5EB" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_centerVertical="true"
- android:layout_marginRight="20dp"
- android:orientation="horizontal" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="zan"
- android:src="@drawable/map_zan" />
- <TextView
- android:id="@+id/info_zan"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="652"
- android:textColor="#FFF5EB" />
- </LinearLayout>
- </RelativeLayout>
- </RelativeLayout>
- </RelativeLayout>
除了MapView,其它都是具体信息的布局。默认是隐藏的,当用户点击Marker显示以及设置初值,当用户单击地图时再将其隐藏。
好了。到此介绍完毕~~
注:开发人员key须要换成自己申请的,不清楚申请的请看第一篇博客的。
百度地图相关博客视频版本号已经上线:Android中百度地图的使用期待您的支持。
博主部分视频已经上线,假设你不喜欢枯燥的文本。请猛戳(初录。期待您的支持):
2、Android自己定义控件实战 打造Android流式布局和热门标签
百度地图相关博客视频版本号已经上线:Android中百度地图的使用期待您的支持。
博主部分视频已经上线,假设你不喜欢枯燥的文本。请猛戳(初录,期待您的支持):
版权声明:本文博客原创文章。博客,未经同意,不得转载。
Android 百度地图 SDK v3.0.0 (三) 加入覆盖Marker与InfoWindow使用的更多相关文章
- Android 百度地图 SDK v3.0.0 (三) 添加覆盖物Marker与InfoWindow的使用
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37737213 上篇博客已经实现了地图的定位以及结合了方向传感器用户路痴定位方向, ...
- Android 百度地图 SDK v3.0.0 (四) 引入离线地图功能
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37758097 一直觉得地图应用支持离线地图很重要啊,我等移动2G屌丝,流量不易, ...
- Android 百度地图 SDK v3.0.0 (四) 离线地图功能介绍
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/37758097 一直认为地图应用支持离线地图非常重要啊.我等移动2G屌丝,流量不易 ...
- Android 百度地图 SDK v3.0.0 (一)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37729091 最近公司要把百度地图集成的项目中,于是我就研究了一天百度地图的SD ...
- 《第一行代码》(三: Android 百度地图 SDK v3.0.0)
百度地图的SDK变化较大,第一行代码里的demo已经不能用了,一直以为是代码类错误,害我花了很多时间,可以参考这位博主的:http://blog.csdn.net/lmj623565791/artic ...
- Android 百度地图 SDK v3.0.0 (二) 定位与结合方向传感器
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37730469 在上一篇博客中,我们成功把地图导入了我们的项目.本篇我们准备为地图 ...
- Android百度地图SDK 导航初始化和地图初始化引起的冲突
如题,相同是百度地图SDK开发过程中遇到的一个问题.交代下背景: 开发了一款内嵌百度地图的应用,因此里面差点儿相同将眼下百度地图SDK开放的主要功能都用到了,定位,地图显示,覆盖物标示.POI搜索,行 ...
- Android 百度地图SDK 定位
引用locSDK_6.1.3.jar,切记添加相应的so文件. 1.定位初始化,需要使用getApplicationContext() mLocClient = new LocationClient( ...
- Android 百度地图API(01)_开发环境 HelloBaiduMap
转载于:http://blog.csdn.net/lmj623565791/article/details/37729091 转载于:http://blog.csdn.net/crazy1235/ar ...
随机推荐
- linux c coding style
Linux kernel coding style This is a short document describing the preferred coding style for the lin ...
- linux stat系统调用,获取文件信息。
stat 函数原型: int stat(const char *path, struct stat *buf); struct stat 说明 struct stat { mode_t st_mode ...
- [概率dp] ZOJ 3822 Domination
题意: 给N×M的棋盘.每天随机找一个没放过棋子的格子放一个棋子 问使得每一个每列都有棋子的天数期望 思路: dp[i][j][k] 代表放了i个棋子占了j行k列 到达目标状态的期望 然后从 dp[n ...
- 编程算法 - 扑克牌的顺子 代码(C)
扑克牌的顺子 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 从扑克牌中随机抽取5张牌, 推断是不是一个顺子, 即这5张牌是不是连续的. 2~1 ...
- Web 应用程序项目 XXXX 已配置为使用 IIS。 无法访问 IIS 元数据库。您没有足够的特权访问计算机上的 IIS 网站。(转载)
Web 应用程序项目 XXXX 已配置为使用 IIS. 无法访问 IIS 元数据库.您没有足够的特权访问计算机上的 IIS 网站. 2012年05月19日 ⁄ 综合 ⁄ 共 261字 ⁄ 字号 小 中 ...
- KeyValuePair用法(转)
转载自:http://blog.sina.com.cn/s/blog_9741eba801016w61.html C# KeyValuePair<TKey,TValue>的用法.结构体,定 ...
- 解析stm32的时钟
STM32 时钟系统 http://blog.chinaunix.net/uid-24219701-id-4081961.html STM32的时钟系统 *** http://www.cnblo ...
- Eclipse使用技巧总结(二)
七.快速切换打开的文件 Ctrl + F6 八.快速大写.小写转换 Ctrl + Shift + Y Ctrl + Shift + X 九.快速删除光标所在行 Ctrl + D 十.快速复制 Ctrl ...
- 我的Python成长之路---第一天---Python基础(作业2:三级菜单)---2015年12月26日(雾霾)
作业二:三级菜单 三级菜单 可一次进入各个子菜单 思路: 这个题看似不难,难点在于三层循环的嵌套,我的思路就是通过flag的真假来控制每一层的循环的,简单来说就是就是通过给每一层循环一个单独的布尔变量 ...
- java--继承的一些笔记
public class Person { public void display(){ System.out.println("Play Person..."); } stati ...