分类:C#、Android、VS2015、百度地图应用; 创建日期:2016-02-04

一、简介

文件名:Demo05MapControl.cs

简介:介绍平移和缩放地图,双指操作地图,监听地图点击事件

详述:

(1)介绍地图缩放级别、旋转度和俯视度的get和set方法;

(2)监听单击和长按地图事件;

(3)单击、双击和长按地图获取该点的经纬度坐标;

(4)对地图显示内容进行截图,截图保存地址为:/mnt/sdcard/test.png;

运行截图

在x86模拟器中的运行效果如下:

二、设计步骤

1、添加demo06_mapcontrol.axml

在layout文件夹下添加该文件,将其改为下面的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal" > <Button
android:id="@+id/zoombutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=""
android:text="缩放" /> <EditText
android:id="@+id/zoomlevel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=""
android:text="" /> <Button
android:id="@+id/rotatebutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=""
android:text="旋转" /> <EditText
android:id="@+id/rotateangle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=""
android:text="" /> <Button
android:id="@+id/overlookbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=""
android:text="俯视" /> <EditText
android:id="@+id/overlookangle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=""
android:text="-30" />
</LinearLayout> <TextView
android:id="@+id/state"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines=""
android:text="点击、长按、双击地图以获取经纬度和地图状态" /> <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <com.baidu.mapapi.map.TextureMapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" /> <Button
android:id="@+id/savescreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dip"
android:text="截图" />
</RelativeLayout> </LinearLayout>

2、添加Demo06MapControl.cs

在SrcSdkDemos文件夹下添加该文件,然后将其内容改为下面的代码:

using Android.App;
using Android.Content.PM;
using Android.Graphics;
using Android.OS;
using Android.Widget;
using Com.Baidu.Mapapi.Map;
using Com.Baidu.Mapapi.Model;
using System.IO; namespace BdMapV371Demos.SrcSdkDemos
{
/// <summary>
/// 演示地图缩放,旋转,视角控制
/// </summary>
[Activity(Label = "@string/demo_name_control",
ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden,
ScreenOrientation = ScreenOrientation.Sensor)]
public class Demo06MapControl : Activity,
BaiduMap.IOnMapClickListener, BaiduMap.IOnMapLongClickListener,
BaiduMap.IOnMapDoubleClickListener, BaiduMap.IOnMapStatusChangeListener,
BaiduMap.ISnapshotReadyCallback
{
private TextureMapView mMapView;
private BaiduMap mBaiduMap; /// <summary>
/// 当前地点击点
/// </summary>
private LatLng currentPt; private string touchType; /// <summary>
/// 用于显示地图状态的面板
/// </summary>
private TextView mStateBar; protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.demo06_mapcontrol); mMapView = FindViewById<TextureMapView>(Resource.Id.bmapView);
mBaiduMap = mMapView.Map;
mBaiduMap.SetMapStatus(MapStatusUpdateFactory.NewLatLng(MainActivity.HeNanUniversity)); mStateBar = FindViewById<TextView>(Resource.Id.state);
mBaiduMap.SetOnMapClickListener(this);
mBaiduMap.SetOnMapLongClickListener(this);
mBaiduMap.SetOnMapDoubleClickListener(this);
mBaiduMap.SetOnMapStatusChangeListener(this); var btnZoom = FindViewById<Button>(Resource.Id.zoombutton);
btnZoom.Click += delegate
{
//处理缩放 sdk 缩放级别范围: [3.0, 19.0]
EditText t = FindViewById<EditText>(Resource.Id.zoomlevel);
try
{
float zoomLevel = float.Parse(t.Text);
MapStatusUpdate u = MapStatusUpdateFactory.ZoomTo(zoomLevel);
mBaiduMap.AnimateMapStatus(u);
}
catch
{
Toast.MakeText(this, "请输入正确的缩放级别", ToastLength.Short).Show();
}
UpdateMapState();
}; var btnRotate = FindViewById<Button>(Resource.Id.rotatebutton);
btnRotate.Click += delegate
{
//处理旋转。旋转角范围:-180~180 , 单位:度,逆时针旋转
EditText t = FindViewById<EditText>(Resource.Id.rotateangle);
try
{
int rotateAngle = int.Parse(t.Text);
MapStatus ms = new MapStatus.Builder(mBaiduMap.MapStatus).Rotate(rotateAngle).Build();
MapStatusUpdate u = MapStatusUpdateFactory.NewMapStatus(ms);
mBaiduMap.AnimateMapStatus(u);
}
catch
{
Toast.MakeText(this, "请输入正确的旋转角度", ToastLength.Short).Show();
}
UpdateMapState();
}; var btnOverlook = FindViewById<Button>(Resource.Id.overlookbutton);
btnOverlook.Click += delegate
{
//处理俯视。俯角范围:-45~0, 单位:度
EditText t = FindViewById<EditText>(Resource.Id.overlookangle);
try
{
int overlookAngle = int.Parse(t.Text);
MapStatus ms = new MapStatus.Builder(mBaiduMap.MapStatus).Overlook(overlookAngle).Build();
MapStatusUpdate u = MapStatusUpdateFactory.NewMapStatus(ms);
mBaiduMap.AnimateMapStatus(u);
}
catch
{
Toast.MakeText(this, "请输入正确的俯角", ToastLength.Short).Show();
}
UpdateMapState();
}; var btnSaveScreen = FindViewById<Button>(Resource.Id.savescreen);
btnSaveScreen.Click += delegate
{
// 截图(在SnapshotReadyCallback中保存图片到sd卡)
mBaiduMap.Snapshot(this);
Toast.MakeText(this, "正在截取屏幕图片...", ToastLength.Short).Show();
UpdateMapState();
};
} #region 实现BaiduMap.IOnMapClickListener接口
public void OnMapClick(LatLng p0)
{
touchType = "单击";
currentPt = p0;
UpdateMapState();
} public bool OnMapPoiClick(MapPoi p0)
{
return false;
}
#endregion #region 实现BaiduMap.IOnMapLongClickListener接口
public void OnMapLongClick(LatLng p0)
{
touchType = "长按";
currentPt = p0;
UpdateMapState();
}
#endregion #region 实现BaiduMap.IOnMapDoubleClickListener接口
public void OnMapDoubleClick(LatLng p0)
{
touchType = "双击";
currentPt = p0;
UpdateMapState();
}
#endregion #region 实现BaiduMap.IOnMapStatusChangeListener接口
public void OnMapStatusChange(MapStatus p0)
{
UpdateMapState();
} public void OnMapStatusChangeFinish(MapStatus p0)
{
UpdateMapState();
} public void OnMapStatusChangeStart(MapStatus p0)
{
UpdateMapState();
}
#endregion #region 实现BaiduMap.ISnapshotReadyCallback接口
public void OnSnapshotReady(Bitmap p0)
{
string file = "/mnt/sdcard/test.png";
FileStream outX;
try
{
outX = new FileStream(file, FileMode.Create);
if (p0.Compress(
Bitmap.CompressFormat.Png, , outX))
{
outX.Flush();
outX.Close();
}
Toast.MakeText(this,
"屏幕截图成功,图片保存在: " + file.ToString(),
ToastLength.Short).Show();
}
catch (FileNotFoundException e)
{
throw e;
}
catch (IOException e)
{
throw e;
}
}
#endregion /// <summary>
/// 更新地图状态显示面板
/// </summary>
private void UpdateMapState()
{
if (mStateBar == null)
{
return;
}
string state = "";
if (currentPt == null)
{
state = "点击、长按、双击地图以获取经纬度和地图状态";
}
else
{
state = string.Format(touchType + ",当前经度:{0:f6} 当前纬度:{1:f6}",
currentPt.Longitude, currentPt.Latitude);
}
state += "\n";
MapStatus ms = mBaiduMap.MapStatus;
state += string.Format("zoom={0:f2},rotate={1:d},overlook={2:d}",
ms.Zoom, (int)ms.Rotate, (int)ms.Overlook);
mStateBar.Text = state;
} protected override void OnPause()
{
mMapView.OnPause();
base.OnPause();
} protected override void OnResume()
{
mMapView.OnResume();
base.OnResume();
} protected override void OnDestroy()
{
mMapView.OnDestroy();
base.OnDestroy();
}
}
}

3、修改MainActivity.cs

在MainActivity.cs文件的demos字段定义中添加下面的代码。

          //示例6--地图操作功能
new DemoInfo<Activity>(Resource.String.demo_title_control,
Resource.String.demo_desc_control,
new Demo06MapControl()),

运行观察结果。

【Android】3.6 地图基本控制方法的更多相关文章

  1. android定位和地图开发实例

    在android开发中地图和定位是很多软件不可或缺的内容,这些特色功能也给人们带来了很多方便. 首先介绍一下地图包中的主要类: MapController : 主要控制地图移动,伸缩,以某个GPS坐标 ...

  2. Android使用百度地图出现闪退及定位时显示蓝屏问题

     目录 1.Android使用百度地图出现闪退 2.Android使用百度地图定位出现蓝屏问题     1.Android使用百度地图出现闪退 一般情况下出现闪退是在AndroidManifest.x ...

  3. Android 打开高德地图、百度地图进行导航;打开第三方App去导航;

    抽成工具类了,复制下来就能直接用了,直接看代码吧: 高德地图Url Api: http://lbs.amap.com/api/amap-mobile/guide/android/navigation ...

  4. Android studio百度地图demo出现230错误,key校验失败

    转自daoxiaomianzi原文 Android studio 百度地图demo出现230错误,key校验失败 使用AndroidStudio导入Baidu地图的as版的demo,引入后,发现没有k ...

  5. Android studio 百度地图开发(3)地图导航

    Android studio 百度地图开发(3)地图导航 email:chentravelling@163.com 开发环境:win7 64位,Android Studio,请注意是Android S ...

  6. Android studio 百度地图开发(2)地图定位

    Android studio 百度地图开发(2)地图定位 email:chentravelling@163.com 开发环境:win7 64位,Android Studio,请注意是Android S ...

  7. Android 绘制中国地图

    最近的版本有这样一个需求: 有 3 个要素: 中国地图 高亮省区 中心显示数字 面对这样一个需求,该如何实现呢? 高德地图 因为项目是基于高德地图来做的,所以很自然而然的想到了高德.但是当查阅高德地图 ...

  8. Android 调用百度地图API

    一.到 百度地图开发平台下载SDK http://lbsyun.baidu.com/index.php?title=androidsdk/sdkandev-download 1.点击自定义下载 2.下 ...

  9. Android下百度地图开发(一):基础地图展示

    最终效果如: 这个需要详细说下,准备工作较为繁琐. 第一步:去百度申请一个开发者账号,新建一个android应用,会让你输入开发者SHA1,目前我只拿到开发版SHA1,发布版那里也填的是这个. 获取方 ...

随机推荐

  1. STL - Unorderedset - 自定义哈希函数

    1. hash工具类 hashval.hpp #ifndef _Core_HashVal_H_ #define _Core_HashVal_H_ #include <functional> ...

  2. (C++)UrlEncode的标准实现

    http://blog.163.com/xiaopengyan_109/blog/static/149832173201072685539394/ 地址栏传中文参数后是什么编码格式 网页中的表单使用P ...

  3. weblogic.servlet.proxy.HttpProxyServlet 进行代理设置

    1:代理访问服务器应用配置 A:192.168.0.11 B:192.168.0.12 访问:http://192.168.0.11:9001/test/test.jsp 代理服务直接代理访问: ht ...

  4. table设置表格有滚动条

    table 设置表格有滚动条. 少说多做,代码中有注释: <!DOCTYPE HTML> <html> <head> <meta http-equiv=&qu ...

  5. oracle卸载清除注册表(彻底卸载)

    用Oracle自带的卸载程序不能从根本上卸载Oracle,从而为下次的安装留下隐患,那么怎么才能完全卸载Oracle呢? 那就是直接注册表清除,步骤如下: . 开始->设置->控制面板-& ...

  6. linux 系统文件的特殊权限

    文件权限与归属 Linux系统中的一切都是文件,但每个文件的类型不尽相同,并且Linux系统会用不同的符号来加以区分,常见的包括有 -:普通文件,d:目录文件,l:链接文件,b:块设备文件,c:字符设 ...

  7. ExceptionLess 搭建到本地服务器

    Exceptionless 是一个开源的实时的日志收集框架,它可以应用在基于 ASP.NET,ASP.NET Core,Web Api,Web Forms,WPF,Console,MVC 等技术栈的应 ...

  8. Mybatis日期类型的关系判断

    进行时间段的查询时,在mapper文件中直接使用">","<"等关系运算符是无法解析的 <if test="executeStart ...

  9. 1、简单的BackGroundWorker多线程时时刷新UI界面,并显示进度

    BackGroundWorker是微软提供的封装好了的,非常实用的控件,我们可以在控件中将其拖到Winform之中,然后简单的系统生成代码式的编辑事件处理. 以下是,比较经典且简单的实用,后面的一篇较 ...

  10. iOS10 推送必看 UNNotificationContentExtension

    来源:徐不同(@2016徐小爷) 链接:http://www.jianshu.com/p/45933f5450a4 大伙久等啦~这绝对是最全最详细的 UNNotificationContentExte ...