Google Map API – chương 4


Building a Store Location Map
Bây giờ chúng ta sẽ gán store của chúng ta lên google maps .
File generate XML là map_data.php

<?php
// Open the ronjons_cache.xml file and load the data for the pins
$datafile = simplexml_load_file("ronjons_cache.xml");
echo "var markers = [\n";
foreach ($datafile->store as $store) {
$description = "{$store->address}<br />";
if ($store->address2) $description .= "{$store->address2}
";
$description .= "{$store->city}, {$store->state}
";
$description .= "{$store->zip}
";
$description .= "Phone: {$store->phone}
";
echo "{
'latitude': {$store->latitude},
'longitude': {$store->longitude},
'name': '{$store->name}',
'description': '$description'
},\n";
}
echo "];\n";
?>

Kết quả xuất ra có dạng

var markers = [
{
'latitude': 39.649652,
'longitude': -74.177547,
'name': '"The Original" Ron Jon Surf Shop',
'description': '901 Central Avenue<br />Long Beach Island,➥
NJ
08008
Phone: (609) 494-8844
'
}, {
'latitude': 28.356577,
'longitude': -80.608069,
'name': '"One of a Kind" Ron Jon Surf Shop',
'description': '4151 North Atlantic Avenue<br />Cocoa Beach,➥
FL
32931
Phone: (321) 799-8888
'
}, {
'latitude': 26.156292,
'longitude': -80.316945,
'name': 'Ron Jon Surf Shop - Sunrise',
'description': '2610 Sawgrass Mills Circle<br />Suite 1415
Sunrise,➥
FL
33323
Phone: (954) 846-1880
'
}, {
'latitude': 28.469972,
'longitude': -81.450143,
'name': 'Ron Jon Surf Shop - Orlando',
'description': '5160 International Drive<br />Orlando,➥
FL
32819
Phone: (407) 481-2555
'
}, {
'latitude': 24.560448,
'longitude': -81.805998,
'name': 'Ron Jon Surf Shop - Key West',
'description': '503 Front Street<br />Key West,➥
FL
33040
Phone: (305) 293-8880
'
}, {
'latitude': 33.783329,
'longitude': -117.890562,
'name': 'Ron Jon Surf Shop - California',
'description': '20 City Blvd.<br />West Building C Suite 1
Orange,➥
CA
92868
Phone: (714) 939-9822
'
}, {
'latitude': 28.40168,
'longitude': -80.59774,
'name': 'Ron Jon Cape Caribe Resort',
'description': '1000 Shorewood Drive<br />Cape Canaveral,➥
FL
32920
Phone: (321) 328-2830
'
},
];

Và file map_functions.js sẽ xuất icon và info Windows ra :

var centerLatitude = 40.6897;
var centerLongitude = -95.0446;
var startZoom = 3;
var map;
var RonJonLogo = new GIcon();
RonJonLogo.image = 'ronjonsurfshoplogo.png';
RonJonLogo.iconSize = new GSize(48, 24);
RonJonLogo.iconAnchor = new GPoint(24, 14);
RonJonLogo.infoWindowAnchor = new GPoint(24, 24);
function addMarker(latitude , longitude, description) {
var marker = new GMarker(new GLatLng(latitude, longitude), RonJonLogo);
GEvent.addListener(marker, 'click',
function() {
marker.openInfoWindowHtml(description);
}
);
map.addOverlay(marker);
}
function init() {
map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
for(id in markers) {
addMarker(markers[id].latitude , markers[id].longitude,
markers[id].description);
}
}
window.onload = init;

Kết quả của chúng ta sau khi xuất ra là :

Advertisements
%d bloggers like this: