前些日子在做景区App遇到需求,使用手绘图片做一个简易的地图,支持放大缩小平移以及显示景点Mark,安卓上可以使用一个叫做“mAppWidget”的开源库来完成,WP上有人建议用ArcGIS,但是考虑到只是简单的放大缩小平移以及展示Mark标记,没必要再去花费精力去大费周折的研究ArcGIS,于是就各种搜索WP下的放大缩小平移图片的文章,还好很庆幸找到一篇(连接地址给忘记了原作者如果看到的话希望能提醒下给加上原链接)。解决了对图片放大缩小平移的问题,剩下的就是在上面添加Mark标记点了以及解决每次放大缩小后的Mark重新定位的问题。不多说。上代码!

前端XAML:

    <!--LayoutRoot 是包含所有页面内容的根网格-->
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Padding="12,12,0,18" Grid.Row="0" Style="{StaticResource SinglePageHeardStyle}" >
<TextBlock FontSize="28" Text="地图导览"/>
</Border> <Grid x:Name="ContentPanel" Grid.Row="1" >
<ViewportControl x:Name="Viewport" HorizontalAlignment="Left" VerticalAlignment="Top">
<Canvas x:Name="can_Map" Width="480" Height="800">
<Image x:Name="Image" Stretch="Uniform"
CacheMode="BitmapCache"
ManipulationStarted="Viewport_ManipulationStarted"
ManipulationDelta="Viewport_ManipulationDelta"
ManipulationCompleted="Viewport_ManipulationCompleted" >
</Image>
</Canvas>
</ViewportControl>
</Grid>
</Grid>

  

后台cs:

    public partial class MapPage : PhoneApplicationPage
{
private const double IMAGE_MAX_WIDTH = 2048; private const double IMAGE_MAX_HEIGHT = 2048; private double _width = 0; private double _height = 0; private double _scale = 1.0;
private Point _relativeCenter;
private bool _pinching = false; private WriteableBitmap _writeableBitmap; public MapPage()
{
InitializeComponent();
this.Loaded += MainPage_Loaded;
} private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var bitmapImage = GetImgForRes();
_writeableBitmap = new WriteableBitmap(bitmapImage); double scale; if (_writeableBitmap.PixelWidth > _writeableBitmap.PixelHeight)
{
scale = IMAGE_MAX_WIDTH / _writeableBitmap.PixelWidth;
}
else
{
scale = IMAGE_MAX_HEIGHT / _writeableBitmap.PixelHeight;
} _width = _writeableBitmap.PixelWidth * scale;
_height = _writeableBitmap.PixelHeight * scale; this.Image.Source = _writeableBitmap;
ConfigureViewport(); App.MapViewModel.GetSp(App.GetUserAcction());
App.MapViewModel.GetMapSpCompleted += () =>
{
foreach (var item in App.MapViewModel.ScencParentByMobile.Data)
{
//计算当前每个Mark的宽高与当前手绘图片的宽高缩放比例
item.Sh = (item.MapY -30) / _height;
item.Sw = (item.MapX - 30) / _width;
Image el = new Image();
//SetLeft 和SetTop 是根据元素的(0,0)点坐标开始的 所以放置坐标Mark的时候要减去元素宽的一半和元素高度
Canvas.SetLeft(el, (Image.Width * item.Sw) - 30);
Canvas.SetTop(el, (Image.Height * item.Sh) - 60);
el.Height = el.Width = 60;
el.Stretch = Stretch.Uniform;
//根据业务区选择加载哪个图片
if (item.IsFlag == 1)
el.Source = new BitmapImage(new Uri("/MapPage/scenic_pointer_gril@2x.png", UriKind.RelativeOrAbsolute));
else
el.Source = new BitmapImage(new Uri("/MapPage/scenic_pointer@2x.png", UriKind.RelativeOrAbsolute));
el.Tag = item.SCID + "|" + item.Sw + "|" + item.Sh; //记录每个Mark的id 宽高缩放比例到Tag以备用
can_Map.Children.Add(el);
}
};
} private void ConfigureViewport()
{
if (_width < _height)
{
_scale = Viewport.ActualHeight / _height;
}
else
{
_scale = Viewport.ActualWidth / _width;
} Image.Width = _width * _scale;
Image.Height = _height * _scale; Viewport.Bounds = new Rect(0, 0, Image.Width, Image.Height);
Viewport.SetViewportOrigin(new Point(Image.Width / 2 - Viewport.Viewport.Width / 2, Image.Height / 2 - Viewport.Viewport.Height / 2)); } private static BitmapImage GetImgForRes()
{
string fileName = "/QWCProject;component/MapPage/map.jpg"; using (Stream stream = Application.GetResourceStream(new Uri(fileName, UriKind.Relative)).Stream)
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
return bitmapImage;
}
} private void Viewport_ManipulationStarted(object sender, System.Windows.Input.ManipulationStartedEventArgs e)
{
if (_pinching)
{
e.Handled = true; CompletePinching();
}
} private void CompletePinching()
{
_pinching = false; double sw = Image.Width / _width;
double sh = Image.Height / _height; _scale = Math.Min(sw, sh); //完成缩放放大后获取所有Mark点集合
var markList = WPTools.VisualTreeTool.GetChildObjects<Image>(can_Map, typeof(Image));
foreach (var item in markList)
{
//过滤手绘图片
if (item.Tag == null)
continue;
//根据当前Image(手绘图片)的宽高乘以该Mark的坐标缩放比例得出缩放放大后的每个Mark的坐标
Canvas.SetLeft(item, (Image.Width * double.Parse(item.Tag.ToString().Split('|')[1])) - 30);
Canvas.SetTop(item, (Image.Height * double.Parse(item.Tag.ToString().Split('|')[2])) - 60);
item.Visibility = Visibility.Visible;
}
} private void Viewport_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
{
if (e.PinchManipulation != null)
{
//获取所有Mark 在缩放放大时候隐藏所有Mark
var markList = WPTools.VisualTreeTool.GetChildObjects<Image>(can_Map, typeof(Image));
foreach (var item in markList)
{
if (item.Tag == null)
continue;
item.Visibility = Visibility.Collapsed;
} e.Handled = true; if (!_pinching)
{
_pinching = true; _relativeCenter = new Point(e.PinchManipulation.Original.Center.X / Image.Width, e.PinchManipulation.Original.Center.Y / Image.Height);
} double w, h; if (_width < _height)
{
h = _height * _scale * e.PinchManipulation.CumulativeScale;
h = Math.Max(Viewport.ActualHeight, h);
h = Math.Min(h, _height); w = h * _width / _height;
}
else
{
w = _width * _scale * e.PinchManipulation.CumulativeScale;
w = Math.Max(Viewport.ActualWidth, w);
w = Math.Min(w, _width); h = w * _height / _width;
} Image.Width = w;
Image.Height = h; Viewport.Bounds = new Rect(0, 0, w, h); GeneralTransform transform = Image.TransformToVisual(Viewport);
Point p = transform.Transform(e.PinchManipulation.Original.Center); double x = _relativeCenter.X * w - p.X;
double y = _relativeCenter.Y * h - p.Y; if (w < _width && h < _height)
{
System.Diagnostics.Debug.WriteLine("Viewport.ActualWidth={0} .ActualHeight={1} Origin.X={2} .Y={3} Image.Width={4} .Height={5}",
Viewport.ActualWidth, Viewport.ActualHeight, x, y, Image.Width, Image.Height); Viewport.SetViewportOrigin(new Point(x, y)); }
}
else if (_pinching)
{
e.Handled = true; CompletePinching();
}
} private void Viewport_ManipulationCompleted(object sender, System.Windows.Input.ManipulationCompletedEventArgs e)
{
if (_pinching)
{
e.Handled = true; CompletePinching();
}
}
}

  

另外还有个获取指定元素下的所有指定类型子元素的方法:

      public static List<T> GetChildObjects<T>(DependencyObject obj, Type typename) where T : FrameworkElement
{
DependencyObject child = null;
List<T> childList = new List<T>(); for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
{
child = VisualTreeHelper.GetChild(obj, i); if (child is T && (((T)child).GetType() == typename))
{
childList.Add((T)child);
}
childList.AddRange(GetChildObjects<T>(child, typename));
}
return childList;
}

  

要注意的是:地图图片文件的生成资源要改为:Resource  不然没法获取到图片!坐标信息是从服务器获取的,在MainPage_Loaded()方法内—— App.MapViewModel.GetSp()方法

最后上个演示视频(手绘地图上用户去过的景点的Mark会出现卡通人物头像 没去过的则不出现卡通人物头像):

WindowsPhone开发—— 使用手绘图片做景区导览地图的更多相关文章

  1. AI应用开发实战 - 手写识别应用入门

    AI应用开发实战 - 手写识别应用入门 手写体识别的应用已经非常流行了,如输入法,图片中的文字识别等.但对于大多数开发人员来说,如何实现这样的一个应用,还是会感觉无从下手.本文从简单的MNIST训练出 ...

  2. 给手绘图着色(添加颜色或色彩):CVPR2020论文点评

    给手绘图着色(添加颜色或色彩):CVPR2020论文点评 Learning to Shade Hand-drawn Sketches 论文链接:https://arxiv.org/pdf/2002.1 ...

  3. Windows Phone开发(21):做一个简单的绘图板

    原文:Windows Phone开发(21):做一个简单的绘图板 其实我们今天要说的就是一个控件--InkPresenter,这个控件并不是十分强大,没办法和WPF中的InkCanvas相比,估计在实 ...

  4. roughViz 一个可重用,功能强大的手绘图表组件

    前段时间介绍过一个chart.xkcd 的手绘图表组件,roughViz 是另外一个,同时也提供了 比较多的图表类型,api 参考文档也比较全 支持的图表类型 Bar Horizontal Bar D ...

  5. 如何在WindowsPhone Bing Map控件中显示必应中国中文地图、谷歌中国中文地图。

    原文:如何在WindowsPhone Bing Map控件中显示必应中国中文地图.谷歌中国中文地图. 最近正好有点业余时间,所以在做做各种地图.Bing Map控件本身就能显示必应地图,但是很遗憾微软 ...

  6. C# Windows Phone 8 WP8 高级开发,制作不循环 Pivot ,图片(Gallery)导览不求人! 内附图文教学!!

    原文:C# Windows Phone 8 WP8 高级开发,制作不循环 Pivot ,图片(Gallery)导览不求人! 内附图文教学!! 一般我们在开发Winodws Phone APP 的时候往 ...

  7. 3D Touch开发全面教程之Peek and Pop - 预览和弹出

    ## 3D Touch开发全面教程之Peek and Pop - 预览和弹出 --- ### 了解3D Touch 在iPhone 6s和iPhone 6s Plus中Apple引入了3D Touch ...

  8. 记一次IE浏览器做图片预览的坑

    随便写写吧,被坑死了 IE 10 及 IE10以上,可以使用FileReader的方式,来做图片预览,加载本地图片显示 IE 9 8 7 没有FileReader这个对象,所以只能使用微软自己的东西来 ...

  9. AI应用开发实战 - 手写算式计算器

    扩展手写数字识别应用 识别并计算简单手写数学表达式 主要知识点 了解MNIST数据集 了解如何扩展数据集 实现手写算式计算器 简介 本文将介绍一例支持识别手写数学表达式并对其进行计算的人工智能应用的开 ...

随机推荐

  1. Python进程、线程、协程

    进程和线程的解释 进程(process)和线程(thread)是操作系统的基本概念,计算机的核心是CPU,它承担了所有的计算任务: 单个CPU一次只能运行一个任务,代表单个CPU总是运行一个进程,其他 ...

  2. VS2012解决方案的设置

    用VS开发项目时,一个解决方案可以包含多个项目,在此我记录一下: 1.首先我新建一个Win32Demo的解决方案: 2.勾选"空项目": 3.新建完之后,会默认生成一个Win32D ...

  3. SpringBoot集成jsp(附源码)+遇到的坑

    1.大体步骤 (1)       创建Maven web project: (2)       在pom.xml文件添加依赖: (3)       配置application.properties支持 ...

  4. 从webRoot中下载Excel

    @RequestMapping("downLoad") public void downLoad(Offsupervise off1,HttpServletRequest requ ...

  5. [Prodinner项目]学习分享_第三部分_Service层(业务逻辑层)

    前两节讲到怎样生成一个Model和怎样将Model映射到数据库,这一节将讲到业务逻辑层,也就是Service层. 1.Prodinner架构已经构建好的,基本的增删改查. 假设,我现在想操作第二节中讲 ...

  6. Delphi IDE 设置

    显示编译进度 Tools/Environment Options

  7. [java基础]循环结构1

    [java基础]循环结构1 循环结构:for循环,while循环,do_while循环在,增强型for循环 /** 文件路径:G:\JavaByHands\循环语句\ 文件名称:WhileTest.j ...

  8. 【Asp.net之旅】--数据绑定控件之Repeater

    http://blog.csdn.net/zhang_xinxiu/article/details/21872433

  9. php null o false ''

    php中很多还不懂php中0,"",null和false之间的区别,这些区别有时会影响到数据判断的正确性和安全性,给程序的测试运行造成很多麻烦.先看一个例子: <? $str ...

  10. oracle连接本地数据库

    连接方式: 通过SQL Developer进行连接: 通过sql plus 进行连接: SQL Developer进行连接1.安装Oracle 11g会自带一个叫做SQL Developer的工具,它 ...