Zend FR

Consultez la FAQ sur le ZF avant de poster une question

Vous n'êtes pas identifié.

#1 01-04-2015 11:35:08

simsam
Nouveau membre
Date d'inscription: 01-04-2015
Messages: 5

Get latitude and longitude from database into an array

Hello evryone, i'm developping my first zf2 application but i have a problem with the javascript function. I want to display on my map the points of my events using latitude and longitude. However, when i try with a static points all works fine, but when i put some variables from database, no results in my map.

This is my code :

Code:

function geolocationSuccess() {
<?php foreach ($eventss as $even) : ?>
// alert(<?php echo ($even->latitude); ?>);
//alert(<?php echo ($even->longitude); ?>);
var locations = [
['Bondi Beach', <?php echo ($even->latitude); ?>,<?php echo ($even->longitude); ?>],
['Maroubra Beach', 43.5528470, 7.0173690],
['Allmagne', 51.16569, 10.45153],
['Marroc', 31.79170, -7.09262]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: new google.maps.LatLng(46.22, 2.36),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
<?php endforeach; ?>
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});

google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
}

Any help please smile Thanks.

Hors ligne

 

#2 01-04-2015 11:56:24

flobrflo
Membre
Lieu: Marseille
Date d'inscription: 26-04-2013
Messages: 376

Re: Get latitude and longitude from database into an array

Hey simsam!

i see only js here sad
Is your array eventss as you expects? (if yes we can't help you, it's a ZF forum and not js wink )
If not, give us the way you get $eventss too let us help you.

Dernière modification par flobrflo (01-04-2015 11:56:42)

Hors ligne

 

#3 01-04-2015 12:08:48

simsam
Nouveau membre
Date d'inscription: 01-04-2015
Messages: 5

Re: Get latitude and longitude from database into an array

No it's a phtml file and this is the hole script :

Code:

 
[lang=php]<body>
<div class="map" id="map" style="overflow:hidden;">
</div>
</body>
<script>
            function writeAddressName(latLng) {
                var geocoder = new google.maps.Geocoder();
                geocoder.geocode({
                    "location": latLng
                },
                function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK)
                        document.getElementById("address").innerHTML = results[0].formatted_address;
                    else
                        document.getElementById("error").innerHTML += "Unable to retrieve your address" + "<br />";
                });
            }
            function geolocationSuccess() {
<?php foreach ($eventss as $even) : ?>
                    //  alert(<?php echo ($even->latitude); ?>);
                    //alert(<?php echo ($even->longitude); ?>);
                    var locations = [
                        ['Bondi Beach', <?php echo ($even->latitude); ?>,<?php echo ($even->longitude); ?>],
                        ['Maroubra Beach', 43.5528470, 7.0173690],
                        ['Tunis, Tunisia', 33.88692, 9.53750],                       
                    ];

                    var map = new google.maps.Map(document.getElementById('map'), {
                        zoom: 5,
                        center: new google.maps.LatLng(46.22, 2.36),
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    });
<?php endforeach; ?>
                var infowindow = new google.maps.InfoWindow();
                var marker, i;
                for (i = 0; i < locations.length; i++) {
                    marker = new google.maps.Marker({
                        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                        map: map
                    });
                    google.maps.event.addListener(marker, 'click', (function (marker, i) {
                        return function () {
                            infowindow.setContent(locations[i][0]);
                            infowindow.open(map, marker);
                        }
                    })(marker, i));
                }
            }
            function geolocationError(positionError) {
                document.getElementById("error").innerHTML += "Error: " + positionError.message + "<br />";
            }
            function geolocateUser() {
                // If the browser supports the Geolocation API
                if (navigator.geolocation)
                {
                    var positionOptions = {
                        enableHighAccuracy: true,
                        timeout: 10 * 1000 // 10 seconds
                    };
                    navigator.geolocation.getCurrentPosition(geolocationSuccess, geolocationError, positionOptions);
                    // navigator.geolocation.getCurrentPosition(geolocationSuccess1, geolocationError, positionOptions);
                }
                else
                    document.getElementById("error").innerHTML += "Your browser doesn't support the Geolocation API";
            }
            window.onload = geolocateUser;
        </script>

and here is the actionController :

Code:

[lang=php] public function calendarAction() {

        $language = $this->params()->fromRoute('lang', 'en_US');
        $language = explode('_', $language);
        $lang = $language[0];
        $this->layout()->setVariable('lang', $this->params()->fromRoute('lang', 'en_US'));
        $this->layout()->setVariable('action', $this->params()->fromRoute('action', 'index'));
        $this->layout()->setVariable('id', $this->params()->fromRoute('id', ''));

        $Events = array();
        $Eventss = array();
        //$taches=array();
        $events = $this->getEventTable()->fetchAll(null);
        foreach ($events as $a) {
            $Events[] = $a;
        }
        $eventss = $this->getEventTable()->fetchAll();
        foreach ($eventss as $l) {
            $Eventss[] = $l;
        }
        return (array(
            'events' => $Events,
            'eventss' => $Eventss,
        ));
    }

Dernière modification par simsam (02-04-2015 14:04:41)

Hors ligne

 

#4 01-04-2015 13:39:14

flobrflo
Membre
Lieu: Marseille
Date d'inscription: 26-04-2013
Messages: 376

Re: Get latitude and longitude from database into an array

In your code you get :

var locations = [...]

so you reset all the times the value, i think you want to push every time, so something like that:

Code:

[lang=php]
<?php foreach($eventss as $even){ ?>
     location.push(['Bondi Beach', <?php echo ($even->latitude); ?>,<?php echo ($even->longitude); ?>])
<?php  } ?>

Hors ligne

 

#5 01-04-2015 18:35:25

simsam
Nouveau membre
Date d'inscription: 01-04-2015
Messages: 5

Re: Get latitude and longitude from database into an array

I tried with these and i searched what exatly the problem. But each time with the locations.push, only 5 points diplay on my map. however, i have 13 points on my database.

Hors ligne

 

#6 01-04-2015 23:19:34

flobrflo
Membre
Lieu: Marseille
Date d'inscription: 26-04-2013
Messages: 376

Re: Get latitude and longitude from database into an array

You get 13 elem on eventss?

Hors ligne

 

#7 02-04-2015 09:55:56

simsam
Nouveau membre
Date d'inscription: 01-04-2015
Messages: 5

Re: Get latitude and longitude from database into an array

Hello, yes when i put the alert the display the points whitch i have in my database i have 13 alerts for points, but only 5 points are in my map. i don't know what's the problem!

Hors ligne

 

#8 02-04-2015 12:04:05

flobrflo
Membre
Lieu: Marseille
Date d'inscription: 26-04-2013
Messages: 376

Re: Get latitude and longitude from database into an array

Ok, so their are only few ways :

Code:

[lang=php] 
1) $even->latitude || $even->longitude //aren't float
2) $even->latitude || $even->longitude //wron place : too far or same position
3) error js wich broke your loop

if not, can't help you more xD

Hors ligne

 

Pied de page des forums

Propulsé par PunBB
© Copyright 2002–2005 Rickard Andersson
Traduction par punbb.fr

Graphisme réalisé par l'agence Rodolphe Eveilleau
Développement par Kitpages