Create new Windows 10 project and choose the map control from toolbox list.
Geo Location API allows easy to access a device’s current location. Here we will see how to get the longitude and latitude co-ordinates of the device.
Firstly, enable the location capabilities to allow the device to access the location like the following screen,
Next add the namespace in you code page.
- using Windows.Devices.Geolocation;
- using Windows.Services.Maps;
Create new instance for Geolocator using that we can retrieve the position of our device. Then add the location coordinates points to our map control.
Write the following code to set your position to the map control.
- var geolocator = new Geolocator();
- var position = await geolocator.GetGeopositionAsync();
- mymap.Center = position.Coordinate.Point;
- mymap.ZoomLevel = 15;
Set the map zoom level according to your needs and you will get the co-ordinates of the current position.
Next we are going to get the current street address of the position using the coordinates.
You will get the following details also:
- BuildingFloor
- BuildingName
- BuildingRoom
- BuildingWing
- Continent
- Country
- CountryCode
- District
- FormattedAddress
- Neighborhood
- PostCode
- Region
- RegionCode
- Street
- StreetNumber
- Town
Write the following code to get the address of the street.
- var mapLocation = await MapLocationFinder.FindLocationsAtAsync(position.Coordinate.Point);
- if (mapLocation.Status == MapLocationFinderStatus.Success)
- {
- string address = mapLocation.Locations[0].Address.StreetNumber + " " + mapLocation.Locations[0].Address.Street;
- }
- else
- {
- dialog.Content = "Not Found";
- }
Now run the app and see the output like the following screen.