center the map on marker key on google maps api 3

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yousef Altaf
    New Member
    • Jun 2011
    • 22

    center the map on marker key on google maps api 3

    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

    Code:
    <?php
    // Gets data from URL parameters
    $name = $_GET['name'];
    $address = $_GET['address'];
    $lat = $_GET['lat'];
    $lng = $_GET['lng'];
    $type = $_GET['type'];
    
    mysql_connect("localhost", "root", "") or
             die("Could not connect: " . mysql_error());
    mysql_select_db("arabia");
    
    // Insert new row with user data
    $query = sprintf("INSERT INTO markers " .
             " (id, name, address, lat, lng, type ) " .
             " VALUES (NULL, '%s', '%s', '%s', '%s', '%s');",
             mysql_real_escape_string($name),
             mysql_real_escape_string($address),
             mysql_real_escape_string($lat),
             mysql_real_escape_string($lng),
             mysql_real_escape_string($type));
    
    $result = mysql_query($query);
    
    if (!$result) {
      die('Invalid query: ' . mysql_error());
    }
    
    ?>
    and this is the HTML code
    Code:
    <!DOCTYPE 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: Map Simple</title>
        <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
        <script type="text/javascript">
        var marker;
        var infowindow;
    
        function initialize() {
          var latlng = new google.maps.LatLng(30.0599153,31.2620199,13);
          var options = {
            zoom: 13,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
          }
          var map = new google.maps.Map(document.getElementById("map-canvas"), options);
          var html = "<table>" +
                     "<tr><td>Name:</td> <td><input type='text' id='name'/> </td> </tr>" +
                     "<tr><td>Address:</td> <td><input type='text' id='address'/></td> </tr>" +
                     "<tr><td>Type:</td> <td><select id='type'>" +
                     "<option value='bar' SELECTED>bar</option>" +
                     "<option value='restaurant'>restaurant</option>" +
                     "</select> </td></tr>" +
                     "<tr><td></td><td><input type='button' value='Save & Close' onclick='saveData()'/></td></tr>";
        infowindow = new google.maps.InfoWindow({
         content: html
        });
     
        google.maps.event.addListener(map, "click", function(event) {
            marker = new google.maps.Marker({
              position: event.latLng,
              map: map
            });
            google.maps.event.addListener(marker, "click", function() {
              infowindow.open(map, marker);
            });
        });
        }
    
        function saveData() {
          var name = escape(document.getElementById("name").value);
          var address = escape(document.getElementById("address").value);
          var type = document.getElementById("type").value;
          var latlng = marker.getPosition();
     
          var url = "phpsqlinfo_addrow.php?name=" + name + "&address=" + address +
                    "&type=" + type + "&lat=" + latlng.lat() + "&lng=" + latlng.lng();
          downloadUrl(url, function(data, responseCode) {
            if (responseCode == 200 && data.length <= 1) {
              infowindow.close();
              document.getElementById("message").innerHTML = "Location added.";
            }
          });
        }
    
        function downloadUrl(url, callback) {
          var request = window.ActiveXObject ?
              new ActiveXObject('Microsoft.XMLHTTP') :
              new XMLHttpRequest;
    
          request.onreadystatechange = function() {
            if (request.readyState == 4) {
              request.onreadystatechange = doNothing;
              callback(request.responseText, request.status);
            }
          };
    
          request.open('GET', url, true);
          request.send(null);
        }
    
        function doNothing() {}
        </script>
      </head>
    
      <body style="margin:0px; padding:0px;" onload="initialize()">
        <div id="map-canvas" style="width: 800px; height: 600px"></div>
        <div id="message"></div>
      </body>
    
    </html>
    now this is getting data from the db code

    php code

    Code:
    <?php  
    
    // Start XML file, create parent node
    
    $dom = new DOMDocument("1.0");
    $node = $dom->createElement("markers");
    $parnode = $dom->appendChild($node); 
    
    // Opens a connection to a MySQL server
    
    mysql_connect("localhost", "root", "") or
             die("Could not connect: " . mysql_error());
    mysql_select_db("arabia");
    
    // Select all the rows in the markers table
    
    $query = "SELECT * FROM markers WHERE 1";
    $result = mysql_query($query);
    if (!$result) {  
      die('Invalid query: ' . mysql_error());
    } 
    
    header("Content-type: text/xml"); 
    
    // Iterate through the rows, adding XML nodes for each
    
    while ($row = @mysql_fetch_assoc($result)){  
      // ADD TO XML DOCUMENT NODE  
      $node = $dom->createElement("marker");  
      $newnode = $parnode->appendChild($node);   
      $newnode->setAttribute("name",$row['name']);
      $newnode->setAttribute("address", $row['address']);  
      $newnode->setAttribute("lat", $row['lat']);  
      $newnode->setAttribute("lng", $row['lng']);  
      $newnode->setAttribute("type", $row['type']);
    } 
    
    echo $dom->saveXML();
    
    ?>
    and the html code
    Code:
    <!DOCTYPE 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>map</title>
        <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
        <script type="text/javascript">
        //<![CDATA[
    
        var customIcons = {
          restaurant: {
            icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png'
          },
          bar: {
            icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png'
          }
        };
        function load() {
          var map = new google.maps.Map(document.getElementById("map"), {
            center: new google.maps.LatLng(30.0599153,31.2620199,13)||map.setCenter(marker.getPosition()),
            zoom: 13,
            mapTypeId: 'roadmap'
          });
          var infoWindow = new google.maps.InfoWindow;
    
          // Change this depending on the name of your PHP file
          downloadUrl("phpsqlajax_genxml.php", function(data) {
            var xml = data.responseXML;
            var markers = xml.documentElement.getElementsByTagName("marker");
            for (var i = 0; i < markers.length; i++) {
              var name = markers[i].getAttribute("name");
              var address = markers[i].getAttribute("address");
              var type = markers[i].getAttribute("type");
              var point = new google.maps.LatLng(
                  parseFloat(markers[i].getAttribute("lat")),
                  parseFloat(markers[i].getAttribute("lng")));
              var html = "<b>" + name + "</b> <br/>" + address;
              var icon = customIcons[type] || {};
              var marker = new google.maps.Marker({
                map: map,
                position: point,
                icon: icon.icon
              });
              bindInfoWindow(marker, map, infoWindow, html);
            }
          });
        }
    
        function bindInfoWindow(marker, map, infoWindow, html) {
          google.maps.event.addListener(marker, 'click', function() {
            infoWindow.setContent(html);
            infoWindow.open(map, marker);
          });
        }
    
        function downloadUrl(url, callback) {
          var request = window.ActiveXObject ?
              new ActiveXObject('Microsoft.XMLHTTP') :
              new XMLHttpRequest;
    
          request.onreadystatechange = function() {
            if (request.readyState == 4) {
              request.onreadystatechange = doNothing;
              callback(request, request.status);
            }
          };
    
          request.open('GET', url, true);
          request.send(null);
        }
    
        function doNothing() {}
    
        //]]>
    
      </script>
    
      </head>
    
      <body onload="load()">
        <div id="map" style="width: 500px; height: 300px"></div>
      </body>
    
    </html>
    now how to center the map on the key
Working...