如何在C#中获取点击地图上的点击地理位置

时间:2013-02-15 17:23:00

标签: c# bing-maps

是否有可能获得我在c#中点击Map的点的地理位置?

2 个答案:

答案 0 :(得分:1)

好吧,因为你在C#中使用WinRT,所以你正在使用Bing Maps Windows Store App控件。 将地图添加到XAML标记中:

<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>

然后你可以这样做,在你点击的地方添加图钉:

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);
}

答案 1 :(得分:-1)

Bing Maps API使这很容易。

<!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>

来源:Bing Maps API on MSDN