//If I place a pushpin in the bay(sea), it still draw a polyline on the land closest to the pin. I am not too sure how to fix this.
private void MapTapped(object sender, TappedRoutedEventArgs e)
{
Pushpin pin = new Pushpin();
var position = e.GetPosition(this.myMap);
Bing.Maps.Location location;
myMap.TryPixelToLocation(position, out location);
MapLayer.SetPosition(pin, location);
pin.Tag = count.ToString();
this.myMap.Children.Add(pin);
this.myMap.SetView(location);
this.GetDirections(location);
}
private async void GetDirections(Bing.Maps.Location location)
{
this.WayPoints.Add(string.Format("{0}, {1}", location.Latitude, location.Longitude));
if (this.WayPoints.Count < 2) return;
HttpClient client = new HttpClient();
StringBuilder builder = new StringBuilder("http://dev.virtualearth.net/REST/V1/Routes/Driving?o=xml&");
for (int index = 0; index < this.WayPoints.Count; index++)
{
builder.Append(string.Format("wp.{0}={1}&", index, this.WayPoints[index]));
}
builder.Append("routePathOutput=Points&avoid=Tolls&key=");
builder.Append(myMap.Credentials);
try
{
response = await client.GetAsync(builder.ToString());
response.EnsureSuccessStatusCode();
}
catch
{
ShowMessage("Route cannnot be found");
}
try{
Stream stream = await response.Content.ReadAsStreamAsync();
XDocument document = XDocument.Load(stream);
var query = from p in document.Descendants(this.BingMapsNamespace + "Point")
select new
{
Latitude = p.Element(this.BingMapsNamespace + "Latitude").Value,
Longitude = p.Element(this.BingMapsNamespace + "Longitude").Value
};
MapPolyline polyline = new MapPolyline();
foreach (var point in query)
{
double latitude, longitude;
double.TryParse(point.Latitude, out latitude);
double.TryParse(point.Longitude, out longitude);
polyline.Locations.Add(new Location(latitude, longitude));
}
polyline.Color = Colors.Green;
polyline.Width = 8;
layer.Shapes.Add(polyline);
this.myMap.ShapeLayers.Add(layer);
var distance = (from d in document.Descendants(this.BingMapsNamespace + "TravelDistance")
select d).First().Value;
this.DistanceTextBlock.Text = string.Format("{0} Kms", distance.ToString());
}
catch
{
ShowMessage("Please try again.");
}
}