in the last post, we talked about how to integrate an ASP.NET application with google maps. But, we didn’t do anything interesting at all. Today, I will show some features of the google maps API so your application can be more funny. What about an application that show how to people arrive in your home from anywhere? So, let’s do it now.
Now, if you compile and run you application, you will see the marker over your home on map.
All right, now let’s create the ASP.NET controllers that your friends will use to type their address. I will use that a Labe, a TextBox and a Button. So, after that my form will be like this:
- Get the textfield value;
- Create a new google directions object;
- Use the function directions.load() to return a route from your friend’s address to yours.
The searchRoute function is shown below:
Here, we use the ClientID property to get the server control identifier generated by ASP.NET. All we have to do later is to create a string in the format from: Source to: Destination and call the function directions.load(). Notice that I’m using my coordinates as destination, so in your case, just use yours.
Because every ASP.NET control sends a PostBack to the server, we will have to use some Ajax to view the result. If we don’t use Ajax, the page will be reloaded (and also the map since the function load() well be called to our form). To prevent that, we just have to use a ScriptManner and an UpdatePanel, which will make that just a part of the page is reloaded after the button click event.
Our new <body> is show belown:
If it’s still not working, remember to type the whole address, with city, country, etc.
Today we talked about more google maps API features and created a simple application that shows how to arrive to you home from anywhere. To learn more about google maps and its API take a look at google maps API documentation.