【Android】3.16 离线地图功能
分类:C#、Android、VS2015、百度地图应用; 创建日期:2016-02-04
一、简介
百度地图目前已经支持矢量离线地图数据的下载、更新。
使用离线地图,可满足在无网络环境下查看地图信息的需求,此外,在有离线地图的情况下,SDK会优先加载离线地图使用,减少用户流量方面的开销,为用户提供更流畅的地图服务体验。
更新离线地图增加update方法,与start方法区别开来,当检测到有城市离线包有更新,调用update方法执行更新,调用pause暂停,调用start继续
离线地图资源可通过SDK“在线下载离线包接口”下载获取。自v2.9.0起,官网不再支持地图离线包下载,所以SDK去掉“手动导入离线包接口”,SDK在线下载离线包接口仍维持不变。
二、运行截图
简介:介绍如何下载和使用离线地图。
详述:
可以搜索、下载、删除、查看离线地图。
该示例运行截图如下:

三、设计步骤
1、添加demo16_offline.xml文件
在layout文件夹下添加该文件,然后将代码改为下面的内容:
<?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" > <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal" > <TextView
android:id="@+id/cityid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="131" /> <!-- 隐藏输入法用 -->
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" /> <EditText
android:id="@+id/city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="北京" /> <Button
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_style"
android:text="搜索" />
</LinearLayout> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal" > <TextView
android:id="@+id/state"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="已下载:--" /> <Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_style"
android:text="开始" /> <Button
android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_style"
android:text="停止" /> <Button
android:id="@+id/del"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_style"
android:text="删除" />
</LinearLayout> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/city_list"
android:layout_width="match_parent"
android:layout_height="50dip"
android:orientation="horizontal" > <Button
android:id="@+id/clButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_style"
android:text="城市列表" /> <Button
android:id="@+id/localButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_style"
android:text="下载管理" />
</LinearLayout> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/citylist_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="热门城市" /> <ListView
android:id="@+id/hotcitylist"
android:layout_width="fill_parent"
android:layout_height="200dip" /> <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="全国" /> <ListView
android:id="@+id/allcitylist"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/localmap_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="已下载城市 " /> <ListView
android:id="@+id/localmaplist"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout> </LinearLayout>
2、添加demo16_offline_localmap_list.xml文件
在layout文件夹下添加该文件,然后将代码改为下面的内容:
<?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="horizontal"
android:padding="10dip" > <TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="已下载城市 " /> <TextView
android:id="@+id/update"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:textColor="#FF0000" /> <TextView
android:id="@+id/ratio"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" /> <Button
android:id="@+id/display"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_style"
android:text="查看" /> <Button
android:id="@+id/remove"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_style"
android:text="删除" /> </LinearLayout>
3、添加Demo16Offline.cs文件
在SrcSdkDemos文件夹下添加该文件,然后将代码改为下面的内容:
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Util;
using Android.Views;
using Android.Widget;
using Com.Baidu.Mapapi.Map.Offline;
using System.Collections.Generic;
namespace BdMapV371Demos.SrcSdkDemos
{
[Activity(Label = "@string/demo_name_offline",
ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden,
ScreenOrientation = ScreenOrientation.Sensor)]
public class Demo16Offline : Activity, IMKOfflineMapListener
{
private MKOfflineMap mOffline = null;
private TextView cidView;
private TextView stateView;
private EditText cityNameView; //已下载的离线地图信息列表
private IList<MKOLUpdateElement> localMapList = null;
private LocalMapAdapter lAdapter = null; protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.demo16_offline); mOffline = new MKOfflineMap();
mOffline.Init(this); //【搜索】按钮--搜索需要下载的城市离线包
var search = FindViewById<Button>(Resource.Id.search);
search.Click += delegate
{
IList<MKOLSearchRecord> records = mOffline.SearchCity(cityNameView.Text);
if (records == null || records.Count != )
return;
cidView.Text = records[].CityID.ToString();
}; //【开始】按钮--开始下载
var start = FindViewById<Button>(Resource.Id.start);
start.Click += delegate
{
int cityid = int.Parse(cidView.Text);
mOffline.Start(cityid);
Toast.MakeText(this, "开始下载离线地图. cityid: " + cityid, ToastLength.Short).Show();
}; //【停止】按钮--暂停下载
var stop = FindViewById<Button>(Resource.Id.stop);
stop.Click += delegate
{
int cityid = int.Parse(cidView.Text);
mOffline.Pause(cityid);
Toast.MakeText(this, "暂停下载离线地图. cityid: " + cityid, ToastLength.Short).Show();
}; //【删除】按钮--删除离线地图
var del = FindViewById<Button>(Resource.Id.del);
del.Click += delegate
{
int cityid = int.Parse(cidView.Text);
mOffline.Remove(cityid);
Toast.MakeText(this, "删除离线地图. cityid: " + cityid, ToastLength.Short).Show();
}; //【城市列表】按钮--切换至城市列表
var clButton = FindViewById<Button>(Resource.Id.clButton);
clButton.Click += delegate
{
LinearLayout cl = FindViewById<LinearLayout>(Resource.Id.citylist_layout);
LinearLayout lm = FindViewById<LinearLayout>(Resource.Id.localmap_layout);
lm.Visibility = ViewStates.Gone;
cl.Visibility = ViewStates.Visible;
}; //【下载管理】按钮--切换至下载管理列表
var localButton = FindViewById<Button>(Resource.Id.localButton);
localButton.Click += delegate
{
LinearLayout cl = FindViewById<LinearLayout>(Resource.Id.citylist_layout);
LinearLayout lm = FindViewById<LinearLayout>(Resource.Id.localmap_layout);
lm.Visibility = ViewStates.Visible;
cl.Visibility = ViewStates.Gone;
}; InitView();
} private void InitView()
{
cidView = FindViewById<TextView>(Resource.Id.cityid);
cityNameView = FindViewById<EditText>(Resource.Id.city);
stateView = FindViewById<TextView>(Resource.Id.state); ListView hotCityList = FindViewById<ListView>(Resource.Id.hotcitylist);
IList<string> hotCities = new List<string>();
//获取热闹城市列表
IList<MKOLSearchRecord> records1 = mOffline.HotCityList;
if (records1 != null)
{
foreach (MKOLSearchRecord r in records1)
{
hotCities.Add(string.Format("{0}({1})--{2}",
r.CityName, r.CityID, FormatDataSize(r.Size)));
}
}
IListAdapter hAdapter = new ArrayAdapter<string>(this,
Android.Resource.Layout.SimpleListItem1, hotCities);
hotCityList.Adapter = hAdapter; ListView allCityList = FindViewById<ListView>(Resource.Id.allcitylist);
//获取所有支持离线地图的城市
IList<string> allCities = new List<string>();
IList<MKOLSearchRecord> records2 = mOffline.OfflineCityList;
if (records1 != null)
{
foreach (MKOLSearchRecord r in records2)
{
allCities.Add(string.Format("{0}({1})--{2}",
r.CityName, r.CityID, FormatDataSize(r.Size)));
}
}
IListAdapter aAdapter = new ArrayAdapter<string>(this,
Android.Resource.Layout.SimpleListItem1, allCities);
allCityList.Adapter = aAdapter; LinearLayout cl = FindViewById<LinearLayout>(Resource.Id.citylist_layout);
LinearLayout lm = FindViewById<LinearLayout>(Resource.Id.localmap_layout);
lm.Visibility = ViewStates.Gone;
cl.Visibility = ViewStates.Visible; //获取已下过的离线地图信息
localMapList = mOffline.AllUpdateInfo;
if (localMapList == null)
{
localMapList = new List<MKOLUpdateElement>();
} ListView localMapListView = FindViewById<ListView>(Resource.Id.localmaplist);
lAdapter = new LocalMapAdapter(this);
localMapListView.Adapter = lAdapter; } // 更新状态显示
public void UpdateView()
{
localMapList = mOffline.AllUpdateInfo;
if (localMapList == null)
{
localMapList = new List<MKOLUpdateElement>();
}
lAdapter.NotifyDataSetChanged();
} protected override void OnPause()
{
int cityid = int.Parse(cidView.Text);
MKOLUpdateElement temp = mOffline.GetUpdateInfo(cityid);
if (temp != null && temp.Status == MKOLUpdateElement.Downloading)
{
mOffline.Pause(cityid);
}
base.OnPause();
} protected override void OnResume()
{
base.OnResume();
} public string FormatDataSize(int size)
{
string ret = "";
if (size < ( * ))
{
ret = Java.Lang.String.Format("%dK", size / );
}
else
{
ret = Java.Lang.String.Format("%.1fM", size / ( * 1024.0));
}
return ret;
} protected override void OnDestroy()
{
// 退出时,销毁离线地图模块
mOffline.Destroy();
base.OnDestroy();
} //实现接口
public void OnGetOfflineMapState(int type, int state)
{
switch (type)
{
case MKOfflineMap.TypeDownloadUpdate:
{
MKOLUpdateElement update = mOffline.GetUpdateInfo(state);
//处理下载进度更新提示
if (update != null)
{
stateView.Text = Java.Lang.String.Format("%s : %d%%", update.CityName,
update.Ratio);
UpdateView();
}
}
break;
case MKOfflineMap.TypeNewOffline:
//有新离线地图安装
Log.Debug("OfflineDemo", string.Format("add offlinemap num:{0:d}", state));
break;
case MKOfflineMap.TypeVerUpdate:
// 版本更新提示
// MKOLUpdateElement e = mOffline.GetUpdateInfo(state);
break;
}
} // 离线地图管理列表适配器
public class LocalMapAdapter : BaseAdapter
{
private Demo16Offline offlineDemo; public LocalMapAdapter(Demo16Offline offlineDemo)
{
this.offlineDemo = offlineDemo;
} public override int Count
{
get { return offlineDemo.localMapList.Count; }
} public override Java.Lang.Object GetItem(int index)
{
return offlineDemo.localMapList[index];
} public override long GetItemId(int index)
{
return index;
} public override View GetView(int index, View view, ViewGroup arg2)
{
MKOLUpdateElement e = (MKOLUpdateElement)GetItem(index);
view = View.Inflate(offlineDemo,
Resource.Layout.demo16_offline_localmap_list, null);
InitViewItem(view, e);
return view;
} void InitViewItem(View view, MKOLUpdateElement e)
{
Button display = view.FindViewById<Button>(Resource.Id.display);
Button remove = view.FindViewById<Button>(Resource.Id.remove);
TextView title = view.FindViewById<TextView>(Resource.Id.title);
TextView update = view.FindViewById<TextView>(Resource.Id.update);
TextView ratio = view.FindViewById<TextView>(Resource.Id.ratio);
ratio.Text = e.Ratio + "%";
title.Text = e.CityName;
if (e.Update)
{
update.Text = "可更新";
}
else
{
update.Text = "最新";
}
if (e.Ratio != )
{
display.Enabled = false;
}
else
{
display.Enabled = true;
}
remove.Click += delegate
{
offlineDemo.mOffline.Remove(e.CityID);
offlineDemo.UpdateView();
};
display.Click += delegate
{
Intent intent = new Intent();
intent.PutExtra("x", e.GeoPt.Longitude);
intent.PutExtra("y", e.GeoPt.Latitude);
intent.SetClass(offlineDemo, typeof(Demo02BaseMap));
offlineDemo.StartActivity(intent);
};
}
}
}
}
4、修改MainActivity.cs
在MainActivity.cs文件的demos字段定义中,去掉【示例16】下面的注释。
运行观察结果。
【Android】3.16 离线地图功能的更多相关文章
- 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百度地图离线地图
1.首先把离线地图放在android工程下的assets里面. 注意:建议离线地图下载通过百度地图APIDEMO去下载,因为到官网上下载的离线地图文件格式不一样,APIDEMO的格式是.dat,而官网 ...
- 如何解决Android SDK中离线文档打开慢的问题
原文:http://blog.csdn.net/hansel/article/details/39268511 Android SDK中的离线文档虽然都是本地文件,但是有很多Javascript, C ...
- 一款基于 Android 开发的离线版的 MM 图片浏览 App
一款离线版的 MM 图片浏览 App,有点类似掌上百度的图片专栏应用.图片采用瀑布流展示方式,点击图片集,支持左右手势滑动切换图片:支持放大缩小功能. 实现功能:1)图片完全离线,不耗个人 GPRS ...
- 【Android】16.0 第16章 自定义服务和系统服务—本章示例主界面
分类:C#.Android.VS2015: 创建日期:2016-03-01 一.简介 本章主要演示Started Service.带Intent过滤器的Started Service.IntentSe ...
- [Android]豆瓣FM离线数据
离线目录结构: /sdcard/Android/data/com.douban.radio下 ./cache/fileCaches: 离线音乐歌词(lyric) ./cache/images: 离线音 ...
- [Android Tips] 16. Update Android SDK from command-line
$ cd $ANROID_HOME $ tools/android update sdk -u -s 参数 -s --no-https : Uses HTTP instead of HTTPS (th ...
- android学习16——library project的使用
library project和普通的project没有区别.用如下命令新建的一个工程. android create project --target 3 --name MyActivity --p ...
随机推荐
- Appium Python 三:Desired capabilities
简介 Desired capabilities 是一些键值对的集合.python里面就采用字典的方式. 客户端将这些键值对发给服务端,告诉服务端我们想要怎么测试.比如,我们可以把 platformNa ...
- iOS——Quartz2D
0. 复习. 1.基本图形绘制 * 线段(线宽.线段样式) * 矩形(空心.实心.颜色) * 三角形.四边形等形状 1> 说明 - (void)drawRect:(CGRect)rect 什么时 ...
- javascript 设计模式
了解JavaScript设计模式我们需要知道的一些必要知识点:(内容相对基础,高手请跳过) 闭包:关于闭包这个月在园子里有几篇不错的分享了,在这我也从最实际的地方出发,说说我的理解. 1.闭包最常用的 ...
- JVM:垃圾回收机制和调优手段
转载请注明出处: jiq•钦's technical Blog - 季义钦 引言: 我们都知道JVM内存由几个部分组成:堆.方法区.栈.程序计数器.本地方法栈 JVM垃圾回收仅针对公共内存区域即:堆和 ...
- 【Oracle】迁移含有BLOG类型的字段的表
日常数据库迁移过程中,发现dmp文件中的表,如果表含有blog类型的字段,无法进行迁移. 以下是我迁移含有blog类型字段表的经验分享' 使用PLSQL Developer进行导出 接着选择需要导出的 ...
- 类的专有方法(__len__)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #http://www.imooc.com/code/6252 #类的专有方法(__len__) #如果一个类 ...
- 转:eclipse里面显示中文乱码
显示中文会变成乱码解决方案:Windows- >Pereferences- >General->Workspace- >Text File Encoding 选项下 ...
- document.body、document.documentElement和window获取视窗大小的差别
来源:http://www.ido321.com/906.html 在w3school关于window对象的介绍中,介绍了获取浏览器窗体大小的三种方法(浏览器的视口,不包含工具栏和滚动栏). 对于In ...
- java中写sql语句的小小细节
来源于:http://www.cnblogs.com/reine98/p/6180472.html 看如下一条sql语句 1 2 3 4 5 6 String sql="SELECT * F ...
- Swift3 获取版本号,比较版本大小
Swift获取应用版本号:version 1.获取本地版本号 /// 获取本地版本号 func getLocalVersion() -> String { var localVersion:St ...