Consultez la FAQ sur le ZF avant de poster une question
Vous n'êtes pas identifié.
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 :
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
Thanks.
Hors ligne
Hey simsam!
i see only js here ![]()
Is your array eventss as you expects? (if yes we can't help you, it's a ZF forum and not js
)
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
No it's a phtml file and this is the hole script :
[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 :
[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
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:
[lang=php]
<?php foreach($eventss as $even){ ?>
location.push(['Bondi Beach', <?php echo ($even->latitude); ?>,<?php echo ($even->longitude); ?>])
<?php } ?>Hors ligne
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
You get 13 elem on eventss?
Hors ligne
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
Ok, so their are only few ways :
[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