简体   繁体   中英

Google MAP API with PHP and Javascript

I am very new in HTML5 area, I am trying to make a program that will fetch the data from mysql with PHP and use geocode to transform from address to Latlng. it is a LBS program, and I was trying for 2 weeks, my question is I cannot put markers into the Google MAP, I did the locator corrected and map is loaded, but the marker cannot land on map, please help!! thank you, and I am really appreciate your help. if this code has any mistake or problem please give me generous advice, I am very new in this area, and I am trying to learn more in HTML5 Javascript, thank you and I am very appreciate your advice!!!

my code is here:

             'enter code here`<?php
              require_once 'Common/system_start.php';
              $sql="select addr from tab_mem order by sn desc ";
              $result=  mysql_query($sql);
              $data=mysql_fetch_assoc($result);
              $row=mysql_num_rows($result);
              $n = array(); 
              for($i=0;$i<$rows;$i++) 
                  { 
                   $data=mysql_fetch_row($result);
                   $n[] = $data[0]; 
                      } 
                   echo '<script type="text/javascript">'; 
                   echo var n = new Array("', join($n,'","'), '");'; 
                   echo '</script>'; 
                   ?> 
                   <code>

              <pre>
             <!DOCTYPE html>
             <html><head><meta charset="utf-8">

             <meta name="viewport" content="initial-scale=1.0,user-scalable=no"/>
             <!-- Google Maps -->
             <script type="text/javascript" src="http://maps.google.com/maps/api/js?                                  `enter code here`sensor=true"></script>

             <script type="text/javascript" charset="utf-8" src="cordova-1.6.0.js">
             </script>
             <!-- program main -->
             <script type="text/javascript">
              var iterator=0;
              markers=n;
              var a=n.length;


             function load() {
             var map_div = document.getElementById("map_div");

             //initial locator
             navigator.geolocation.watchPosition(onSuccess, onError, { frequency: 3000        `enter code here`});


              // decide location
        function onSuccess(position){



//fetching latlng from GPS 

              var latlng = new google.maps.LatLng(position.coords.latitude, 
                `position.coords.longitude);

                 // display map
                 var gmap = new google.maps.Map(
                     map_div, {
                     zoom:16,
                     center: latlng,
                     mapTypeId: google.maps.MapTypeId.ROADMAP
                   });




                       //drop the marker

                     function drop() {
                     for (var i = 0; i < a; i++) {
                     setTimeout(function() {
                       }, i * 200);
                      }
                     }



            //decode address to latlng
            //pass address data from PHP
          for(var i in n){
     var address=n;
       geocoder=new google.maps.Geocoder(); 
        geocoder.geocode({'address':address},function(results,status){
     if(status==google.maps.GeocoderStatus.OK){
        //alert("AAA");
        address=results(i).geometry.location;
     function addMarker() { markers.push(new google.maps.Marker({

                       postion:results(i).geometry.location,
                        map:gmap,
                        animation:google.map.animation.DROP
                        //iterater++; 

       }));
   //itertor++;
   }
    //markerNearBy.setMap(map);
   }else{
   alert("Geocode was not sucessful for the following reason:"+status);
     }
  });
 }
   }

   }



     function onError(error) {
             alert('code: '+ error.code+ '\n' 
                 +'message: ' + error.message + '\n');    
       }

   </script>
   </head>
   <body onLoad="load()">
    <!-- Map Display place -->
    <div id="map_div" style="width:320px; height:480px;"></div>
    </body>
    </html>
    <code>

Your code doesn't look well-formed. You have the "addMarker" function defined inside your "for" loop. Also, it's not getting called anywhere.

Take a look at the sample code Google provides for working with markers and overlays and try to emulate it. The sample only uses one marker, but you can simply iterate through your results. You've got the right intentions by adding your Marker objects to an array, and setting your map object to the map property of the Marker, but you will need to format/restructure your code first or it will not work.

Sample code:

https://developers.google.com/maps/documentation/javascript/overlays#RemovingOverlays

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM