Solved

google map

Posted on 2013-06-18
6
705 Views
Last Modified: 2013-09-04
I want to do two things with Google Map

1. I have 15 locations that I want to make into the map. How can I do that?
2. I want allow user to enter zip codes, and once the button is clicked, It will tell the user which location will be closer. like to sort vertically by shortest distance to longest.

I am not programmer and try to find easier way to handle it.
0
Comment
Question by:solution1368
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
Comment Utility
0
 
LVL 38

Expert Comment

by:Tom Beck
Comment Utility
Working example:
   <!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Google Maps JavaScript API v3 Example: Geocoding Simple</title>
    <style type="text/css">
      html { height: 100% }
        body { height: 100%; margin: 0; padding: 0 20px }
        #map-canvas { height: 70%; width: 80% }
		#list li { list-style: none }
      </style>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
    <script src="http://maps.googleapis.com/maps/api/js?sensor=false&v=3&libraries=geometry" type="text/javascript"></script>

    <script>
			var startingPoint = 123;
            var bounds = new google.maps.LatLngBounds();
			var geocoder;		
			
      $(document).ready(function() {            

            var map = new google.maps.Map(document.getElementById('map-canvas'));

            geocoder = new google.maps.Geocoder();
                  geocoder.geocode({'address': 'USA'}, function(results, status) {
                  map.fitBounds(results[0].geometry.viewport);
            });
	  });

            var chunk = 0, inerval;
			
            var locations = [
                  {
                        address: '48 W Seegers Rd, Arlington Heights, IL 60005',
                        company: 'Company B'
                  },
                  {
                        address: '220 N Michigan Ave, Chicago, IL 60601',
                        company: 'Company A'
                  },
                  {
                        address: '1670 Barclay Blvd, Buffalo Grove, IL 60089',
                        company: 'Company C'
                  },
                  {
                        address: '1665 Elk Blvd, Des Plaines, IL 60016',
                        company: 'Company D'
                  },
                  {
                        address: '1800 W Central Rd, Mount Prospect, IL 60056',
                        company: 'Company E'
                  },
                  {
                        address: '3400 Dundee Rd, Northbrook, IL 60062',
                        company: 'Company F'
                  },
                  {
                        address: '3100 Dundee Rd Ste 704, Northbrook, IL 60062',
                        company: 'Company G'
                  },
                  {
                        address: '188 W Randolph St Ste 600, Chicago, IL 60601',
                        company: 'Company H'
                  },
                  {
                        address: '69 W Washington St, Chicago, IL 60602',
                        company: 'Company I'
                  },
                  {
                        address: '269 W Washington St, Chicago, IL 60601',
                        company: 'Company J'
                  }
            ];
			
			function geolocate(){
            	inerval = setInterval(delayCode, 700);
			}
			
            function delayCode() {
				if(startingPoint != 123){
                  var step = 1;
                  if (chunk < locations.length) {
                        for (x = chunk; x < locations.length && x < chunk + step; x++) {
                              codeAddress(locations[x]);
                        }
                        chunk += step;
                  } else{
                        clearInterval(inerval);
                        compareDistances();
                  }
				}
            }

            function setDefaultAddress(address) {
                  var geocoder = new google.maps.Geocoder();
                  geocoder.geocode({'address': address}, function(results, status) {
                        if(status == google.maps.GeocoderStatus.OK) {
                              result = results[0].geometry.location;
                              startingPoint = result;
                              var mapOptions = {
                                    zoom: 15,
                                    center: startingPoint,
                                    mapTypeId: google.maps.MapTypeId.ROADMAP
                              }

                              map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
                              var marker = new google.maps.Marker({
                                    map: map,
                                    icon: "http://www.conceptfire-uk.com/wp-content/uploads/2011/07/icon-pin-color.png",
                                    position: startingPoint
                              });
							  geolocate();
                        }
                        else {
                              alert("Unable to find address: " + status);
                        }
                  });
            }

            function codeAddress(address) {
                  geocoder.geocode({ 'address':address.address }, function(results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                        var location = results[0].geometry.location;
                        address.location = location;
                        address.distance = parseFloat(google.maps.geometry.spherical.computeDistanceBetween(startingPoint, location) * 0.000621371192).toFixed(2) ; //convert to miles with two decimal places
                        }
                        else {
                              alert('Geocode was not successful for the following reason: ' + status);
                        }
                  });
            }




            function compareDistances(){
                  locations.sort(function(a,b) {
                        return a.distance - b.distance;
                  });
                  //add the two closest markers
                  var marker1 = new google.maps.Marker({map: map, position: locations[0].location, title: locations[0].company, address: locations[0].address, distance: locations[0].distance});
                  var marker2 = new google.maps.Marker({map: map, position: locations[1].location, title: locations[1].company, address: locations[1].address, distance: locations[1].distance});
				  
				  var infowindow1 = new google.maps.InfoWindow();
			      google.maps.event.addListener(marker1, "click", function () {
                	  infowindow1.setContent('<b>' + this.title + '</b><br />' + this.address + '<br/>Distance: ' + this.distance + ' miles');
                	  infowindow1.open(map, this);
            	  });
				  var infowindow2 = new google.maps.InfoWindow();
				  google.maps.event.addListener(marker2, "click", function () {
                	  infowindow2.setContent('<b>' + this.title + '</b><br />' + this.address + '<br/>Distance: ' + this.distance + ' miles');
                	  infowindow2.open(map, this);
            	  });

                  //extend the bounds of the map to include the two closest markers and the starting point
                  bounds.extend(startingPoint);
                  bounds.extend(locations[0].location);
                  bounds.extend(locations[1].location);

                  //zoom the map to nicely fit the bounds of the two closest markers and the starting point
                  map.fitBounds(bounds);
				  
				  //display all addresses and their distance from the starting point
				  $('#list').html("");
				  $(locations).each(function(index){
					  $('#list').append('<li>' + (index+1) + '.)&nbsp;&nbsp;<b>' + locations[index].company + '</b>, ' + locations[index].address + ', <b>' + locations[index].distance + ' miles</b></li>');
				  });
            }

	 // });
    </script>
    
  </head>
  <body>
  <h3>Map Showing the Two Closest Companies to a the Zip Code Provided</h3>
  	<input type="text" id="zip" /><label for="zip"> Enter Zip Code</label><input type="button" value="Geo Locate" onClick="setDefaultAddress(document.getElementById('zip').value)" />
    <br />
    <br />
    <div id="map-canvas"></div>
    <ul id="list">
    </ul>
  </body>
</html>

Open in new window

0
 

Author Comment

by:solution1368
Comment Utility
Tommy: I copy/paste your code in html file. It is not working
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 38

Expert Comment

by:Tom Beck
Comment Utility
Sorry to hear that. Can you provide any additional details, error messages perhaps? Here's what I get when I run the code in Firefox. I typed in the zip code 60601 to get these results.Google Map -- Two closest markers plus a list of distances.
0
 

Author Comment

by:solution1368
Comment Utility
I use google chrome and once the button is clicked, it just show blank page.
0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
Comment Utility
I'm getting the same exact results in Chrome v28 (on a Mac) as I'm getting in Firefox so I am unable to reproduce the problem you are experiencing.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a pageā€¦
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now