2013-12-19 145 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

謝謝...這將是一個很容易! –

相關問題