Reputation: 4641
How can I get latitude and longitude from place where I tap/click on the map ?
I found example using GeoCoordinate class but it was sample from Win 7 mobile and this class does not exists in Win 8 Metro
I found the Geocoordinate class but it is totaly diffrent and also I can't convert my map to this class like in sample
My sample from Win 7 looks like this
Point p = e.GetPosition(this.MapMain);
GeoCoordinate geo = new GeoCoordinate();
geo = MapMain.ViewportPointToLocation(p);
As for now I've created new project, added bing map and set Tapped action. But I have no idea and can't find anywhere how to get coordinates based on tap
Upvotes: 0
Views: 2476
Reputation: 965
// --------- HTML/JS --------------
var mapCenter = map.getCenter();
var pos = map.tryPixelToLocation(new Microsoft.Maps.Point(location.clientX, location.clientY), Microsoft.Maps.PixelReference.control);
// --------- C# -------------------
Geolocator geolocator = new Geolocator();
var pos = await geolocator.GetGeopositionAsync();
Location location = new Location(pos.Coordinate.Latitude, pos.Coordinate.Longitude);
Upvotes: 0
Reputation: 102
First, add a event listener for click event:
Microsoft.Maps.Events.addHandler(map, 'click', mapClick);
Then in event handler:
function mapClick(e) {
if (e.targetType == "map") {
//Get x and y
var point = new Microsoft.Maps.Point(e.getX(), e.getY());
//Convert map point to location
var location = e.target.tryPixelToLocation(point);
console.log(location.longitude);
console.log(location.latitude);
}
}
Upvotes: 0
Reputation: 139
Try this:
Point p = e.GetPosition(this.MainMap);
Location location = new Location();
MainMap.TryPixelToLocation(p, out location);
The location variable will have the latitude and longitude values.
Upvotes: 1