Expecho's .Net corner
All about software development

How to use the virtual earth winforms user control part 2: Adding pushpins

June 25, 2008 22:32 by Peter Bons

There are two ways to add a pushpin to the map, these are:

  • using the VE_AddPushPin() method, that takes a SearchLocation object as a parameter
  • using the VE_FindLocations() method, that takes a List<> of SearchLocation objects

The VE_AddPushPin() can only be used when the latitude and longitude are known. The VE_FindLocations() method can be used when you want to add pushpins based on an address or business name.

VE_AddPushPin()
An example of the usage of this method would be to add a pushpin on the map at the location where the user had clicked with the mouse like this:

private void FrmDemo_Shown(object sender, EventArgs e)
{
    ucVEarth.InitMap();    // Initialize the VEarth user control
    ucVEarth.VE_OnMouseClick += new MouseClick(ucVEarth_VE_OnMouseClick);
}

void ucVEarth_VE_OnMouseClick(double latitude, double longitude, object id, bool altKey, bool ctrlKey, bool shiftKey, bool leftButton, bool rightButton)
{
    SearchLocation loc = new SearchLocation();
    loc.Latitude = latitude;
    loc.Longitude = longitude;
    loc.PushPinDescription = String.Format("Lat: {0}, Long: {1}", latitude, longitude); 
    loc.PushPinTitle = "You've put me on the map";
    ucVEarth.VE_AddPushPin(loc); 

    pushpinID = loc.ID; // store the id of the added pushpin so we can delete it later by calling the VE_DeletePushPin(string id) method
}

VE_FindLocations()
An example for the usage of this method is would be knowing the addresses of the locations where you want to add a pushpin. The method takes a list of SearchLocation objects.

private void AddPushPinsDemo()
{
    List<SearchLocation> locations = new List<SearchLocation>();
    SearchLocation a = new SearchLocation();
    a.Where = "Amsterdam, Holland"// where to search for (address)
    a.PushPinTitle = "Amsterdam, Holland"// Title of the puspin
    a.PushPinImage = "PushPin.GIF"; // Custom image
    locations.Add(a);

    SearchLocation b = new SearchLocation();
    b.Where = "Rotterdam, Holland";    
    b.PushPinTitle = "Rotterdam, Holland";       
    b.PushPinImage = "PushPin.GIF"
    locations.Add(b);

    ucVEarth.VE_FindLocations(
           locations,  // the locations to find
           true,         // center the map at the last found location when finished
           true          // display the pushpins on the map
    );
}



In the background the method VE_FindLocations() calls the javascript method "VEMap.Find()" which is an asynchronous method. Therefore there are two options you can use to do actions when the method is finished. There is an event handler that is fired when the call to VE_FindLocations() is finished like this:

ucVEarth.VE_OnFinishedFindingLocations += new FinishedFindingLocations(ucVEarth_VE_OnFinishedFindingLocations);

    void ucVEarth_VE_OnFinishedFindingLocations()
    {
        foreach(SearchLocation loc in locations)
        {
           //  after a call to VE_FindLocations() the properties ID, latitude and longitude are set with the
           //  id of the pushpin and the exact location (lat/long) so we can for example store these in a database for later use.
           StoreInDatabase(loc.ID, loc.Latitude, loc.Longtitude);
        }
    }



The other option is to call the VE_FindLocations() method with an extra parameter pointing to a callback function like this:

ucVEarth.VE_FindLocations(locations, true, true, FindLocationsCallBack);

    void FindLocationsCallBack()
    {
        foreach(SearchLocation loc in locations)
        {
           //  after a call to VE_FindLocations() the properties ID, latitude and longitude are set with the
           //  id of the pushpin and the exact location (lat/long) so we can for example store these in a database for later use.
           StoreInDatabase(loc.ID, loc.Latitude, loc.Longtitude);
        }
    }



With this method you can translate addresses to latitude/longitude coordinates. If you set the showResults parameter to false no pushpins will be added to the map but the Latitude and Longitude properties of each SerachLocation in the list will be set.

The SearchLocation class
As you can see a SearchLocation object represents a location on the map. The class is defined like this:

  • Do net set the ID yourself, after a call to VE_AddPushpin() or VE_FindLocations() this ID is set to the id of the pushpin generated by the virtual earth control. When using VE_FindLocations() the ID is only set when the displayResults parameter is set to true.
  • Use the PushPinImage property to specify the icon used on the map.
  • The PushPinDescription property can be a string with HTML code that will be viewed when the pushpin is clicked.
  • Do not set the Latitude and Longitude when using VE_FindLocations() as these properties will be set during the find operation based on the What (business) or Where (address) properties.
  • When using VE_AddPushpin() make sure your object has the Latitude and Longitude set, the What or Where properties are not used since this method adds a pushpin based on the Latitude and Longitude.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

July 4. 2009 18:24