2013-12-19 93 views
1

我目前已经有了一个Windows Phone 8应用程序和一个地图,我将MapPolygon添加到地图中。如何将事件处理程序添加到WP8中的MapPolygons?

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

但是,没有内置MapElement的'手势'处理。

这里是我试过:

  • 通过的NuGet我加入了Windows Phone工具包的包
  • 我试图创建一个“GestureService”和应用“点击”事件/姿态向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. 
} 

问题为t Poly_Tap方法永远不会被触发。 'GestureService'显示一条警告,即使我安装了Windows Phone Toolkit软件包,它现在'已过时'。是否有创建MapElement的手势/事件处理程序的新/更好/不同的方式?

感谢

回答

0

这里是我想出了解决方案:

// 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; 
    } 
+1

有,这是否对你的API:GetMapElementsAt(点)。它将返回给定位置的所有MapElement。 http://msdn.microsoft.com/en-us/library/windowsphone/develop/microsoft.phone.maps.controls.map.getmapelementsat(v=vs.105).aspx – user8709

+0

谢谢...这将是一个很容易! –

相关问题