is it possible to get the geolocation of the point where i clicked on a bing Map
in c#?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Alright, since you're in WinRT in C#, you're using the Bing Maps Windows Store App control. Add the map into your XAML markup:
<Page
x:Class="Win8PublicApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:m="using:Bing.Maps">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<m:Map Name="MyMap" Credentials="YOURKEY"/>
</Grid>
</Page>
Then you can do this to add pushpin where you have clicked:
public MainPage()
{
this.InitializeComponent();
this.MyMap.PointerPressedOverride += MyMap_PointerPressedOverride;
}
void MyMap_PointerPressedOverride(object sender, PointerRoutedEventArgs e)
{
Bing.Maps.Location l = new Bing.Maps.Location();
this.MyMap.TryPixelToLocation(e.GetCurrentPoint(this.MyMap).Position, out l);
Bing.Maps.Pushpin pushpin = new Bing.Maps.Pushpin();
pushpin.SetValue(Bing.Maps.MapLayer.PositionProperty, l);
this.MyMap.Children.Add(pushpin);
}
回答2:
The Bing Maps API makes this pretty easy.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
<script type="text/javascript">
var map = null;
function GetMap()
{
// Initialize the map
map = new Microsoft.Maps.Map(document.getElementById("myMap"),
{credentials:"Bing Maps Key"});
//Add handler for the map click event.
Microsoft.Maps.Events.addHandler(map, 'click', displayLatLong);
}
function displayLatLong(e) {
if (e.targetType == "map") {
var point = new Microsoft.Maps.Point(e.getX(), e.getY());
var loc = e.target.tryPixelToLocation(point);
document.getElementById("textBox").value= loc.latitude + ", " + loc.longitude;
}
}
</script>
</head>
<body onload="GetMap();">
<div id='myMap' style="position:relative; width:400px; height:400px;"></div><br>
<b>Click the map to display the coordinate values at that point.</b><br>
Latitude, Longitude:
<input id='textBox' type="text" style="width:250px;"/>
</body>
</html>
Source: Bing Maps API on MSDN