Learn how to write the code to use the WebClient to download an image of a map of a location from a REST API.
- [Instructor] Now that we have an account on here.com, we can call the map image REST API directly from our C Sharp code. Go to Visual Studio, File, Open, Project. On my Desktop we go into the folder with the exercise files with the Where am I API. Go to the Begin folder and open the solution. In the Program.cs file, you'll see where we left off the last time.
We used the GeoCoordinate watcher to obtain the location. Every time the position changed, we were provided with a location. Now, when we are provided with that changed position, we are going to stop watching and produce the image. How to produce the image? We will have to call Map Image REST API. And we'll do so right here in the eventHandler. Now to make this easier, I've already created a helper class here with some of the helper methods necessary.
And so we'll add a Show method here and we'll pass in the location e.Position.Location for the Show method to actually show us the map image. Now, in this case I've done some of the work for you. In the MapImage file, you can see here we have a Show method, put that in. Now, this is not really part of learning, but you would need the GeoCoordinate, of course, control dot. We've previously added the reference, so now we'll add the using statement, usingSystem.Device.Location.
And we'll build a filename, a temporary file, if you like, and I'll use the dollar notation here on the string again, location.Latitude, and notice this is the string formatting notation. So if I said location.Latitude.toString, I'd be able to pass in this format string. Here I can use it as a shorthand notation by following the colon with the actual format notation. So location.Latitude has two digits optional, dot and then three fraction digits.
The same for location.Longitude, separate them by comma. And I'll add a second comma here for, watch this, the HorizontalAccuracy. Now, in this case I know it's based on meters, that's what the Windows API is going to give us, and so I'm going to create a filename here that is the Latitude,Longitude,Horizontal accuracy, all numbers, .jpg. Then I'll ask a method to do the actual download for me. You'll see the .net code in just a moment.
And I would need to build up the URI that's necessary for the download. Now, this is quite a bit of complicated, finicky string manipulation, but it is essentially coming up with a string that is a fully qualified URL with query string parameters. And based on the filename, we'll put that on the local disk. And once the download completes, we'll open the downloaded file with the default viewer on this machine. So, given the filename, let's go to DownloadMapImage. We'll provide target URI, filename and the client is simply system.net.webClient.
Probably you've seen this before, very simple to use. Give it target URL, give it a local filename and it'll do the download for you. You can even do so with eventHandlers, so you can do it asynchronously. In this case we are already in a background thread, so I'll do this call synchronously. Now, buildURI, let's scroll all the way down. This is where things get a little bit more complicated. Ctrl + kau, comment this back in.
So first off, we need the App ID and App Code provided from here.com. So when we signed up for our account in the previous step, we obtained an App ID and an App Code that are the secrets we need to call this API from our application. This is associated with our subscription, our project on here.com. The DNS name, as per the documentation, there's a server name that we have to use to obtain our map image, and notice here the cit is the customer integration and testing API, so I'm calling image.maps basically on the test infrastructure.
Once you switch to production, you would leave out the cit. For the URL, we use the DNS name from above, the one that might change based on production or test, and we add the path, the folders and the rest of the REST API structure necessary to make the get request. Then we add the secrets as query string parameters. We build up query string there, we build up the latitude and longitude as query string parameters, and then we combine the whole thing into a URI.
So the base URL, so https DNS name and path, followed by a question mark. And remember the u parameter is the uncertainty, a circle that will be drawn on the map, based on our accuracy, followed by the query string parameters that is the secrets, followed by the latitude/longitude that was also built in as query string parameters. And so all this is doing, this whole method of string manipulation, it's just creating one long URL for us to open.
That URL will be passed to the Download method. So we have a filename, we have a URL based on the location, we ask for the download, and once that file is downloaded, we'll open it with the DefaultApp. Press F12 to see that method. So down here we can see the method is simply opening a command prompt window. We will suppress the window, so you won't actually see it, but cmd/C will run the command following the C parameter, which is start, which is a built-in keyword to just say start the following based on file associations.
And so the filename will be .jpg, and so the default viewer for jpg will be used. Let's build. No typing mistakes. Go into Program. Let me just proofread this really quickly. And so I think that's it. So let's run it. Press F5, click Start. And position change has happened, and there comes the photo viewer, the default viewer for jpg. Notice in the top left corner the name of the file is our location, and the accuracy, followed by M for meters of horizontal accuracy, .jpg, j-p-g.
And it says, I'm not so sure. I think I know where you are, but with bad accuracy in the sense of almost 5.5 kilometers, because it's based off wifi, rather than GPS. And so if we were to run this on a device with a GPS for a better estimate, we will of course get a much smaller circle of accuracy.