GeoNames Search (3)
In our last post, we created an application that queried the GeoNames webservice and returned the results into a table. A logical next step would be to add the capability to map the results on a world map. So today will be use some of the code we already wrote for the WorldMapper project to add that functionality.
Here is how my final form looked after I added a world map picture and 2 new buttons:

The only new code I had to write was for the “Map It” button. I needed to access the selected row in the results table, grab the longitude and latitude, and add a point on the map at that location.
Here is how I did that:
private void btnMapIt_Click(object sender, EventArgs e) { DataGridViewSelectedRowCollection rows = grdResults.SelectedRows; foreach (DataGridViewRow row in rows) { double longX = Convert.ToDouble(row.Cells["lng"].Value); double latY = Convert.ToDouble(row.Cells["lat"].Value); Point mapPoint = ConvertLatLon2ImagePosition(longX, latY); PlotPosition(mapPoint); } }
The source code for the two functions, ConvertLatLon2ImagePosition and PlotPosition, was already covered in the WorldMapper post. The “Clear Map” button’s code is also found in that post.
In the above screenshot, I queried GeoNames for Toronto and was able to map the location of the city on the map. For non-US locations, be sure to look up the county code here.
This concludes our GeoNames posts for now. Many of these early projects are really laying the groundwork for some much cooler projects to come. If you have any questions about what we’ve already covered or have an idea for a project you would like to see, send us an e-mail.