| Ricky's profileRicky's Bing Maps BlogBlogSkyDrive | Help |
|
|
5/3/2009 Integrating a GPS receiver with Virtual EarthIntegrating a GPS receiver with Virtual Earth has often been a topic that has come up on the Virtual Earth forums. In the book Practical .NET 2.0 Networking Projects there is a great section on how to create a Virtual Earth WinForm application that integrates with a GPS device. The Virtual Earth related code was written for version 3. Virtual Earth is now in its 6th version and the code has changed a lot since version 3. Many others who have attempted to follow the code in the previously mentioned book have had some issues getting the code to work. After hearing a lot of questions on the forums I ended up buying the book myself and put together an updated version of the this application but never got around to posting it online. So its better late than never. I've made a few modifications to the original design. Knowing that I would not always have access to the internet I have set the program up so that even if there is no internet connection it will still be capable of recording the RAW GPS data in a text file. This file can then be read later by the same program when an internet connection is available and display the path on a Virtual Earth map. For the GPS I managed to pick one up used on EBay for $15. It connects to a computer through USB but requires a serial port emulator to work correctly which is available from the GPS manufactures web site. This application requires following steps:
Connecting to a GPS Receiver The first step is to get your computer to recognize your device. Drivers may be needed in order for your device to be made available through one of your serial ports (COM). These drivers usually give you the ability to set which ports your GPS data will be transmitted on and if it will transmit the raw data or NMEA data. You will want to transmit NMEA data. The manufacturer of your GPS Receiver may provide an application to monitor your GPS receiver. This is very useful when debug your application. It also usually allows you to see what other information can be retrieved from the RAW data that the comes from the GPS device. Once your computer is able to connect to your GPS receiver you should be able to connect to it as well. To connect to your GPS receiver from your application we will have a drop down with a list of all the available serial ports so that the user can select the port that their GPS device is connected to. You will want to populate this drop down when the application loads. The following code can be used to retrieve a list of serial ports and fill our drop down with the names:
Our application has a button to connect to the GPS receiver. We will give this button the ability to b oth connect and disconnect to the GPS receiver. If we want to connect to the GPS receiver we will want to close the port if it is already open. Next we can specify the port configuration. Once the port configuration are set the connection can be opened. If we want to disconnect from the GPs receiver we just have to call the Close method of the connected serial port. The following code shows how to do this:
Reading and Processing GPS Data The data that you will be receiving from your GPS will be in NMEA format. NMEA stands for National Marine Electronics Association. The National Marine Electronics Association developed a standard for representing GPS related data, often referred to as NMEA sentences. The following is an example of some NMEA data that I'm able to receive from my GPS: $GPRMC,054715,A,4340.254,N,07923.009,W,0.0,0.0,030509,10.4,W*48 Most GPS devices support the NMEA schema. Here are a list of common NMEA data sentences and their meanings:
More information on NMEA sentences can be found here: http://www.gpsinformation.org/dale/nmea.htm For our application we are only interested in geographical location of where we are. This information is contained in the $GPGGA sentence. The $GPGGA sentence separates it's data using commas. The data in this field has the following meaning:
Note that field 14 and 15 are not comma separated. For our application we will want to latitude, longitude, altitude, and unit of measure of the altitude. We can retrieve this information from columns 2-5, 9,10. To read the GPS data we first need to set up a timer that will consistently query the GPS for data. When the timer fires an event we will call a method called UpdateGPSData.This method will verify that the serial port is open and then will read the current data from the port. It will then verify that data was received. In our application we will output the raw data to a textbox for the user to see. We will then pass the data to method called ProcessNMEAData which will parse the data and update the map if need be. If the user is saving data to a file the data will be written to the file. The UpdateGPSData method looks like this:
The ProcessNMEAData method iterates through each NMEA sentence it receives and passes it on to the appropriate method to be process further. Since we only need the $GPGGA data we will send all data in this sentence to a method called ProcessGPGGA. We will setup the frame work from accessing the other sentences for future use. The ProcessNMEAData looks like this:
The ProcessGPGGA method will parse the raw data in the $GPGGA sentence and will update the appropriate methods. The latitude and longitude values in the NMEA sentence are in degrees, minutes, and decimal minute form. We will want to convert these to decimal degrees. This can be done using the following formula: Note that the vertical bars represent integer division. The direction of the latitude and longitude coordinate can make the respective value negative if the longitude value is in the (W)est direction or if the latitude value is in the (S)outh direction. We will display the current location in a textbox for the user. If the user is connected to the internet then we can send the data to the map to be displayed. If the user is loading in a file of NMEA sentences then this method will call a javascript function called AddPoint. This function creates an array of coordinates. If the user is not loading in a file and has selected the option to follow their position then a pushpin will be used to display the users current location on the map. This is achieved by calling a method called AddPushpin.
Displaying data on a Virtual Earth map A simple HTML page is used to create an Virtual Earth map. This HTML page is loaded into a Web Browser control in our WinForm application. This map will have two shape layers. One to display paths, and one to display pushpins. A function called AddPushpin will remove all other pushpins and create a new pushpin with the data it is sent. A function called drawPath will use a polyline to draw out a path of locations that either the user has currently traveled or have loaded. Four other simple functions are used to add a point to the points array, clear the points array, center the map and update the data. The updateMapData method will send information back to the WinForm application so that it can display the information in a textbox. This HTML page looks like this:
Before we can load the map and display locations on it we need to determine if there is an internet connection. A simple way to do this is to try and make a web request to a well known URL and verify that an "OK" response is received. The following method does this:
In the ProcessGPGGA method we make a call to an AddPushpin method. This method passes our coordinate information to the javascript. This method looks like this:
If the user has decided not to follow their current location on the map but decides they would like to just show the current location on the map this can by centering the map over the current location. The button click event for this functionality looks like this:
Many of the other functionality that are in this application are pretty straight forward and can be found in the source code here: http://cid-e7dba9a4bfd458c5.skydrive.live.com/self.aspx/VE%20Sample%20code/GPSMapper.zip An idea for integrating some of the data that is in the other NMEA sentences: It should be possible to determine the position of all the satellites that your GPS sees. With this in mind you should be able to create a Virtual Earth 3D application that displays 3D models of the satellites around the globe in the correct position. 2/5/2009 MultiShapes and Virtual Earth
Current Virtual Earth supports pushpins, polylines, and polygons. Another commonly used shape type that is used in other mapping applications is MultiShape or MultiGeometry. Not having this in Virtual Earth has been an issue for some users. I have put together a javascript file that can be loaded after the Virtual Earth map control that creates a MultiShape class that can be used with Virtual Earth. This new shape object takes in an array of VEShape objects and has methods to add and remove shapes from the object. Many of the functionalities that exist in the VEShape class exist in the MultiShape class. The VEMap and VEShapelayer classes have been extended in order to support this adding and removing of the MultiShape objects from the map using the existing methods in Virtual Earth. This javascript file and a simple sample html file can be downloaded here: http://cid-e7dba9a4bfd458c5.skydrive.live.com/self.aspx/VE%20Tools/MultiShape.zip Note: Since this extends the VEMap and VEShapeLayer classes by accessing undocumented methods in the map control it is possible that those methods may change without notice. This may result in unexpected errors occurring in the future. Browsers tested: IE7, FF2 Virtual Earth versions tested: 5, 6, 6.1, 6.2 MultiShape Constructor var x = new MultiShape(shapes);
Public Methods These methods can be used in the same way as the equivalent methods in the VEShape object.
These methods can be used in the same way as the equivalent methods in the VEShapeLayer class.
These are new methods which can be used with a MultiShape object.
Constructors var x = MultShape.GetIconLocation(); var x = MultShape.SetIconlocation(latlong); where latlong is a VELatLong object. var x = MultShape.IndexOfShape(shape); where shape is a VEShape object. MultiShape properties
Extended VEMap and VEShapeLayer method's The following methods have been extended to work when used with MultiShape objects.
10/1/2008 KML to GeoRSS TranslatorWhen version 6.0 of Virtual Earth was released it was announced that KML file types were supported. Unfortunately, it there were some delays and support was not added until after the release. It just happened that I needed to load some KML data into a Virtual Earth map so I threw together this program that translates KML files to GeoRSS. When KML support was added there were the following limitations:
By translating your KML files to GeoRSS format you gain the ability to add upwards of 10,000 points, and ability to follow network links to through all levels. This translator is setup to handle the following KML elements:
This program has the ability to either translate an individual KML or a go through a directory and translate all KML files within the directory. You can download the complete source code here.
Batch Geocoding with Virtual Earth Geocoding ServiceIt has been a standard practice for MapPoint and Virtual Earth users to Geocode their location data using the Virtual Earth/MapPoint Customer Service site. Since the introduction of rooftop geocoding in Virtual Earth users have been asking for the ability to Geocode their location data with the same accuracy. This can now be done using the Virtual Earth geocoding service. I have thrown together a simple program that can do this. You can download it here: http://cid-e7dba9a4bfd458c5.skydrive.live.com/self.aspx/VE%20Tools/BatchGeocodeWithVE.zip. This program takes in tab or pipe delimited data source files, similar to the Customer Service site. It fills in the latitude and longitude fields and adds three more fields; Confidence, MatchCode, and CalculationMethod. Running a couple of addresses through this program you can see a difference in the accuracy of the coordinates provided by the VE Geocoding service compared to the coordinates provided by the Customer Service site.
*Note: You may have to update the service reference if an InvalidOperationException occurs when you first run the application. |
|
|