您的位置:首页 > 其它

WP8: 在Bing map上圈出固定区域大小

2014-11-06 16:11 274 查看
先上要达到的效果图



说明: 这个区域会跟随缩放级别自动变化,显示实际区域大小

扩展GeoCoordinate的方法

using System;
using System.Collections.Generic;
using System.Device.Location;

namespace WpWinNl.Utilities
{
public static class GeoCoordinateExtensions
{
public static GeoCoordinate GetAtDistanceBearing(this GeoCoordinate point,
double distance, double bearing)
{
const double degreesToRadian = Math.PI / 180.0;
const double radianToDegrees = 180.0 / Math.PI;
const double earthRadius = 6378137.0;

var latA = point.Latitude * degreesToRadian;
var lonA = point.Longitude * degreesToRadian;
var angularDistance = distance / earthRadius;
var trueCourse = bearing * degreesToRadian;

var lat = Math.Asin(
Math.Sin(latA) * Math.Cos(angularDistance) +
Math.Cos(latA) * Math.Sin(angularDistance) * Math.Cos(trueCourse));

var dlon = Math.Atan2(
Math.Sin(trueCourse) * Math.Sin(angularDistance) * Math.Cos(latA),
Math.Cos(angularDistance) - Math.Sin(latA) * Math.Sin(lat));

var lon = ((lonA + dlon + Math.PI) % (Math.PI * 2)) - Math.PI;

var result = new GeoCoordinate(lat * radianToDegrees, lon * radianToDegrees);

return result;
}
public static IList<GeoCoordinate> GetCirclePoints(this GeoCoordinate center,
double radius, int nrOfPoints = 50)
{
var angle = 360.0 / nrOfPoints;
var locations = new List<GeoCoordinate>();
for (var i = 0; i <= nrOfPoints; i++)
{
locations.Add(center.GetAtDistanceBearing(radius, angle * i));
}
return locations;
}
}
}


wp8 调用(测试通过)

void MainPageLoaded(object sender, RoutedEventArgs e)
{
var location = new GeoCoordinate(52.181427, 5.399780);
MyMap.Center = location;
MyMap.ZoomLevel = 16;

var fill = Colors.Purple;
var stroke = Colors.Red;
fill.A = 80;
stroke.A = 80;
var circle = new MapPolygon
{
StrokeThickness = 2,
FillColor = fill,
StrokeColor = stroke,
StrokeDashed = false,
};

foreach( var p in location.GetCirclePoints(150))
{
circle.Path.Add(p);
}

MyMap.MapElements.Add(circle);
}

这就可以显示方圆150米的区域

对windows store来说,用法稍有不同,首先获取当前地理左边引用dll不同

using System;
using System.Collections.Generic;
#if WINDOWS_PHONE
using GeoCoordinate = System.Device.Location.GeoCoordinate;
#else
using GeoCoordinate=Bing.Maps.Location;
#endif

windows store 调用方法(未测试)

void MainPageLoaded(object sender, RoutedEventArgs e)
{
var location = new Location(52.181427, 5.399780);
MyMap.Center = location;
MyMap.ZoomLevel = 18;
var fill = Colors.Purple;
fill.A = 80;
var circle = new MapPolygon
{
FillColor = fill,
};

foreach (var p in location.GetCirclePoints(150))
{
circle.Locations.Add(p);
}

var layer = new MapShapeLayer();
layer.Shapes.Add(circle);

MyMap.ShapeLayers.Add(layer);
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  wp8 bingmap 附近区域