affichage de marqueurs sur un google map

Réduire
X
 
  • Filtrer
  • Heure
  • Afficher
Tout effacer
nouveaux messages

  • [RÉGLÉ] affichage de marqueurs sur un google map

    bonsoir
    je cherche à extraire des données de ma bdd pour els afficher sur une google map
    mais cela bloque voici le code de mon script tout se passe dans la partie en php
    merci d'une aide
    LJ
    Code HTML:
    <script type="text/javascript">
    	function initialize() {
    	  var myLatlng = new google.maps.LatLng(45.7363882,1.1044922);
    	  var mapOptions = {
    	    zoom: 8,
    	    center: myLatlng
    	  };
    
    	  var map = new google.maps.Map(document.getElementById('div_carte'), mapOptions);
    
      var ctaLayer = new google.maps.KmlLayer({
        url: 'http://www.ingall-niger.org/images/stories/Documents/87.kml'
      });
      ctaLayer.setMap(map);
    
    	  var contentString = '<h1>coucou</h1><br>';
    
    	  var infowindow = new google.maps.InfoWindow({
    	      content: contentString
    	  });
    
    	  /*var marker = new google.maps.Marker({
    	      position: myLatlng,
    	      map: map,
    	      title: 'Moov it !'
    	  });*/
    
    
            <?php
    	  $user = JFactory::getUser();
    	  $userid = $user->id;        
    	//Connection a la base de donnée
    	$bdd =& JFactory::getDBO();
            $sql = "SELECT name, lat, lng 
    		FROM #__batenergie	
    		WHERE author='$userid'";
            $bdd = setQuery($sql);
     
    		while($data = mysql_fetch_assoc($bdd))
                    {
    			echo 'var marker = new google.maps.Marker({
    				position: new google.maps.LatLng('.$data['lat'].','.$data['lng'].'), 
    				map: map,
                                    title: \''.$data['name'].'\'
    			});
     
    			google.maps.event.addListener(marker);
     
    			';	
    		}
           ?>
    
    	  google.maps.event.addListener(marker, 'click', function() {
    	    infowindow.open(map,marker);
    	  });
    	}
    
    	google.maps.event.addDomListener(window, 'load', initialize);
    </script>
    Dernière édition par laurent00 à 31/12/2013, 14h01
    mon avatar : http://www.ingall-niger.org

  • #2
    Re : affichage de marqueurs sur un google map

    Si tu cherches les infos, détails, de la carte : ils sont dans le fichier kml?
    Règlement du forum : http://forum.joomla.fr/faq.php
    Comment mettre le post en [Réglé]: http://forum.joomla.fr/announcement.php?f=58
    Joomla! 3 Le Livre Pour Tous , n'hésitez pas à le lire pour vous aider
    http://www.iwannaclick.org

    Commentaire


    • #3
      Re : affichage de marqueurs sur un google map

      non ce n'est pas ce que je cherche, le kml sert juste d'habillage de mon territoire
      je souhaite afficher des marqueurs qui sont dans ma bdd avec le
      $sql = "SELECT name, lat, lng
      FROM #__batenergie
      WHERE author='$userid'";
      LJ
      mon avatar : http://www.ingall-niger.org

      Commentaire


      • #4
        Re : affichage de marqueurs sur un google map

        bonjour
        mon, code évolue et le problème maintenant c'est que j'ai ceci dans un script qui ramène une
        Code PHP:
        <b>Fatal error</b>: Call to undefined function setQuery() in ... 
        pourtant plus haut dans mon fichier le même setQuery fonctionne après ma requête !?

        Code PHP:
        function initCarte(){
                //appel tableau de donnée de la requête
                <?php $bdd setQuery($query);
                
        $tResult = array(); 
                    while(
        $marker mysql_fetch_object$bdd)) {
                      
        $tResult[] = $marker;
                    }
                echo 
        "var dataMarker="json_encode$tResult), ";\n";
                
        ?>
        ...
        ce code est destiné à appeler un tableau de données issues d'une requête en amont du script

        LJ
        Dernière édition par laurent00 à 30/12/2013, 17h49
        mon avatar : http://www.ingall-niger.org

        Commentaire


        • #5
          Re : affichage de marqueurs sur un google map

          Bonjour,

          Dans la fonction initCarte, $bdd n'est pas visible (règle de portée des variables)
          Soit passer $bdd en argument à la fonction, soit soit utiliser un JFacrory::getDBO() pour accéder à l'instance de la base de données.

          De plus en mixant un setQuery() Joomla! avec des fonctions mysql_fetch* tu casses tout.
          Code PHP:
          public function initCarte($bdd$query )

              
          //appel tableau de donnée de la requête 
              
          $bdd->setQuery$query ) ;
              
          $tresult = array() ;
              
          $res $bdd->loadObjectList();
              foreach ( 
          $tresult as $marker 
              {
                  
          $tResult[] = $marker->champ_voulu 
              } 
              echo 
          "var dataMarker="json_encode$tResult), ";\n"

          par exemple, fonctionnerait beaucoup mieux.
          Pas de demande de support par MP.
          S'il n'y a pas de solution, c'est qu'il n'y a pas de problème (Devise Shadok)

          Commentaire


          • #6
            Re : affichage de marqueurs sur un google map

            comment est-ce que j'insère cela dans mon script ?
            Code PHP:
            <script type="text/javascript">
                function initCarte(){
                    //appel tableau de donnée de la requête
                    <?php $bdd setQuery($query);
                    
            $tResult = array(); 
                        while(
            $marker mysql_fetch_object$bdd)) {
                          
            $tResult[] = $marker;
                        }
                    echo 
            "var dataMarker="json_encode$tResult), ";\n";
                    
            ?>

                      var oMap, oMarker, oInfo;
                      var i, nb = dataMarker.length;
                      // création de la carte
                      oMap = new google.maps.Map( document.getElementById( 'div_carte'),{
                        'center' : new google.maps.LatLng( 46.80, 1.70),
                        'zoom' : 6,
                        'mapTypeId' : google.maps.MapTypeId.ROADMAP
                      });
                      // création infobulle
                      oInfo = new google.maps.InfoWindow();
                      // création des markers dans une boucle
                      for( i = 0; i < nb; i++){
                        oMarker = new google.maps.Marker({
                          'position' : new google.maps.LatLng( dataMarker[i].lat, dataMarker[i].lng,
                          'map' : oMap,
                          'title' : dataMarker[i].name
                        });
                        // événement clic sur le marker
                        google.maps.event.addListener( oMarker, 'click', function( data){
                          // affichage position du marker
                          oInfo.setContent( 'position :<br>' +data.latLng.toUrlValue(5));
                          oInfo.open( oMap, oMarker);
                        });
                      }
                    }
                // init lorsque la page est chargée
                google.maps.event.addDomListener(window, 'load', initCarte);
            </script>
            mon avatar : http://www.ingall-niger.org

            Commentaire


            • #7
              Re : affichage de marqueurs sur un google map

              Mais là, à voir un peu plus de ton code, tu fais un mix de JavaScript et PHP !
              Pas de demande de support par MP.
              S'il n'y a pas de solution, c'est qu'il n'y a pas de problème (Devise Shadok)

              Commentaire


              • #8
                Re : affichage de marqueurs sur un google map

                oui c'est un script js google maps dans lequel la partie php appelle le tableau de données
                en tout cas ta solution enlève l'erreur SetQuery mais la map ne s'affiche pas
                Code PHP:
                <script type="text/javascript">
                function 
                initCarte($bdd$query )
                    { 
                        
                //appel tableau de donnée de la requête 
                        
                $bdd->setQuery$query ) ;
                        
                $tresult = array() ;
                        
                $res $bdd->loadObjectList();
                        foreach ( 
                $tresult as $marker 
                        {
                        
                $tResult[] = $marker->lat ;
                        
                $tResult[] = $marker->lng 
                        
                $tResult[] = $marker->name ;  
                        } 
                        echo 
                "var dataMarker="json_encode$tResult), ";\n"
                     

                          var 
                oMapoMarkeroInfo;
                          var 
                inb dataMarker.length;
                          
                // création de la carte
                          
                oMap = new google.maps.Mapdocument.getElementById'div_carte'),{
                            
                'center' : new google.maps.LatLng46.801.70),
                            
                'zoom' 6,
                            
                'mapTypeId' google.maps.MapTypeId.ROADMAP
                          
                });
                          
                // création infobulle
                          
                oInfo = new google.maps.InfoWindow();
                          
                // création des markers dans une boucle
                          
                for( 0nbi++){
                            
                oMarker = new google.maps.Marker({
                              
                'position' : new google.maps.LatLngdataMarker[i].latdataMarker[i].lng,
                              
                'map' oMap,
                              
                'title' dataMarker[i].name
                            
                });
                            
                // événement clic sur le marker
                            
                google.maps.event.addListeneroMarker'click', function( data){
                              
                // affichage position du marker
                              
                oInfo.setContent'position :<br>' +data.latLng.toUrlValue(5));
                              
                oInfo.openoMapoMarker);
                            });
                          }
                        }
                    
                // init lorsque la page est chargée
                    
                google.maps.event.addDomListener(window'load'initCarte);
                </
                script
                mon avatar : http://www.ingall-niger.org

                Commentaire


                • #9
                  Re : affichage de marqueurs sur un google map

                  Il t emanque les balises <?php et ?> entre la partie PHP et la partie JavaScript.

                  Et dans la fonction, comment lui passes-tu ses paramètres ? depuis autre chose qui l'appelle en PHP ?

                  Évites autant que possible ces mixtures indigestes en organisant ton code de manière cohérente (le PHP d'un côté, le JavaScript d'un autre), ces mélanges façon spaghetti, ça donne du code illisible et non maintenable.
                  Pas de demande de support par MP.
                  S'il n'y a pas de solution, c'est qu'il n'y a pas de problème (Devise Shadok)

                  Commentaire


                  • #10
                    Re : affichage de marqueurs sur un google map

                    mes données viennent d'une bd par une requete sql spécifique à chaque user
                    je souhaite appeler dans le script ces données pour les afficher
                    LJ
                    mon avatar : http://www.ingall-niger.org

                    Commentaire


                    • #11
                      Re : affichage de marqueurs sur un google map

                      Si c'est un fichier js le truc, pas moyen.

                      Si c'est du PHP, il faut organiser le tout de manière cohérente.
                      Pas de demande de support par MP.
                      S'il n'y a pas de solution, c'est qu'il n'y a pas de problème (Devise Shadok)

                      Commentaire


                      • #12
                        Re : affichage de marqueurs sur un google map

                        c un fichier default_map.php ci dessous qui est appelé par une vue default.php
                        Code PHP:
                        <?php
                        defined
                        ('_JEXEC') or die('Restricted access');  

                              
                        $user JFactory::getUser();
                              
                        $userid $user->id;

                            
                        $bdd =& JFactory::getDBO();
                            
                        $query "SELECT name, lat, lng 
                                    FROM #__batenergie_batenergieep
                                    WHERE author='
                        $userid'";
                            
                        //$bdd->setQuery ( $query );
                            //$data = $bdd->loadObjectList();
                            //echo var_dump($data);

                                
                        ?>

                        <style type="text/css">
                        html, body {
                          height : 100%;
                          width:100%;
                          margin : 0;
                          padding : 0;
                        }

                        #div_main {
                          margin : auto;
                          width : auto;
                        }
                        #div_carte {
                          margin : auto;
                          width : 1000px;
                          height : 300px;
                          border : 1px solid #c0c0c0;
                        }
                        </style>

                        <hr>
                            <div align="left">
                                <h1>Implantation des postes</h1>
                            </div>

                        <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&language=fr"></script>

                        <script type="text/javascript">
                            function initCarte()
                            { 
                              <?php //appel tableau de données de la requête 
                                      
                        $bdd->setQuery$query );
                                    
                        $tresult = array() ;
                                    
                        $tresult $bdd->loadObjectList();
                                        foreach ( 
                        $tresult as $marker 
                                        {
                                        
                        $tResult[] = $marker->lat ;
                                        
                        $tResult[] = $marker->lng 
                                        
                        $tResult[] = $marker->name ;  
                                        } 
                                    echo 
                        "var dataMarker="json_encode$tResult), ";\n"
                              
                        ?>

                                  var oMap, oMarker, oInfo;
                                  var i, nb = dataMarker.length;
                                  // création de la carte
                                  oMap = new google.maps.Map( document.getElementById( 'div_carte'),{
                                    'center' : new google.maps.LatLng( 46.80, 1.70),
                                    'zoom' : 6,
                                    'mapTypeId' : google.maps.MapTypeId.ROADMAP
                                  });
                                  // création infobulle
                                  oInfo = new google.maps.InfoWindow();
                                  // création des markers dans une boucle
                                  for( i = 0; i < nb; i++){
                                    oMarker = new google.maps.Marker({
                                      'position' : new google.maps.LatLng( dataMarker[i].lat, dataMarker[i].lng,
                                      'map' : oMap,
                                      'title' : dataMarker[i].name
                                    });
                                    // événement clic sur le marker
                                    google.maps.event.addListener( oMarker, 'click', function( data){
                                      // affichage position du marker
                                      oInfo.setContent( 'position :<br>' +data.latLng.toUrlValue(5));
                                      oInfo.open( oMap, oMarker);
                                    });
                                  }
                                }
                            // init lorsque la page est chargée
                            google.maps.event.addDomListener(window, 'load', initCarte);
                        </script>

                            <div id="div_main">
                              <div id="div_carte"></div>
                            </div>
                        <br>
                        mon avatar : http://www.ingall-niger.org

                        Commentaire


                        • #13
                          Re : affichage de marqueurs sur un google map

                          Ceci est plus clair et propre (enfin, il faudrait aussi veiller à envoyer le CSS et les scripts dans le head du document.

                          Code PHP:
                          <?php 
                          defined
                          ('_JEXEC') or die('Restricted access');   

                          $user JFactory::getUser(); 
                          $userid $user->id

                          $bdd JFactory::getDBO(); 
                          $query "SELECT name, lat, lng  
                              FROM #__batenergie_batenergieep 
                              WHERE author='
                          $userid'"
                              
                          $bdd->setQuery $query ); 
                              
                          $data $bdd->loadObjectList(); 
                              
                          $result = array();
                              foreach (
                          $data as $marker) {
                                  
                          // si le tableau est unidimensionnel
                                  
                          $result[] = $marker->lat 
                                  
                          $result[] = $marker->lng ;  
                                  
                          $result[] = $marker->name ;
                                  
                          /*
                                   * Si tableau bidimensionnel décommenter ce bloc
                                  $result[] = array( $marker->lat, $marker->lng, $marker->name );
                                  */
                              
                          }
                              
                          $tResult 'var dataMarker=' json_encode($result) .";\n" ;         
                          ?> 

                          <style type="text/css"> 
                          html, body { 
                            height : 100%; 
                            width:100%; 
                            margin : 0; 
                            padding : 0; 


                          #div_main { 
                            margin : auto; 
                            width : auto; 

                          #div_carte { 
                            margin : auto; 
                            width : 1000px; 
                            height : 300px; 
                            border : 1px solid #c0c0c0; 

                          </style> 

                          <hr> 
                              <div align="left"> 
                                  <h1>Implantation des postes</h1> 
                              </div> 

                          <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&language=fr"></script> 

                          <script type="text/javascript"> 
                              function initCarte() 
                              {  
                                <?php 
                                      
                          echo $tResult;
                                
                          ?> 

                                    var oMap, oMarker, oInfo; 
                                    var i, nb = dataMarker.length; 
                                    // création de la carte 
                                    oMap = new google.maps.Map( document.getElementById( 'div_carte'),{ 
                                      'center' : new google.maps.LatLng( 46.80, 1.70), 
                                      'zoom' : 6, 
                                      'mapTypeId' : google.maps.MapTypeId.ROADMAP 
                                    }); 
                                    // création infobulle 
                                    oInfo = new google.maps.InfoWindow(); 
                                    // création des markers dans une boucle 
                                    for( i = 0; i < nb; i++){ 
                                      oMarker = new google.maps.Marker({ 
                                        'position' : new google.maps.LatLng( dataMarker[i].lat, dataMarker[i].lng, 
                                        'map' : oMap, 
                                        'title' : dataMarker[i].name 
                                      }); 
                                      // événement clic sur le marker 
                                      google.maps.event.addListener( oMarker, 'click', function( data){ 
                                        // affichage position du marker 
                                        oInfo.setContent( 'position :<br>' +data.latLng.toUrlValue(5)); 
                                        oInfo.open( oMap, oMarker); 
                                      }); 
                                    } 
                                  } 
                              // init lorsque la page est chargée 
                              google.maps.event.addDomListener(window, 'load', initCarte); 
                          </script> 

                              <div id="div_main"> 
                                <div id="div_carte"></div> 
                              </div> 
                          <br />
                          Pas de demande de support par MP.
                          S'il n'y a pas de solution, c'est qu'il n'y a pas de problème (Devise Shadok)

                          Commentaire


                          • #14
                            Re : affichage de marqueurs sur un google map

                            merci un petit aménagement pour la lecture des données dnas le new google.maps.LatLng
                            et c'est ok pour moi
                            ci-desosus le script final
                            merci

                            Code:
                            <?php  
                            defined('_JEXEC') or die('Restricted access');    
                            
                            $user = JFactory::getUser();  
                            $userid = $user->id;  
                            
                            $bdd = JFactory::getDBO();  
                            $query = "SELECT name, lat, lng   
                                FROM #__batenergie_batenergieep  
                                WHERE author='$userid'";  
                                $bdd->setQuery ( $query );  
                                $data = $bdd->loadObjectList();  
                                $result = array(); 
                                foreach ($data as $marker) { 
                                    // si le tableau est unidimensionnel 
                                    /*$result[] = $marker->lat ;  
                                    $result[] = $marker->lng ;   
                                    $result[] = $marker->name ; */
                                     
                                     // Si tableau bidimensionnel décommenter ce bloc 
                                    $result[] = array( $marker->lat, $marker->lng, $marker->name ); 
                                    
                                } 
                                $tResult = 'var tMarker=' . json_encode($result) .";\n" ;          
                            ?>  
                            
                            <style type="text/css">  
                            html, body {  
                              height : 100%;  
                              width:100%;  
                              margin : 0;  
                              padding : 0;  
                            }  
                            
                            #div_main {  
                              margin : auto;  
                              width : auto;  
                            }  
                            #div_carte {  
                              margin : auto;  
                              width : 1000px;  
                              height : 300px;  
                              border : 1px solid #c0c0c0;  
                            }  
                            </style>  
                            
                            <hr>  
                                <div align="left">  
                                    <h1>Implantation des postes</h1>  
                                </div>  
                            
                            <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&language=fr"></script>  
                            
                            <script type="text/javascript">  
                                function initCarte(){
                            
                            	<?php echo $tResult; ?>
                            
                            	  var oMap, oMarker, oInfo;
                            	  var i, nb = tMarker.length;
                            	  // création de la carte
                            	  oMap = new google.maps.Map( document.getElementById( 'div_carte'),{
                            	    'center' : new google.maps.LatLng( 46.80, 1.70),
                            	    'zoom' : 6,
                            	    'mapTypeId' : google.maps.MapTypeId.ROADMAP
                            	  });
                            	  // création infobulle
                            	  oInfo = new google.maps.InfoWindow();
                            	  // création des markers dans une boucle
                            	  for( i = 0; i < nb; i++){
                            	    oMarker = new google.maps.Marker({
                            	      'position' : new google.maps.LatLng( tMarker[i][0], tMarker[i][1]),
                            	      'map' : oMap,
                            	      'title' : tMarker[i][2]
                            	    });
                            	    // événement clic sur le marker
                            	    google.maps.event.addListener( oMarker, 'click', function( data){
                            	    // affichage position du marker
                            	    oInfo.setContent( 'position :<br>' +data.latLng.toUrlValue(5));
                            	    oInfo.open( this.getMap(), this);
                            	    });
                            	  }
                            	}
                            	// init lorsque la page est chargée
                            	google.maps.event.addDomListener(window, 'load', initCarte);
                            </script>  
                            
                                <div id="div_main">  
                                  <div id="div_carte"></div>  
                                </div>  
                            <br />
                            mon avatar : http://www.ingall-niger.org

                            Commentaire

                            Annonce

                            Réduire
                            Aucune annonce pour le moment.

                            Partenaire de l'association

                            Réduire

                            Hébergeur Web PlanetHoster
                            Travaille ...
                            X