如何在WP8中向MapPolygons添加事件处理程序?

时间:2013-12-19 18:36:57

标签: windows-phone-8 here-api

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

我想在MapPolygon中添加一个事件处理程序,以便我可以“点击”MapPolygon并导航到另一个页面。

但是,MapElements没有内置'Gesture'处理功能。

这是我尝试过的:

  • 通过NuGet我添加了Windows Phone Toolkit包
  • 我尝试创建“GestureService”并将“Tap”事件/手势应用于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软件包,但'GestureService'仍然显示它现在已经“过时”了。是否有新的/更好/不同的方式为MapElement创建手势/事件处理程序?

由于

1 个答案:

答案 0 :(得分: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;
    }