Я разрабатываю приложение для Windows Phone, в котором мне нужно показать карты для определения местоположения двух разных мест и показать маршрут между этими двумя точками, а также направления.
Получаю адресную строку TO и FROM от веб-службы json, которую мне нужно использовать для отправки запроса местоположений в GeoCodeService.
Для достижения вышеуказанного я использую Bing Maps - «GeoCodeService», «GeoRouteService» и пытаюсь использовать следующий код. Во время работы приложения я получаю NullReferenceException
при получении ответа на callBack от GeoCodeService.
Поскольку я совершенно новичок в разработке приложений для Windows Phone, я не использую полный шаблон MVVM. Однако я мог получить данные из веб-службы Json, но затем не смог получить результат от GeoCodeService, чтобы связать их с моей картой.
Вот мой код xaml:
<controls:PivotItem Header="Map">
<StackPanel Orientation="Vertical">
<maps:Map x:Name="bingMap"
Center="50.851041,4.361572"
ZoomLevel="10"
CredentialsProvider="{StaticResource MapCredentials}">
<maps:MapPolyline Locations="{Binding RoutePoints, Converter={StaticResource locationConverter}}"
Stroke="#FF0000FF"
StrokeThickness="5" />
<maps:Pushpin Location="{Binding StartPoint, Converter={StaticResource locationConverter}}"
Content="Start" />
<maps:Pushpin Location="{Binding EndPoint, Converter={StaticResource locationConverter}}"
Content="End" />
</maps:Map>
<StackPanel Orientation="Horizontal"
VerticalAlignment="Bottom">
<Button Margin="80,0,0,0"
x:Name="btnputodo"
Click="btnputodo_Click"
HorizontalAlignment="Left"
FontFamily="Verdana"
FontSize="18"
VerticalAlignment="Bottom"
BorderBrush="Transparent"
Height="60"
Content="Pu to Do"
Width="150"
Background="White"
Foreground="Black"/>
<Button x:Name="btnmyloctopu"
Click="btnmyloctopu_Click"
HorizontalAlignment="Left"
FontFamily="Verdana"
FontSize="18"
VerticalAlignment="Bottom"
BorderBrush="Transparent"
Height="60"
Content="Loc to Pu"
Width="150"
Background="White"
Foreground="Black"/>
</StackPanel>
</StackPanel>
</controls:PivotItem>
<controls:PivotItem Header="Directions">
<ListBox ItemsSource="{Binding Itinerary, Converter={StaticResource itineraryConverter}}"
Grid.RowSpan="2"
ItemTemplate="{StaticResource ItineraryItemComplete}" />
</controls:PivotItem>
Ниже приведен мой код xaml.cs (код позади):
private Location toLocation;
private Location fromLocation;
private Address from;
public Address From
{
get { return from; }
set
{
from = value;
Change("From");
}
}
private Address to;
public Address To
{
get { return to; }
set
{
to = value;
Change("To");
}
}
private Location startPoint;
public Location StartPoint
{
get { return startPoint; }
set
{
startPoint = value;
Change("StartPoint");
}
}
private Location endPoint;
public Location EndPoint
{
get { return endPoint; }
set
{
endPoint = value;
Change("EndPoint");
}
}
private ObservableCollection<Location> routePoints;
public ObservableCollection<Location> RoutePoints
{
get { return routePoints; }
set
{
routePoints = value;
Change("RoutePoints");
}
}
private ObservableCollection<ItineraryItem> itinerary;
public ObservableCollection<ItineraryItem> Itinerary
{
get
{
return itinerary;
}
set
{
itinerary = value;
Change("Itinerary");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void Change(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
public event EventHandler RouteResolved;
private void RaiseRouteResolved()
{
if (RouteResolved != null)
RouteResolved(this, EventArgs.Empty);
}
private void btnputodo_Click(object sender, RoutedEventArgs e)
{
foreach (var dictionary in pickdrops)
{
if (dictionary.Key == "pickups")
{
locate = dictionary.Value;
this.From = new Address();
this.From.AddressLine = locate;
this.From.Locality = this.From.AdminDistrict = this.From.CountryRegion = this.From.District = this.From.PostalCode = this.From.PostalTown = string.Empty;
ResolveRoute();
}
if (dictionary.Key == "drops")
{
locate = dictionary.Value;
this.To= new Address();
this.To.AddressLine = locate;
this.To.Locality = this.To.AdminDistrict = this.To.CountryRegion = this.To.District = this.To.PostalCode = this.To.PostalTown = string.Empty;
ResolveRoute1();
}
}
}
public void ResolveRoute()
{
GetGeoLocation(From, (l) => fromLocation = l);
}
public void ResolveRoute1()
{
GetGeoLocation1(To, (l) => toLocation = l);
}
private void GetGeoLocation1(Address address, Action<Utils.WP7.Bing.BingGeo.GeocodeLocation> callBack)
{
var geoRequest = new GeocodeRequest();
geoRequest.Credentials = new Credentials();
geoRequest.Credentials.ApplicationId = App.BingApiKey;
geoRequest.Address = address;
var geoClient = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
geoClient.GeocodeCompleted += (s, e) =>
{
string statusCode = e.Result.ResponseSummary.StatusCode.ToString();
string ss = e.Result.Results == null ? "EMPTY" : "OK";
if (e.Result.Results != null && e.Result.Results.Count > 0)
{
string dd = e.Result.Results[0].Locations[0].Latitude.ToString();
}
// Am getting Exception at the below statement -->
callBack(e.Result.Results.FirstOrDefault().Locations.FirstOrDefault());
// <---
LocationLoaded();
};
geoClient.GeocodeAsync(geoRequest);
}
private void GetGeoLocation(Address address, Action<Utils.WP7.Bing.BingGeo.GeocodeLocation> callBack)
{
var geoRequest = new GeocodeRequest();
geoRequest.Credentials = new Credentials();
geoRequest.Credentials.ApplicationId = App.BingApiKey;
geoRequest.Address = address;
var geoClient = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
geoClient.GeocodeCompleted += (s, e) =>
{
callBack(e.Result.Results.FirstOrDefault().Locations.FirstOrDefault());
LocationLoaded();
};
geoClient.GeocodeAsync(geoRequest);
}
private void LocationLoaded()
{
if (fromLocation != null && toLocation != null)
{
var fromWaypoint = new Waypoint();
fromWaypoint.Description = "From";
fromWaypoint.Location = new Microsoft.Phone.Controls.Maps.Platform.Location();
fromWaypoint.Location.Altitude = fromLocation.Altitude;
fromWaypoint.Location.Latitude = fromLocation.Latitude;
fromWaypoint.Location.Longitude = fromLocation.Longitude;
var toWaypoint = new Waypoint();
toWaypoint.Description = "To";
toWaypoint.Location = new Microsoft.Phone.Controls.Maps.Platform.Location();
toWaypoint.Location.Altitude = toLocation.Altitude;
toWaypoint.Location.Latitude = toLocation.Latitude;
toWaypoint.Location.Longitude = toLocation.Longitude;
var routeRequest = new RouteRequest();
routeRequest.Credentials = new Microsoft.Phone.Controls.Maps.Credentials();
routeRequest.Credentials.ApplicationId = App.BingApiKey;
routeRequest.Waypoints = new System.Collections.ObjectModel.ObservableCollection<Waypoint>();
routeRequest.Waypoints.Add(fromWaypoint);
routeRequest.Waypoints.Add(toWaypoint);
routeRequest.Options = new RouteOptions();
routeRequest.Options.RoutePathType = RoutePathType.Points;
routeRequest.UserProfile = new Utils.WP7.Bing.BingRoute.UserProfile();
routeRequest.UserProfile.DistanceUnit = Utils.WP7.Bing.BingRoute.DistanceUnit.Kilometer;
var routeClient = new RouteServiceClient("BasicHttpBinding_IRouteService");
routeClient.CalculateRouteCompleted += new EventHandler<CalculateRouteCompletedEventArgs>(OnRouteComplete);
routeClient.CalculateRouteAsync(routeRequest);
}
}
private void OnRouteComplete(object sender, CalculateRouteCompletedEventArgs e)
{
if (e.Result != null && e.Result.Result != null && e.Result.Result.Legs != null & e.Result.Result.Legs.Any())
{
var result = e.Result.Result;
var legs = result.Legs.FirstOrDefault();
StartPoint = legs.ActualStart;
EndPoint = legs.ActualEnd;
RoutePoints = result.RoutePath.Points;
Itinerary = legs.Itinerary;
}
}