In one of my app, I have to give feature of drawing route direction with Bing maps. I found an example in JavaScript, but I want the same in C#/XAML. So Richard Brundritt helped me by providing the Bing Maps REST Service helper class, with that I managed to do it. Here I am posting the code. All REST links URL Templates cam be found here. Thanks to Richard Brundritt :)
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
Uri geocodeRequest = new Uri("http://dev.virtualearth.net/REST/V1/Routes/Driving?o=json&wp.0=28.608280181884766,77.200813293457031&wp.1=19.072799682617188,72.882598876953125&optmz=distance&rpo=Points&key=YOUR_BING_MAP_KEY_HERE"); //Route Ahmedabad to Mumbai
//Make a request and get the response
BingMapsRESTService.Response r = await GetResponse(geocodeRequest);
MapPolyline routeLine = new MapPolyline();
routeLine.Locations = new LocationCollection();
routeLine.Color = Colors.Blue;
routeLine.Width = 5.0;
int bound = ((BingMapsRESTService.Route)(r.ResourceSets[0].Resources[0])).RoutePath.Line.Coordinates.GetUpperBound(0);
for (int i = 0; i < bound; i++)
{
routeLine.Locations.Add(new Location
{
Latitude = ((BingMapsRESTService.Route)(r.ResourceSets[0].Resources[0])).RoutePath.Line.Coordinates[i][0],
Longitude = ((BingMapsRESTService.Route)(r.ResourceSets[0].Resources[0])).RoutePath.Line.Coordinates[i][1]
});
}
MapShapeLayer shapeLayer = new MapShapeLayer();
shapeLayer.Shapes.Add(routeLine);
MyMap.ShapeLayers.Add(shapeLayer);
}
private async Task<Response> GetResponse(Uri uri)
{
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
var response = await client.GetAsync(uri);
using (var stream = await response.Content.ReadAsStreamAsync())
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response));
return ser.ReadObject(stream) as Response;
}
}
No comments:
Post a Comment