简体   繁体   中英

Using named locations as pushpins in WP8 Map control

Is it possible to use a location name for a pushpin rather than coordinates (eg London Eye - rather than the coordinates of it)?

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

Yes you can, but for that you have to create all the details of the pushpin in your viewmodel . So that you can easily bind it to the pushpin control.

Something like this:

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

You can refer these which could help you:

How to display a DataBound Pushpin on Windows Phone

Binding pushpins to Bing maps

Hope it helps!

I can give you single line of code but much explanation is needed for that. Hence am ready to provide you with full solution. Here you go. First add these xaml code.

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

Then in your .cs file, add these codes very carefully.

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

In between, first create Unique App Id for your application by registering your app in BingMapsPortal and use that AppId in the code.

Then, Right Click Reference in Solution Explorer and select Add Service Reference and paste the below link and select Ok. http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc .

This adds service references in your application and finally it works. Happy Coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM