Link Addresses to Google Maps with JQuery (Part 1)
Submitted by rmarrs on Tue, 05/26/2009 - 09:44First, you need JQuery. I'm not going to go into the basics of JQuery, but you should be using it.
Second, We use this to create a global class, usable throughout the website to signify an address, and provide links to Google Maps. The stripHTML function is only required if you wish to markup your addresses for formatting. From then on, you just do a
- <span class="address">1600 Amphitheatre Parkway<br />Mountain View, CA 94043
Now for the code:
- function stripHTML(oldString) {
- var newString = "";
- var inTag = false;
- for(var i = 0; i < oldString.length; i++) {
- if(oldString.charAt(i) == '<') inTag = true;
- if(oldString.charAt(i) == '>') {
- inTag = false;
- i++;
- }
- if(!inTag) newString += oldString.charAt(i);
- }
- return newString;
- }
- /* <span class="address">Address</span> will auto create link to Google Maps */
- $(function() {
- $(".address").each(
- function() {
- var address = this.innerHTML
- this.innerHTML = '<a href="http://maps.google.com/maps?q=' + stripHTML(address) + 'class="map">' + address + '</a>';
- }
- );
- });
Keep in mind, we only want to wrap the address inside the tags.
In part 2, we'll use JQuery with Google Maps to allow the user to view the map on the website, or jump off to Google Maps.