user3821206
user3821206

Reputation: 619

Value does not fall within the expected range. in Bing Maps

I am trying to send the lattitude,longitude from my web service to the bing Maps,this is my code for the map:

<Grid>
    <Grid.Resources>
    <CollectionViewSource x:Name="LocationList"/>
    <DataTemplate x:Key="LogoTemplate">
            <bm:Pushpin>
                <bm:MapLayer.Position>
                    <bm:Location Latitude="{Binding local_latit}" Longitude="{Binding local_longi}"></bm:Location>
                </bm:MapLayer.Position>
            </bm:Pushpin>
        </DataTemplate>
    </Grid.Resources>
    <bm:Map Credentials="...">
        <bm:Map.Children>
            <bm:MapItemsControl x:Name="ListOfItems" 
             ItemsSource="{Binding Source={StaticResource LocationList}}"
             ItemTemplate="{StaticResource LogoTemplate}">
            </bm:MapItemsControl>
        </bm:Map.Children>
    </bm:Map>
</Grid>

this is my code behind:

private async void Page_Loaded(object sender, RoutedEventArgs e)
{
    UriString3 = "URL";
    var http = new HttpClient();
    http.MaxResponseContentBufferSize = Int32.MaxValue;
    var response = await http.GetStringAsync(UriString3);
    var rootObject =  JsonConvert.DeserializeObject<MyApp.Models.RootObject>(response);
    var se = new Location();
    se.Latitude = rootObject.local_latit;
    se.Longitude = rootObject.local_longi;
    LocationList.Source = se; //exception at this line
}

this is my model Class:

public class RootObject
{
    .....
    public double local_longi { get; set; }
    public double local_latit { get; set; }
}

I get this exception:

"Value does not fall within the expected range."

so any help please,to send the lattitude,langitude to the map thanks for help

Update: this is the value of lattituden,longitude when debugging:

enter image description here

Upvotes: 0

Views: 549

Answers (3)

rbrundritt
rbrundritt

Reputation: 17954

In your other post you provide some sample data you are using. In my response there I mentioned that it looks like you have some invalid values in some of your local_latit and local_longi values. Latitude values should be between -90 and 90, and longitude values between -180 and 180. If these values are outside of this range, you will see the error you mentioned above.

In your update you provide an image of one that has valid values, but in one of your other posts you provided an array of data and there was invalid values in there.

That said, now looking at your Page_Load method I see that you are setting the LocationList.Source value to equal a single Location, yet your XAML is expecting your local_latit and local_longi values. Also, the source should be an ObservableCollection, not a single Location object.

Upvotes: 1

N Jacobs
N Jacobs

Reputation: 341

A CollectionViewSource expects a collection and not a single instance. Add the RootObject to a collection (for example a List<>) will solve the problem.

Upvotes: 1

Jens Meinecke
Jens Meinecke

Reputation: 2940

What are the values of local_latit and local_longi?

Examine these values when you get the exception.

Longitude is by it's very nature limited to -180 to 180 and Latitude to -90 to 90.

A common mistake would be to have swapped Longitude and Latitude by accident

Upvotes: 1

Related Questions