bonne nuit, im essayant d'afficher des marqueurs sur une carte avec JSON mais je ne suis pas capable de et je ne sais pas pourquoi, je peux voir la carte et peut se déplacer mais pas même un marqueur est affiché,marqueurs mysql ne figurant pas sur la carte
<?php
require("config.php");
/* lat/lng data will be added to this array */
$locations=array();
$query = $db->query('SELECT * FROM inmuebles');
while($row = $query->fetch()){
$longitude = $row['long'];
$latitude = $row['lat'];
/* Each row is added as a new array */
$locations[]=array('lat'=>$latitude, 'long'=>$longitude);
}
/* Convert data to json */
$markers = json_encode($locations);
echo $markers;
?>
<script type='text/javascript'>
<?php
echo "var markers=$markers;\n";
?>
var map;
var markersArray = [];
function initMap() {
var latlng = new google.maps.LatLng(-16.5338955,-68.0656364);
var myOptions = {
zoom: 13,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
};
var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
var infowindow = new google.maps.InfoWindow(), marker, lat, long;
var json=JSON.parse(markers);
for(var o in json){
lat = json[ o ].lat;
long=json[ o ].long;
marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,long),
map: map
});
google.maps.event.addListener(marker, 'click', function(e){
infowindow.setContent(this.name);
infowindow.open(map, this);
}.bind(marker));
}
}
</script>
la sortie de marqueurs $ est
[{"lat":"-16.52629052070058","long":"-68.0797004699707"},{"lat":"-16.500122130208325","long":"-68.12089920043945"},{"lat":"-16.54307592346882","long":"-68.06425094604492"}]
merci à l'avance que j'ai essayé vraiment difficile de voir où l'erreur est
EDIT
<?php
$con = mysqli_connect('localhost','waru','olairhead154','inmueble');
?>
<?php
require("config.php");
/* lat/lng data will be added to this array */
$locations=array();
$query = $db->query('SELECT * FROM inmuebles');
while($row = $query->fetch()){
$longitude = $row['long'];
$latitude = $row['lat'];
/* Each row is added as a new array */
$locations[]=array('lat'=>$latitude, 'long'=>$longitude);
}
/* Convert data to json */
$markers = json_encode($locations);
echo $markers;
?>
<script type='text/javascript'>
<?php
echo "var markers=$markers;\n";
?>
var map;
var markersArray = [];
function initMap() {
var latlng = new google.maps.LatLng(-16.5338955,-68.0656364);
var myOptions = {
zoom: 13,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
};
var markers = '<?= json_encode($markers) ?>';
var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
var infowindow = new google.maps.InfoWindow(), marker, lat, long;
var json=markers;
for(var o in json){
lat = json[ o ].lat;
long=json[ o ].long;
marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,long),
map: map
});
google.maps.event.addListener(marker, 'click', function(e){
infowindow.setContent(this.name);
infowindow.open(map, this);
}.bind(marker));
}
}
google.maps.event.addDomListener(window, "load", initMap);
</script>
ce sont les changements que je faisais, mais encore aucun marqueur montré im vraiment nouveau à ce s'il vous plaît soyez patient avec moi
grâce
La "sortie de marqueurs $" est pas une chaîne JSON, il est un tableau javascript, 'JSON.parse' ne fonctionnera pas sur elle. Passez-le directement dans votre boucle. ([Proof of concept fiddle] (http://jsfiddle.net/geocodezip/khe9eqj4/1/)) – geocodezip
@geocodezip a essayé ce que tu m'as dit mais je ne peux toujours pas voir les marqueurs, peux-tu m'aider un peu plus? var markers = ' = Json_encode ($ marqueurs)?>'; var map = new google.maps.Map (document.getElementById ("map_canvas"), myOptions); var infowindow = new google.maps.InfoWindow(), marqueur, lat, long; var json = marqueurs; pour (var o in json) { lat = json [o] .lat; long = json [o] .long; marker = new google.maps.Marqueur ({ position: new google.maps.LatLng (lat, long), carte: carte }); –
1. s'il vous plaît ne pas poster (de grandes quantités de) code dans les commentaires, il n'est pas lisible ([modifier] votre question pour répondre aux commentaires). 2. Si je lis ce que vous avez posté, ce n'est pas ce que je "vous ai dit", s'il vous plaît regardez le violon. – geocodezip