在WP8地图控件中使用命名位置作为图钉

本文关键字:位置 地图 WP8 控件 | 更新日期: 2023-09-27 18:14:34

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

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

在WP8地图控件中使用命名位置作为图钉

是的,你可以,但是你必须在你的viewmodel中创建pushpin的所有细节。这样你就可以很容易地将它绑定到图钉控件。

像这样:

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

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

如何在Windows Phone上显示数据绑定图钉

将图钉绑定到Bing地图

希望有帮助!

我可以给你一行代码,但需要更多的解释。因此,我准备为您提供完整的解决方案。给你。首先添加这些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,并在代码中使用该应用程序Id。

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

这在你的应用程序中添加了服务引用,最后它工作了。快乐的编码。