stefano861
31-01-2014, 17:30
Ciao, devo creare una pagina php che mostra, per ogni indirizzo contenuto in un array, una mappa Google. Il codice scritto da me non va, quello che vedo è questo http://imageshack.com/a/img855/5204/mt1a.png
cioè una colonna di mappe grige, a parte l'ultima che però non è il massimo come si può notare dall'immagine.
Questo è il codice html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="../public/css/style.css"/>
<script type="text/javascript" src="../public/js/javascript.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
//$addresses è un array PHP che contiene gli indirizzi, con la funzione json_encode trasformo l'array php in un array javascript
<script type='text/javascript'>
var addresses = <?php echo json_encode($addresses); ?>;
</script>
<script type="text/javascript" src="../public/js/maps.js"></script>
</head>
<body>
<!-- Google maps -->
<div id="content_map-canvas"></div>
</body>
</html>
E il codice javascript:
function initialize() {
var geocoder;
var map;
var count = 0; //Is used to number the div (one per address)
var descriptions = new Array(); //Array of descriptions
//I copy the contents of the addresses array in the descriptions array
for(var i=0; i<addresses.length; i++) {
var address = addresses;
var description = addresses[i];
geocoder = new google.maps.Geocoder();
var options = {
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
count = count + 1;
var id = "map-canvas" + count;
var div = document.createElement("div");
div.id = id;
div.style.width= "300px";
div.style.height= "300px";
var br = document.createElement("br");
var content_map_canvas = document.getElementById("content_map-canvas");
content_map_canvas.appendChild(div);
content_map_canvas.appendChild(br);
map = new google.maps.Map(document.getElementById(id), options);
//geocoding(address, description);
geocoder.geocode({'address': address}, function(results, status) {
if(status === google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker
({map: map,
position: results[0].geometry.location,
title: description
});
marker.setAnimation(google.maps.Animation.DROP);
contentString = description;
var infowindow = new google.maps.InfoWindow({
content: contentString
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
}
else {
alert("Geocode failed: " + status + ", " + address);
}
});
}
La console di Chrome mi da i seguenti errori:
Uncaught TypeError: Cannot call method 'lat' of null at maps.php:12
E...
[I] Uncaught TypeError: Cannot call method 'lat' of null VM51:12
(anonymous function) VM51:12
T.trigger main.js:17
H.lc VM48:132
(anonymous function) main.js:11
T.trigger main.js:17
H.yk VM48:117
(anonymous function) main.js:18
http://imageshack.com/a/img835/1916/ph6t.png
Dove sbaglio? Non capisco..
Grazie a tutti
cioè una colonna di mappe grige, a parte l'ultima che però non è il massimo come si può notare dall'immagine.
Questo è il codice html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<link rel="stylesheet" type="text/css" href="../public/css/style.css"/>
<script type="text/javascript" src="../public/js/javascript.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
//$addresses è un array PHP che contiene gli indirizzi, con la funzione json_encode trasformo l'array php in un array javascript
<script type='text/javascript'>
var addresses = <?php echo json_encode($addresses); ?>;
</script>
<script type="text/javascript" src="../public/js/maps.js"></script>
</head>
<body>
<!-- Google maps -->
<div id="content_map-canvas"></div>
</body>
</html>
E il codice javascript:
function initialize() {
var geocoder;
var map;
var count = 0; //Is used to number the div (one per address)
var descriptions = new Array(); //Array of descriptions
//I copy the contents of the addresses array in the descriptions array
for(var i=0; i<addresses.length; i++) {
var address = addresses;
var description = addresses[i];
geocoder = new google.maps.Geocoder();
var options = {
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
count = count + 1;
var id = "map-canvas" + count;
var div = document.createElement("div");
div.id = id;
div.style.width= "300px";
div.style.height= "300px";
var br = document.createElement("br");
var content_map_canvas = document.getElementById("content_map-canvas");
content_map_canvas.appendChild(div);
content_map_canvas.appendChild(br);
map = new google.maps.Map(document.getElementById(id), options);
//geocoding(address, description);
geocoder.geocode({'address': address}, function(results, status) {
if(status === google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker
({map: map,
position: results[0].geometry.location,
title: description
});
marker.setAnimation(google.maps.Animation.DROP);
contentString = description;
var infowindow = new google.maps.InfoWindow({
content: contentString
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
}
else {
alert("Geocode failed: " + status + ", " + address);
}
});
}
La console di Chrome mi da i seguenti errori:
Uncaught TypeError: Cannot call method 'lat' of null at maps.php:12
E...
[I] Uncaught TypeError: Cannot call method 'lat' of null VM51:12
(anonymous function) VM51:12
T.trigger main.js:17
H.lc VM48:132
(anonymous function) main.js:11
T.trigger main.js:17
H.yk VM48:117
(anonymous function) main.js:18
http://imageshack.com/a/img835/1916/ph6t.png
Dove sbaglio? Non capisco..
Grazie a tutti