Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


google map

Posted on 2013-06-18
Medium Priority
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.
Question by:solution1368
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 38

Expert Comment

by:Tom Beck
ID: 39262289
Working example:
   <!DOCTYPE html>
    <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 }
      <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>

			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) {

            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++) {
                        chunk += step;
                  } else{

            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
                        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

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

	 // });
  <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">

Open in new window


Author Comment

ID: 39367732
Tommy: I copy/paste your code in html file. It is not working
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.

Author Comment

ID: 39372179
I use google chrome and once the button is clicked, it just show blank page.
LVL 38

Accepted Solution

Tom Beck earned 2000 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.

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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)
Suggested Courses

610 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