Solved

google map

Posted on 2013-06-18
6
742 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
ID: 39258524
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 39262289
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
ID: 39367732
Tommy: I copy/paste your code in html file. It is not working
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 38

Expert Comment

by:Tom Beck
ID: 39368350
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
ID: 39372179
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
ID: 39372208
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

803 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