Solved

Configuring a Google Map

Posted on 2014-03-15
4
610 Views
Last Modified: 2014-03-15
I'm trying to learn how to configure a Google Map API V3, and having some challenges.  I would like to be able to specify coordinates for the initial view of the map, which differ from coordinates of the marker.  I'd like the zoom control to zoom in on the marker, not on the initial coordinates of the map.  Can you edit the following code to achieve that?

<script>
jQuery(document).ready(function () {
	initmap("20.915927","-156.282931","Maui Tradewinds","4320 Une Place, Haiku","Hawaii")
	})
	function initmap(lat,lng,hname,add,city) {
		var secheltLoc = new google.maps.LatLng(lat,lng);
		var myMapOptions = {
			 zoom: 14
			,center: secheltLoc
			,mapTypeId: google.maps.MapTypeId.ROADMAP,disableDefaultUI: true,
			draggable: false, zoomControl: false, scrollwheel: false, disableDoubleClickZoom: true
		};
		var theMap = new google.maps.Map(document.getElementById("map_canvas"), myMapOptions);


	var contentString =hname+"<br>"	+add+"<br>"+city //Style here as you like
  var infowindow = new google.maps.InfoWindow({
      content: contentString
  });

  var marker = new google.maps.Marker({
     position: new google.maps.LatLng(lat,lng),
      map: theMap
  });
// Show the popup on load.
  google.maps.event.addListenerOnce(theMap, 'idle', function() {
   infowindow.open(theMap,marker);
});
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(theMap,marker);
  });
	}
	</script>

Open in new window

0
Comment
Question by:ddantes
[X]
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
  • 2
  • 2
4 Comments
 
LVL 38

Expert Comment

by:Tom Beck
ID: 39931289
You can change the initial map center after it's created by using panBy(). Add this line near the bottom of your initmap function to push the marker and infowindow down 100 pixels.

theMap.panBy(0, -100);

Then you can add an event listener to re-center the map to the marker location on zoom_changed. Of course you will have to enable the zoom control.

google.maps.event.addListener(theMap, 'zoom_changed', function() {
    theMap.setCenter(secheltLoc);
  });
0
 

Author Comment

by:ddantes
ID: 39931603
Thank you.  I'll try this, but I was hoping the initial map center could be  set with a different initial  (lat, long) instead of panning by pixels.  If I use pixels, won't the result vary with the screen size and resolution of the user's device?
0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 250 total points
ID: 39931790
The "center" property controls where the center of the map will be. Set it to something other than the value passed to the function for the marker. Or, just set the latitude to something else.

var myMapOptions = {
                   zoom: 14
                  ,center: new google.maps.LatLng("20.9165",lng)
                  ,mapTypeId: google.maps.MapTypeId.ROADMAP,disableDefaultUI: true,
                  draggable: false, zoomControl: true, scrollwheel: false, disableDoubleClickZoom: true
            };
0
 

Author Comment

by:ddantes
ID: 39931808
This is exactly what I hoped for.
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Use Mid in Html 6 23
Duplicate Elements with JS / jQuery 3 25
Can anyone help me find this preloader? 51 43
Table header must be on top 2 31
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…

726 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