Για να ξεκινήσετε μια εφαρμογή Google Maps με PHP, πρέπει να δημιουργήσετε ένα σενάριο PHP που θα εξάγει τα δεδομένα που χρειάζεται η εφαρμογή Google Maps σε μορφή XML.Στη συνέχεια, πρέπει να δημιουργήσετε τη λειτουργικότητα της εφαρμογής Google Maps με HTML και Javascript και να καλέσετε το σενάριο PHP για να ανακτήσετε τα δεδομένα που χρειάζονται για τον χάρτη. Προαιρετικά, μπορείτε να ενθυλάκωσετε ολόκληρη την εφαρμογή Google Maps μέσα σε ένα αρχείο PHP, αντί για ένα αρχείο HTML, για να συμπεριλάβετε πρόσθετη λογική.
<?php
class Coordinate {
public $latitude, $longitude, $description;
public function __construct($lat, $lng, $description) {
$this->latitude = $lat;
$this->longitude = $lng;
$this->description = $description;
}
}class Coordinates {
protected $coordinates;
public function __construct() {
$this->coordinates = array();
}
public function add($name, $description, $lat, $lng) {
$this->coordinates[$name] = new Coordinate($lat, $lng, $description);
}
public function output_xml() {
$xml = new DOMDocument("1.0");
$node = $xml->createElement("markers");
$root = $xml->appendChild($node);
foreach ($this->coordinates as $key => $value) {
$node = $xml->createElement("marker");
$newnode = $root->appendChild($node);
$newnode->setAttribute("city", $key);
$newnode->setAttribute("lat", $value->latitude);
$newnode->setAttribute("lng", $value->longitude);
$newnode->setAttribute("desc", $value->description);
}
echo $xml->saveXML();
}
}$cities = new Coordinates();
$cities->add("Nassau", "Bahamas", "25.08333", "-77.3333");
$cities->add("Cairo", "Egypt", "36.01667", "31.2333");
$cities->add("Paris", "France", "48.83333", "2.33333");
$cities->add("Beijing", "China", "39.91667", "116.3333");
$cities->add("Santiago", "Chile", "-33.40000", "-70.66667");
$cities->output_xml();
?><?php
$custom_name = "Steve";
?>
<!DOCTYPE html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $custom_name; ?>'s Map of International Cities</title><script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY" type="text/javascript"></script>
Σημείωση: Το Google Maps API απαιτεί τώρα API key. Το παλιό URL χωρίς key δεν λειτουργεί πλέον.
<script type="text/javascript">
var map;function create() {
var newYork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
var mapOptions = {zoom:13, center:newYork, mapTypeId:"roadmap"};
map = new google.maps.Map(document.getElementById("map"), mapOptions);
infoWindow = new google.maps.InfoWindow;
downloadPHPData("coordinates.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i=0; ifunction downloadPHPData(URL, callback) {
var request = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', URL, true);
request.send(null);
}
function doNothing() {}function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, "click", function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}div για τον χάρτη και να εκκινήσετε τη συνάρτηση create όταν φορτώνει η σελίδα. Π.χ., πληκτρολογήστε:
</script>
</head>
<body onload="create()">
<div id="map" style="width:800px; height:800px"></div>
</body>
</html>Πνευματικά δικαιώματα © Γνώση Υπολογιστών Όλα τα δικαιώματα διατηρούνται.