?
Solved

google API change center with onclick

Posted on 2011-05-02
9
Medium Priority
?
620 Views
Last Modified: 2013-11-20
I have example like seeing in image.when anyone click on green bar marker should mark proper address and change the center of ma.

and for light green bar there is different address and that will change the marker in map and change the center.

for thrid orange there is different address and when anyone click on that address will change on map and change the cetner accrodingly.

Can anyone please help me on this
map.png
0
Comment
Question by:itgirl7
  • 4
  • 3
7 Comments
 
LVL 11

Expert Comment

by:thydzik
ID: 35510133
add onclick events to the bars that dynamically center the map and move the marker location.

have a look at this page
http://code.google.com/apis/maps/documentation/javascript/events.html

the first example shows how to change a map's centered location.

the second example shows how to change a marker's location
0
 

Author Comment

by:itgirl7
ID: 35513605
I just wanted to know that thing how can I call any of the function that center the map in onclick of that DIV.
0
 
LVL 11

Expert Comment

by:thydzik
ID: 35513756
add an onclick event to the bars.

example here;
http://www.javascriptkit.com/javatutors/event2.shtml
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:itgirl7
ID: 35513794
It's not working I am just adding my code.Please let me know is it correct I am trying to first center the map as I clicked.
<!DOCTYPE html> 
<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title>Google Maps JavaScript API v3 Example: Markers, Info Window and StreetView</title> 
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" /> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 
 var map;
function initialize() {
  var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
  var myOptions = {
    zoom: 4,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  
  google.maps.event.addListener(map, 'click', function(event) {
    placeMarker(event.latLng);
  });
}
  
function placeMarker(location) {
  var marker = new google.maps.Marker({
      position: location, 
      map: map
  });

  map.setCenter(location);
}
</script> 
</head> 
<body onLoad="initialize()"> 
  <div id="map_canvas" style="float:left;width:700px; height:500px;"></div> 
   <div id="mymap" style="float:left;width:100px; height:100px; float:right;background-color:#3C9" onclick="placeMarker(event.latLng)" ></div>
	
</body> 
</html>

Open in new window

0
 
LVL 11

Accepted Solution

by:
thydzik earned 2000 total points
ID: 35514118
try the modified code attached;

 
<!DOCTYPE html> 
<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title>Google Maps JavaScript API v3 Example: Markers, Info Window and StreetView</title> 
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" /> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 
 var map;
function initialize() {
  var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
  var myOptions = {
    zoom: 4,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  
  google.maps.event.addListener(map, 'click', function(event) {
    placeMarker(event.latLng);
  });
}
  
function placeMarker(location) {
  var marker = new google.maps.Marker({
      position: location, 
      map: map
  });

  map.setCenter(location);
}

function tester(lat,lng) {
	var myLatlng = new google.maps.LatLng(lat,lng);

	var marker = new google.maps.Marker({
		position: myLatlng, 
		map: map
	});
}

</script> 
</head> 
<body onLoad="initialize()"> 
  <div id="map_canvas" style="float:left;width:700px; height:500px;"></div> 
   <div id="mymap" style="float:left;width:100px; height:100px; float:right;background-color:#3C9" onclick="javascript:tester(-32.036238,115.930738);" ></div>
	
</body>

Open in new window

0
 

Author Comment

by:itgirl7
ID: 35514494
hey Thanks It's working..I want to know one more thing is that possible when I click on DIV that open window same like image or I have to click on marker to open that window.

Please let me know so I can ocmplete it as soon as possible.
0
 
LVL 11

Expert Comment

by:thydzik
ID: 35514538
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

850 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