PHP Classes

File: ex16.php

Recommend this page to a friend!
  Classes of Vagharshak Tozalakyan   Map Builder   ex16.php   Download  
File: ex16.php
Role: Example script
Content type: text/plain
Description: Example 16 - Center map on marker click.
Class: Map Builder
Display maps using Google Maps API v3
Author: By
Last change:
Date: 7 years ago
Size: 2,165 bytes
 

Contents

Class file image Download
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>MapBuilder class - Center marker on click sample</title>
</head>
<body>

<?php

// Include MapBuilder class.
require_once 'class.MapBuilder.php';

// Create MapBuilder object.
$map = new MapBuilder();

// Set API key
$map->setApiKey('AIzaSyB230QxSetZoJiM9noon7FiAQXbc-HPSLU');

// Set map's center position by latitude and longitude coordinates.
$map->setCenter(48.860181, 2.3249648);

// Set the default map type.
$map->setMapTypeId(MapBuilder::MAP_TYPE_ID_HYBRID);

// Set width and height of the map.
$map->setSize(860, 550);

// Set default zoom level.
$map->setZoom(14);

// Make zoom control compact.
$map->setZoomControlStyle(MapBuilder::ZOOM_CONTROL_STYLE_SMALL);

// Define locations and add markers with attached info windows.
$locations = array(
    array(
'Eifel Tower', 48.858278, 2.294254, '#FF7B6F', 'http://armdex.com/maps/eifel-tower.jpg', 120, 160),
    array(
'The Louvre', 48.8640411, 2.3360444, '#6BE337', 'http://armdex.com/maps/the-louvre.jpg', 160, 111),
    array(
'Musee d\'Orsay', 48.860181, 2.3249648, '#E6E325', 'http://armdex.com/maps/musee-dorsay.jpg', 160, 120),
    array(
'Jardin du Luxembourg', 48.8469529, 2.337285, '#61A1FF', 'http://armdex.com/maps/jardin-du-luxembourg.jpg', 160, 106),
    array(
'Promenade Plantee', 48.856614, 2.3522219, '#FF61E3', 'http://armdex.com/maps/promenade-plantee.jpg', 160, 120)
);
foreach (
$locations as $i => $location) {
   
$map->addMarker($location[1], $location[2], array(
       
'title' => $location[0],
       
'defColor' => $location[3],
       
'defSymbol' => ($i + 1),
       
'html' => '<div><img src="' . $location[4] . '" width="' . $location[5] . '" height="' . $location[6] . '" /></div><b>' . $location[0] . '</b>',
       
'infoCloseOthers' => true
   
));
}

// Display the map.
$map->show();

?>

<script>
/* Create callback to center the map when marker is clicked. */
function mbOnAfterInit(map) {
    for (var i = 0; i < markers.length; i++) {
        google.maps.event.addListener(markers[i], "click", function() {
            map.setCenter(this.getPosition());
        });
    }
}
</script>

</body>
</html>