Hi, I my code, I want to be able to zoom to a point or extent on the map. How can I do that?
The user can enter an address, and an API call returns the coordinats, which I am using to create a Graphic on the map:
MapPoint houseNumberToDisplay = new MapPoint(HouseNumberToMap.geometri.coordinates[0][0], HouseNumberToMap.geometri.coordinates[0][1], sr);
CreateGraphics(houseNumberToDisplay);
I got a Map property thats binding to the xmal:
<esri:MapView x:Name="MainMapView"
Map="{Binding Map}"
Grid.ColumnSpan="2"
Background="White"
GraphicsOverlays = "{Binding GraphicsOverlays}"
/>
I am trying to create a mapView object and set the Map object to the mapView object, so i can use the 'SetViewpointAsync' method. But I get this error message:
System.Windows.Markup.XamlParseException: ''Set property 'Esri.ArcGISRuntime.UI.Controls.MapView.Map' threw an exception.' Line number '79' and line position '23'.'
ArcGISRuntimeException: Object is already owned: Already owned.
What am I missing?
Solved! Go to Solution.
Note that you can't zoom to a point - you can pan to it, but to zoom, you'll need the overload of Viewpoint that takes a scale as well. This is because the point has an infinitely small size, so we wouldn't know how far to zoom in. It really depends on what the point represents (building, address, city, etc).
Wrt Already Owned exception: A map can only be active with one MapView at a time. Are you using the map with multiple mapviews? Also note that if the garbage collector hasn't had a chance to clear out the old mapview yet, the map might still be associated with the mapview. A good way around this is when a mapview closes, to unhook the map immediately from the mapview, so it's ready for use on the next one.
That is correct. Setting view points are view operations that require view-code. However the toolkit introduces a GeoViewController that gives you the ability to declare a view controller in your view model, and then bind that on to the mapview: https://github.com/Esri/arcgis-maps-sdk-dotnet-toolkit/blob/main/docs/geoviewcontroller.md
Example: https://github.com/Esri/arcgis-maps-sdk-dotnet-toolkit/blob/0d12057c95e3be6b2d693715fd2b1c1fd13e8b98...
Hi @Daniel4, you should be able to update the viewpoint by simply using your `MainMapView` object:
await MainMapView.SetViewpointAsync(new Viewpoint(geometry));
In your case you can use the `MapPoint` you have called "houseNumberToDisplay" as the geometry.
You can find more examples of this in our Change viewpoint sample.
I hope this helps!
Hi @HamishDuff thanks for your answer.
It seems like I can't access the 'MainMapView' object from my ViewModel. I got this red squiggles line
In the code-behinde of the xaml, I can access the MainMapView object, and zoom to a location like this
private void SearchAddressButton_Click(object sender, RoutedEventArgs e)
{
var sr = SpatialReference.Create(25832);
MapPoint mapCenterPoint = new MapPoint(725301.71, 6171291.95, sr);
MainMapView.SetViewpoint(new Viewpoint(mapCenterPoint, 200));
}
But all my logic is in the ViewModel.
That is correct. Setting view points are view operations that require view-code. However the toolkit introduces a GeoViewController that gives you the ability to declare a view controller in your view model, and then bind that on to the mapview: https://github.com/Esri/arcgis-maps-sdk-dotnet-toolkit/blob/main/docs/geoviewcontroller.md
Example: https://github.com/Esri/arcgis-maps-sdk-dotnet-toolkit/blob/0d12057c95e3be6b2d693715fd2b1c1fd13e8b98...
Note that you can't zoom to a point - you can pan to it, but to zoom, you'll need the overload of Viewpoint that takes a scale as well. This is because the point has an infinitely small size, so we wouldn't know how far to zoom in. It really depends on what the point represents (building, address, city, etc).
Wrt Already Owned exception: A map can only be active with one MapView at a time. Are you using the map with multiple mapviews? Also note that if the garbage collector hasn't had a chance to clear out the old mapview yet, the map might still be associated with the mapview. A good way around this is when a mapview closes, to unhook the map immediately from the mapview, so it's ready for use on the next one.