Using GeoCode service to show addresses as PushPin(s) in Bing Map Silverlight Control.
Views (218)
For this first we need to add service reference to Geocode service to our Silverlight Application
http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc
On how to create Silverlight Application that uses Bing Map Control please refer to
https://nishantrana.wordpress.com/2011/03/05/using-bing-maps-silverlight-control/
Geocode service will return us the results found (locations) based on the address passed to it.
We can use the below sample code that displays the Pushpins on Bing Map based on the query (address) provided. (Here I am providing my permanent address)
public MainPage()
{
InitializeComponent(); string Address = "759 Jantanagar, Chandkheda, Gandhinagar, Gujarat, 382424";
GeocodeServiceClient myGeoCodeClient = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
// create an event hanler for GeocodeCompleted event as the service would be call asynchronously
myGeoCodeClient.GeocodeCompleted+=new EventHandler<GeocodeCompletedEventArgs>(myGeoCodeClient_GeocodeCompleted);
// create the request and pass the address to it
GeocodeRequest myGeoCodeRequest = new GeocodeRequest();
myGeoCodeRequest.Credentials = new Microsoft.Maps.MapControl.Credentials();
myGeoCodeRequest.Credentials.ApplicationId=((ApplicationIdCredentialsProvider)myMap.CredentialsProvider).ApplicationId;
myGeoCodeRequest.Query = Address;
// Pass the request to GeocodeAsyn method
myGeoCodeClient.GeocodeAsync(myGeoCodeRequest);
}
void myGeoCodeClient_GeocodeCompleted(object sender, GeocodeCompletedEventArgs e)
{
// create a map layer
MapLayer myMapLayer = new MapLayer();
myMap.Children.Add(myMapLayer);
// create a location collection class
LocationCollection myLocationColl=new LocationCollection();
foreach (GeocodeResult gr in e.Result.Results)
{
Pushpin myPushPin = new Pushpin();
// set it to first found location
myPushPin.Location = gr.Locations[0];
// add it to location collection
// which would be used to set the map's bound
myLocationColl.Add(myPushPin.Location);
// Add the drawn point to the route layer.
myMapLayer.Children.Add(myPushPin);
}
var bounds = new LocationRect(myLocationColl);
myMap.SetView(bounds);
}
}
This is how it looks

Bye.
Filed under: Bing Map, Silverlight Tagged: Bing Map, Silverlight
This was originally posted here.

Like
Report
*This post is locked for comments