1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

can't get google map to center

Discussion in 'Google API' started by marklandry, Jul 18, 2012.

  1. #1
    Hi,
    I've created a map with php/mysql/ajax according the tutorial provided at the google developers site.

    All works fine but I"m trying to use the geocoder example to center the map per user input. The geocoder script works fine by itself but when I add it to my map code it will pull lat/long but won't center the map.

    I've included my code below for the whole thing - thanx for any help you can provide.

    Mark

    Code (Text):
    1. <!DOCTYPE html >  <head>
    2.     <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    3.     <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    4.     <title>Tantomedia AON Map</title>
    5.     <link href="https://developers.google.com/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
    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.  
    11.     var customIcons = {
    12.       restaurant: {
    13.         icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
    14.         shadow: ''
    15.       },
    16.       bar: {
    17.         icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
    18.         shadow: ''
    19.       }
    20.     };
    21.     var map;
    22.     var geocoder;
    23.  
    24.  
    25.     function load() {
    26.       geocoder = new google.maps.Geocoder();
    27.       var map = new google.maps.Map(document.getElementById("map"), {
    28.         center: new google.maps.LatLng(39.824476,-105.080261),
    29.         zoom: 13,
    30.         mapTypeId: google.maps.MapTypeId.ROADMAP
    31.       });
    32.  
    33.       var infoWindow = new google.maps.InfoWindow;
    34.       google.maps.event.addListener(map, 'idle', showMarkers);
    35.      
    36.        function showMarkers(){
    37.         var bounds = map.getBounds();
    38.            var ne = bounds.getNorthEast();
    39.            var sw = bounds.getSouthWest();
    40.            var neLong = ne.lng();
    41.            var neLat = ne.lat();
    42.            var swLong = sw.lng();
    43.            var swLat = sw.lat();
    44.            //var neLong = -105.011;
    45.            //var neLat = 39.864;
    46.            //var swLong = -105.1489;
    47.            //var swLat = 39.784;
    48.            //alert("NE LAT = "+neLat + "\nNE LONG = "+neLong + "\nSW LAT = "+swLat + "\nSW LONG = "+swLong);
    49.            //tanto ='"'+'phpsqlajax_genxml3.php?swLat='+swLat+'&swLong='+swLong+'&neLat='+neLat+'&neLong='+neLong+'"';
    50.            tanto ="phpsqlajax_genxml3.php?swLat="+swLat+"&swLong="+swLong+"&neLat="+neLat+"&neLong="+neLong;
    51.            //tanto = "phpsqlajax_genxml3.php";
    52.            //alert(tanto);
    53.       // Change this depending on the name of your PHP file
    54.       downloadUrl(tanto, function(data) {
    55.       //downloadUrl("phpsqlajax_genxml3.php", function(data) {
    56.         var xml = data.responseXML;
    57.         var markers = xml.documentElement.getElementsByTagName("marker");
    58.         for (var i = 0; i < markers.length; i++) {
    59.           var name = markers[i].getAttribute("name");
    60.           var address = markers[i].getAttribute("address");
    61.           var type = markers[i].getAttribute("type");
    62.           var point = new google.maps.LatLng(
    63.               parseFloat(markers[i].getAttribute("lat")),
    64.               parseFloat(markers[i].getAttribute("lng")));
    65.           var html = "<b>" + name + "</b> <br/>" + address;
    66.           var icon = customIcons[type] || {};
    67.           var marker = new google.maps.Marker({
    68.             map: map,
    69.             position: point,
    70.             icon: icon.icon,
    71.             shadow: icon.shadow
    72.           });
    73.  
    74.  
    75.           bindInfoWindow(marker, map, infoWindow, html);
    76.         }
    77.       });
    78.      }
    79.     }
    80.  
    81.  
    82.  
    83.     function bindInfoWindow(marker, map, infoWindow, html) {
    84.       google.maps.event.addListener(marker, 'click', function() {
    85.         infoWindow.setContent(html);
    86.         infoWindow.open(map, marker);
    87.       });
    88.     }
    89.  
    90.  
    91.     function downloadUrl(url, callback) {
    92.       var request = window.ActiveXObject ?
    93.           new ActiveXObject('Microsoft.XMLHTTP') :
    94.           new XMLHttpRequest;
    95.  
    96.  
    97.       request.onreadystatechange = function() {
    98.         if (request.readyState == 4) {
    99.           request.onreadystatechange = doNothing;
    100.           callback(request, request.status);
    101.         }
    102.       };
    103.  
    104.  
    105.       request.open('GET', url, true);
    106.       request.send(null);
    107.     }
    108.  
    109.  
    110.     function doNothing() {}
    111.  
    112.  
    113. function codeAddress() {
    114.     var loc=[];
    115.     var map;
    116.     var address = document.getElementById("address").value;
    117.  
    118.  
    119.     // next line creates asynchronous request
    120.     geocoder.geocode( { 'address': address}, function(results, status) {
    121.       // and this is function which processes response
    122.       if (status == google.maps.GeocoderStatus.OK) {
    123.         loc[0]=results[0].geometry.location.lat();
    124.         loc[1]=results[0].geometry.location.lng();
    125.         var addressbox = document.getElementById('address');  
    126.         var latbox = document.getElementById('lat');  
    127.         var longbox = document.getElementById('long');
    128.         var testbox = document.getElementById('testbox');
    129.         latbox.value = loc[0];
    130.         longbox.value = loc[1];
    131.         testbox.value = results[0].geometry.location;
    132.         map.setCenter(results[0].geometry.location);
    133.         var marker = new google.maps.Marker({
    134.             map: map,
    135.             position: results[0].geometry.location
    136.         });
    137.       } else {
    138.         alert("Geocode was not successful for the following reason: " + status);
    139.       }
    140.     });
    141.  
    142.  
    143.     // pretty meaningless, because it always will be []
    144.     // this line is executed right after creating AJAX request, but not after its response comes
    145.     return loc;
    146.    
    147.   }
    148.  
    149.  
    150.  
    151.  
    152.     //]]>
    153.  
    154.  
    155.   </script>
    156.  
    157.  
    158.  
    159.  
    160.  
    161.  
    162.   </head>
    163.  
    164.  
    165.   <body onload="load()">
    166.     <div id="map" style="width: 800px; height: 600px"></div>
    167.  
    168.  
    169.  
    170.  
    171.     <input id="address" type="textbox" value="1135 Eudora st. denver">
    172.     <input type="Submit" value="Submit" onclick="codeAddress()">
    173.     <input id = "lat" type="textbox">
    174.     <input id = "long" type="textbox">
    175.     <input id = "testbox" type="textbox">
    176.  
    177.  
    178.  
    179.  
    180.   </body>
    181.  
    182.  
    183. </html>
     
    marklandry, Jul 18, 2012 IP