【问题标题】:Using named locations as pushpins in WP8 Map control在 WP8 地图控件中使用命名位置作为图钉
【发布时间】:2014-07-24 16:36:55
【问题描述】:

是否可以为图钉使用位置名称而不是坐标(例如伦敦眼 - 而不是它的坐标)?

XAML

    <maps:Map x:Name="MyMap" Center="47.6097, -122.3331" ZoomLevel="10" />

C#

    MapLayer layer1 = new MapLayer();
    Pushpin pushpin1 = new Pushpin();
    pushpin1.GeoCoordinate = new GeoCoordinate(51.5033, -0.1197);
    pushpin1.Content = "Pin 1";

    MapOverlay overlay1 = new MapOverlay();
    overlay1.Content = pushpin1;
    overlay1.GeoCoordinate = new GeoCoordinate(51.5033, -0.1197);
    layer1.Add(overlay1);

    WC_WATMap.Layers.Add(layer1);

【问题讨论】:

    标签: c# xaml windows-phone-8 windows-phone


    【解决方案1】:

    是的,您可以,但为此您必须在您的viewmodel 中创建pushpin 的所有详细信息。以便您可以轻松地将其绑定到图钉控件。

    类似这样的:

      <m:Pushpin Location="{Binding Location}" />
    

    你可以参考这些对你有帮助的:

    How to display a DataBound Pushpin on Windows Phone

    Binding pushpins to Bing maps

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      我可以给你一行代码,但需要很多解释。因此,我准备为您提供完整的解决方案。干得好。首先添加这些xaml代码。

      <my:Map Margin="12,0,12,0" Name="MapControl" LogoVisibility="Collapsed" ScaleVisibility="Visible" CopyrightVisibility="Collapsed" ZoomBarVisibility="Visible">
           <toolkit:GestureService.GestureListener>
                <toolkit:GestureListener Tap="GestureListener_Tap"/>
           </toolkit:GestureService.GestureListener>
           <my:Pushpin Name="Pushpin"/>
      </my:Map>
      

      然后在你的 .cs 文件中,非常小心地添加这些代码。

      private void GestureListener_Tap(object sender, GestureEventArgs e)
          {
              try
              {
                  var point = new Point(e.GetPosition(MapControl).X, e.GetPosition(MapControl).Y);
                  var location = MapControl.ViewportPointToLocation(point);
                  Pushpin.Location = location;
                  Pushpin.Content = "loading...";
                  var request = new ReverseGeocodeRequest()
                  {
                      Location = location,
                      Credentials = new Credentials()
                      {
                          ApplicationId = "YourAPP_ID"
                      }
                  };
      
                  // prepare the service
                  GeocodeServiceClient service = new GeocodeServiceClient(new BasicHttpBinding(), new EndpointAddress("http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc"));
                  service.ReverseGeocodeCompleted += service_ReverseGeocodeCompleted;
                  service.ReverseGeocodeAsync(request);
              }
              catch (Exception)
              {
                  MessageBox.Show("Couldn't communicate with server");
              }
          }
      
          void service_ReverseGeocodeCompleted(object sender, ReverseGeocodeCompletedEventArgs e)
          {
              if (e.Result.Results.Count > 0)
                  Pushpin.Content = e.Result.Results[0].Address.FormattedAddress;
              else
                  Pushpin.Content = "Invalid";
          }
      

      在这两者之间,首先通过在BingMapsPortal 中注册您的应用程序为您的应用程序创建唯一的应用程序 ID,并在代码中使用该 AppId。

      然后,在解决方案资源管理器中右键单击参考并选择添加服务参考并粘贴以下链接并选择确定。 http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc.

      这会在您的应用程序中添加服务引用,最终它可以工作。快乐编码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多