【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 ...
随机推荐
- Java从零开始学十四(包和访问控制)
一.java中的包 Java文件的组织形式Windows中的文件功能类似 在开发比较大的项目时,不可能只涉及到一个java文件,可能要创建几十,甚至几百个java文件,这个时候,我们就可以使用包,把相 ...
- Ubuntu 12.04解决重启后resolv.conf清空的问题
这跟以前用RHT系的 情况是完全不一样的: 在google上搜了一下,发现这里面还真有些奥妙: 1 /etc/resolv.conf 其实是一个Link 它其实指向的是 /run/resolvconf ...
- Unity 文字爆炸(风化)消失效果 粒子系统应用
利用Unity的粒子系统,使用C#代码控制粒子的位置和速度,实现文字风化爆炸的效果. Unity的东西,不像flash,不能直接放到网页中,没办法了,只能放截图了.有兴趣的可以下载看看:text_ex ...
- eclispe Missing artifact...
eclispe Missing artifact... CreateTime--2018年4月24日18:47:21 Author:Marydon 1.情景再现 eclipse pom.xml报错 ...
- vscode简洁的代码编辑器
微软去年tuichu的代码编辑神器,vscode很不辞哦,感受还不错.微软也破天荒地跨平台地支持... 支持多种语言 vscode(官方):code.visualstudio.com vscode中文 ...
- mysql利用yum安装指定数据存放路径
测试环境: Centos6.5.MySQL5.6.28 yum安装具有速度快,便捷关键是不用编译,编译时间太久了! 01.下载mysql https://mirrors.tuna.tsinghua.e ...
- Go 1.5keyword搜索文件夹、文件、文件内容_修复一个小BUG
package main import ( "bufio" "bytes" "flag" "fmt" "io& ...
- openvpn服务端一键生成windows,macos客户端配置文件
#!/bin/bash #获取参数 while getopts "n:" opt; do case $opt in n) client_name=$OPTARG ;; \?) ;; ...
- zookeeper与kafka安装部署及java环境搭建(发布订阅模式)
1. ZooKeeper安装部署 本文在一台机器上模拟3个zk server的集群安装. 1.1. 创建目录.解压 cd /usr/ #创建项目目录 mkdir zookeeper cd zookee ...
- Android学习系列(4)--App自适应draw9patch不失真背景
做人要大度,海纳百川,做事要圆滑,左右逢源,这让我想到了编程也是如此,代码要扩展,界面也要考虑自适应.这篇文章是Android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用. 1. ...