如何在WP8中将事件处理程序添加到MapPolygons



我目前有一个带有地图的Windows Phone 8应用程序,我正在向地图中添加MapPolygon。

我想在MapPolygon中添加一个事件处理程序,这样我就可以"点击"MapPolygon,它就会导航到另一个页面。

然而,MapElements没有内置的"手势"处理。

以下是我尝试过的:

  • 通过NuGet,我添加了Windows Phone工具包包
  • 我尝试创建一个"手势服务",并将"点击"事件/手势应用于MapPolygon

下面是代码的样子:

var poly_gesture = GestureService.GetGestureListener(poly);
poly_gesture.Tap += new EventHandler<GestureEventArgs>(Poly_Tap);

private void Poly_Tap(object sender, GestureEventArgs e)
{
// Handle the event here.
}

问题是Poly_Tap方法永远不会被激发。"手势服务"显示一个警告,尽管我安装了Windows Phone Toolkit包,但它现在已经"过时"了。有没有一种新的/更好的/不同的方式来为MapElement创建手势/事件处理程序?

感谢

这是我提出的解决方案:

// A method that gets called when a user clicks on a map.
private void map1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
// When a user clicks the map, turn this into a GPS location.
var point = e.GetPosition(map1);
var location = map1.ConvertViewportPointToGeoCoordinate(point);
// Make variables to store the latitude and longitude.
double smallest_lat = 0; 
double largest_lat = 0;
double smallest_long = 0;
double largest_long = 0;
// Loop through each coordinate of your Polygon
foreach (GeoCoordinate gc in poly.Path)
{
// Store the values of the first GPS location
//  into the variables created above
if (smallest_lat == 0)
{
smallest_lat = gc.Latitude;
largest_lat = gc.Latitude;
smallest_long = gc.Longitude;
largest_long = gc.Longitude;
}
// For each new GPS coordinate check to see if it is 
//  smaller/larger than the previous one stored.
if (gc.Latitude < smallest_lat) { smallest_lat = gc.Latitude; }
if (gc.Latitude > largest_lat) { largest_lat = gc.Latitude; }
if (gc.Longitude < smallest_long) { smallest_long = gc.Longitude; }
if (gc.Longitude > largest_long) { largest_long = gc.Longitude; }
}
// Call the "isWithin" function to identify if where
//  the user clicked is within the Polygon you're looking at. 
btnPoly.Content = isWithin(location, smallest_lat, largest_lat, smallest_long, largest_long);
}
// A function that determines if a user clicks withing a specified rectangle
public Boolean isWithin(GeoCoordinate clicked_gc, double smallest_lat, double largest_lat, double smallest_long, double largest_long)
{
bool slat = clicked_gc.Latitude >= smallest_lat;
bool llat = clicked_gc.Latitude <= largest_lat;
bool slong = clicked_gc.Longitude >= smallest_long;
bool llong = clicked_gc.Longitude <= largest_long;
// Returns 'true' if the user clicks in the defined coordinates
// Returns 'false' if the user doesn't click in the defined coordinates
return slat && llat && slong && llong;
}