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
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

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

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

770 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