473,750 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

center the map on marker key on google maps api 3

22 New Member
Hi all I am working on google map api-3 and I have two interface one is inserting data to my database and second getting the data on my site from my database now everything is working fine I insert the data to my databse and getting it but here is the problem I need to center the map on the marker key on the map

here is my code

to insert the data

php code

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Gets data from URL parameters
  3. $name = $_GET['name'];
  4. $address = $_GET['address'];
  5. $lat = $_GET['lat'];
  6. $lng = $_GET['lng'];
  7. $type = $_GET['type'];
  8.  
  9. mysql_connect("localhost", "root", "") or
  10.          die("Could not connect: " . mysql_error());
  11. mysql_select_db("arabia");
  12.  
  13. // Insert new row with user data
  14. $query = sprintf("INSERT INTO markers " .
  15.          " (id, name, address, lat, lng, type ) " .
  16.          " VALUES (NULL, '%s', '%s', '%s', '%s', '%s');",
  17.          mysql_real_escape_string($name),
  18.          mysql_real_escape_string($address),
  19.          mysql_real_escape_string($lat),
  20.          mysql_real_escape_string($lng),
  21.          mysql_real_escape_string($type));
  22.  
  23. $result = mysql_query($query);
  24.  
  25. if (!$result) {
  26.   die('Invalid query: ' . mysql_error());
  27. }
  28.  
  29. ?>
and this is the HTML code
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html >
  2.   <head>
  3.     <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
  4.     <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  5.     <title>Google Maps JavaScript API v3 Example: Map Simple</title>
  6.     <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
  7.     <script type="text/javascript">
  8.     var marker;
  9.     var infowindow;
  10.  
  11.     function initialize() {
  12.       var latlng = new google.maps.LatLng(30.0599153,31.2620199,13);
  13.       var options = {
  14.         zoom: 13,
  15.         center: latlng,
  16.         mapTypeId: google.maps.MapTypeId.ROADMAP
  17.       }
  18.       var map = new google.maps.Map(document.getElementById("map-canvas"), options);
  19.       var html = "<table>" +
  20.                  "<tr><td>Name:</td> <td><input type='text' id='name'/> </td> </tr>" +
  21.                  "<tr><td>Address:</td> <td><input type='text' id='address'/></td> </tr>" +
  22.                  "<tr><td>Type:</td> <td><select id='type'>" +
  23.                  "<option value='bar' SELECTED>bar</option>" +
  24.                  "<option value='restaurant'>restaurant</option>" +
  25.                  "</select> </td></tr>" +
  26.                  "<tr><td></td><td><input type='button' value='Save & Close' onclick='saveData()'/></td></tr>";
  27.     infowindow = new google.maps.InfoWindow({
  28.      content: html
  29.     });
  30.  
  31.     google.maps.event.addListener(map, "click", function(event) {
  32.         marker = new google.maps.Marker({
  33.           position: event.latLng,
  34.           map: map
  35.         });
  36.         google.maps.event.addListener(marker, "click", function() {
  37.           infowindow.open(map, marker);
  38.         });
  39.     });
  40.     }
  41.  
  42.     function saveData() {
  43.       var name = escape(document.getElementById("name").value);
  44.       var address = escape(document.getElementById("address").value);
  45.       var type = document.getElementById("type").value;
  46.       var latlng = marker.getPosition();
  47.  
  48.       var url = "phpsqlinfo_addrow.php?name=" + name + "&address=" + address +
  49.                 "&type=" + type + "&lat=" + latlng.lat() + "&lng=" + latlng.lng();
  50.       downloadUrl(url, function(data, responseCode) {
  51.         if (responseCode == 200 && data.length <= 1) {
  52.           infowindow.close();
  53.           document.getElementById("message").innerHTML = "Location added.";
  54.         }
  55.       });
  56.     }
  57.  
  58.     function downloadUrl(url, callback) {
  59.       var request = window.ActiveXObject ?
  60.           new ActiveXObject('Microsoft.XMLHTTP') :
  61.           new XMLHttpRequest;
  62.  
  63.       request.onreadystatechange = function() {
  64.         if (request.readyState == 4) {
  65.           request.onreadystatechange = doNothing;
  66.           callback(request.responseText, request.status);
  67.         }
  68.       };
  69.  
  70.       request.open('GET', url, true);
  71.       request.send(null);
  72.     }
  73.  
  74.     function doNothing() {}
  75.     </script>
  76.   </head>
  77.  
  78.   <body style="margin:0px; padding:0px;" onload="initialize()">
  79.     <div id="map-canvas" style="width: 800px; height: 600px"></div>
  80.     <div id="message"></div>
  81.   </body>
  82.  
  83. </html>
now this is getting data from the db code

php code

Expand|Select|Wrap|Line Numbers
  1. <?php  
  2.  
  3. // Start XML file, create parent node
  4.  
  5. $dom = new DOMDocument("1.0");
  6. $node = $dom->createElement("markers");
  7. $parnode = $dom->appendChild($node); 
  8.  
  9. // Opens a connection to a MySQL server
  10.  
  11. mysql_connect("localhost", "root", "") or
  12.          die("Could not connect: " . mysql_error());
  13. mysql_select_db("arabia");
  14.  
  15. // Select all the rows in the markers table
  16.  
  17. $query = "SELECT * FROM markers WHERE 1";
  18. $result = mysql_query($query);
  19. if (!$result) {  
  20.   die('Invalid query: ' . mysql_error());
  21.  
  22. header("Content-type: text/xml"); 
  23.  
  24. // Iterate through the rows, adding XML nodes for each
  25.  
  26. while ($row = @mysql_fetch_assoc($result)){  
  27.   // ADD TO XML DOCUMENT NODE  
  28.   $node = $dom->createElement("marker");  
  29.   $newnode = $parnode->appendChild($node);   
  30.   $newnode->setAttribute("name",$row['name']);
  31.   $newnode->setAttribute("address", $row['address']);  
  32.   $newnode->setAttribute("lat", $row['lat']);  
  33.   $newnode->setAttribute("lng", $row['lng']);  
  34.   $newnode->setAttribute("type", $row['type']);
  35.  
  36. echo $dom->saveXML();
  37.  
  38. ?>
and the html code
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html >
  2.   <head>
  3.     <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
  4.     <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  5.     <title>map</title>
  6.     <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
  7.     <script type="text/javascript">
  8.     //<![CDATA[
  9.  
  10.     var customIcons = {
  11.       restaurant: {
  12.         icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png'
  13.       },
  14.       bar: {
  15.         icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png'
  16.       }
  17.     };
  18.     function load() {
  19.       var map = new google.maps.Map(document.getElementById("map"), {
  20.         center: new google.maps.LatLng(30.0599153,31.2620199,13)||map.setCenter(marker.getPosition()),
  21.         zoom: 13,
  22.         mapTypeId: 'roadmap'
  23.       });
  24.       var infoWindow = new google.maps.InfoWindow;
  25.  
  26.       // Change this depending on the name of your PHP file
  27.       downloadUrl("phpsqlajax_genxml.php", function(data) {
  28.         var xml = data.responseXML;
  29.         var markers = xml.documentElement.getElementsByTagName("marker");
  30.         for (var i = 0; i < markers.length; i++) {
  31.           var name = markers[i].getAttribute("name");
  32.           var address = markers[i].getAttribute("address");
  33.           var type = markers[i].getAttribute("type");
  34.           var point = new google.maps.LatLng(
  35.               parseFloat(markers[i].getAttribute("lat")),
  36.               parseFloat(markers[i].getAttribute("lng")));
  37.           var html = "<b>" + name + "</b> <br/>" + address;
  38.           var icon = customIcons[type] || {};
  39.           var marker = new google.maps.Marker({
  40.             map: map,
  41.             position: point,
  42.             icon: icon.icon
  43.           });
  44.           bindInfoWindow(marker, map, infoWindow, html);
  45.         }
  46.       });
  47.     }
  48.  
  49.     function bindInfoWindow(marker, map, infoWindow, html) {
  50.       google.maps.event.addListener(marker, 'click', function() {
  51.         infoWindow.setContent(html);
  52.         infoWindow.open(map, marker);
  53.       });
  54.     }
  55.  
  56.     function downloadUrl(url, callback) {
  57.       var request = window.ActiveXObject ?
  58.           new ActiveXObject('Microsoft.XMLHTTP') :
  59.           new XMLHttpRequest;
  60.  
  61.       request.onreadystatechange = function() {
  62.         if (request.readyState == 4) {
  63.           request.onreadystatechange = doNothing;
  64.           callback(request, request.status);
  65.         }
  66.       };
  67.  
  68.       request.open('GET', url, true);
  69.       request.send(null);
  70.     }
  71.  
  72.     function doNothing() {}
  73.  
  74.     //]]>
  75.  
  76.   </script>
  77.  
  78.   </head>
  79.  
  80.   <body onload="load()">
  81.     <div id="map" style="width: 500px; height: 300px"></div>
  82.   </body>
  83.  
  84. </html>
now how to center the map on the key
Feb 8 '14 #1
0 2017

Sign in to post your reply or Sign up for a free account.

Similar topics

3
61091
by: Sean | last post by:
Have you ever wanted to add the great features inherent in Google Maps? Here is how you do it. ============== == STEP ONE == ============== Create a new MS Access form called frmGoogleMap. Size the form to your liking...
6
1657
by: JimInOC | last post by:
Does anyone have any idea as to how google loads pieces of images together so as you drag new image pieces are loaded from the server? I haven't had much luck finding any articles on it. thanks
6
2988
by: Sam Carleton | last post by:
Ok, over the years I have read about doing web programing and I have done some real basic stuff. Now I am digging into some real ASP.Net 2.0 and am totally lost some things. I have a master page setup and that is working great. On my contact page I would like to use Google Maps API Version 2 to show a map to my location. Below is the first Google example. I would like to add this to my aspx page that is using the master page. I...
3
9618
by: Lemon Tree | last post by:
Hi everybody. I am new to Javascript so probably I am asking something trivial, but I cannot see where there problem is. Or, better. Maybe I know where the problem is but I cannot find a solution. So, here we go... I have loaded an XML document containing a bunch of addresses. For each address I would like to put a marker on a map using the Google Maps API. In order to do so, I iterate over the address tags in the XML
3
4494
by: ziycon | last post by:
I'm not sure if anyone here would know anything about the google map js, i've tried the google maps api community with no luck. I have the below code, it loads ok, but when i click on the map it doesn't place a marker. What should happen is that when you click on the map it should place a marker and when clicked on the map a second time that it should place a marker at the new location and remove the previous one so that there ...
5
2939
by: Nike1984 | last post by:
I'm fairly new to Javascript and it's more of a guessing game for me... I'm trying to build an app for Google Maps and just had some issues recently. First off I just wanted to say that everything works fine in FF and IE. It's Chrome I'm having issues with. I understand that Chrome is still somewhat in beta stages, so some bugs might occur. However this seems like something I might have done. So... I used a code that I found on Econym as...
0
1782
by: Ashriya | last post by:
Hi All, I have developed a Google maps application in which there are a few markers. On mouse over of those markers, I need to show certain data. The map is placed inside the iframe of a window. The content on mouseover is through a label created from a Label.js file. How do I display the label next to the marker itself? Could someone please help me with this? Thanks in advance.
0
1394
by: lucas vidgen | last post by:
Hi, I'm trying to write a function that will show arrowheads on polylines selected from a select box in a google map. I only want the arrowheads that belong to the line shown to be displayed. I have got it working using check boxes, and the select box shows the lines, but am having trouble tweaking the code to get the arrows to show up as well. You can see the working checkboxes (and the semi-working selectbox) here:...
6
16199
by: mfaisalwarraich | last post by:
Hi everyone, I am trying to add multiple pinpoint to google map using Lon/Lat. All addresses will be fetched from mysql database using PHP. I have looked at google and searched for it on various sites but nothing helpful i found. i have used the following code which worked for only one address whereas i have alot of addresses. <!DOCTYPE html>
1
2716
by: George Thompson | last post by:
i have a google maps api on my website, where when you click it grabs the lat/long coordinates and works well for me, but i would for a visual reference like to see a marker added to the place where i clicked do i know exactly where it is.. and i\'ve tried many different configurations.. any ideas? <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> var geocoder;...
0
9568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6079
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4709
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.