Fixus
Fixus

Reputation: 4641

how to get latitude and longitude from Windows 8 Metro Bing Map app

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

Answers (3)

Chadit
Chadit

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

Disco Banana
Disco Banana

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

Diogo Muller
Diogo Muller

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

Related Questions