Skip to content

Commit

Permalink
Add support for waypoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
Olicek committed Apr 9, 2015
1 parent b3b8b14 commit 1d4a3e2
Show file tree
Hide file tree
Showing 12 changed files with 524 additions and 225 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2014, Petr Olišar
Copyright (c) 2015, Petr Olišar
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
203 changes: 8 additions & 195 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,210 +1,23 @@
GoogleAPI
Google Map API
=========
This component is for Nette framework and make Google map display easier

[Google map API](http://dev.olisar.eu/google-map-api/api/2.0)
This component is stated for Nette framework and it simlifies working with a Google map.

Requirements
============
* Nette Framework 2.1+
* Jquery 1.8+

Installation
============

composer require olicek/google-map-api:dev-master

then you can enable the extension using your neon config

extensions:
map: Oli\GoogleAPI\MapApiExtension

Configuration in neon file
==========================

You can define your map and all markers in in config neon file. All configuration in neon is optional.
```yml
map:
key: your_key # your personal key to google map
zoom: 2
width: 300px # map will 300px width
height: 150px # map will 150px height
coordinates: # you can name keies as you whis or use [49, 15]
lat: 49.8037633
lng: 15.4749126
type: SATELLITE # type of map
markers: # this section will be configured amrkers
bound: on # zoom as mutch as possible, but every marker will be displaied
markerClusterer: on # neer markers group to one cluster
addMarkers: # definitions of markers
- # first marker has no name
coordinates: # the same as map coordinates
sirka: 47
vyska: 15
icon: images/point.png # it will display png image from www/images/point.png
message:
text: Opened message # text of message
autoOpen: on # if it is on, this message will be displaied after map loaded
1: # second marker has name 1
coordinates: [46, 13]
animation: DROP # this marker will has drop animation
title: Hello world
icon: images/point2.png
Prague: # last marker has name Prague
coordinates:
lat: 48
lng: 15
animation: DROP
message:
Closed message
autoOpen: off # message has autoOpen default off
```

Default values can be seen in [MapApiExtension](https://github.com/Olicek/GoogleMapAPI/blob/master/src/DI/MapApiExtension.php#L19-L31)

Simplest usage
==============

To your presenter include (if you have PHP 5.4+)

``` php
class MapPresenter extends Nette\Application\UI\Presenter
{
use \Oli\GoogleAPI\TMap;

// ...
}
```
and to template

{control map}


Define map in component
=======================
``` php
private $map;
private $markers;
and now the component can be registered in extensions in a neon config

public function __construct(\Oli\GoogleAPI\IMapAPI $mapApi, \Oli\GoogleAPI\IMarkers $markers)
{
$this->map = $mapApi;
$this->markers = $markers;
}

public function createComponentMap()
{
$map = $this->map->create();
$markers = $this->markers->create();

// ...

$map->addMarkers($markers);
return $map;
}
```
And in template

{* JS and HTML *}
{control map}
{* just HTML *}
{control map:HTML}
{* just JS *}
{control map:JS}

Map options
-----------
``` php
protected function createComponentGoogleMap()
{
$map->setCoordinates(array(41.15, 15.65))
$map->setProportions('800px', '480px'); // Default is 100% x 100%. If proportion is default, map must be
// wrapped to some concrete proportions otherwise it will not be displayed.
$map->setKey('your_key');
$map->setZoom(8); // <0, 19>
$map->setType(MapAPI::SATELITE);
$map->isStaticMap(); // Map will be displayed as img. (To the image can be inserted colored) markers.
$map->addMarkers($markers); // give markers to the map
return $map;
}
```
Markers options
---------------
``` php
$markers = $this->markers->create();

/**
* Put marker to the coordinate
* Optional animation, can be DROP, BOUNCE, false
* Optional title can be string|null
*/
$markers->addMarker(array(50.250718,14.583435), false, null);

/**
* Message can contains HTML tags
* Optional auto open: auto open the message
* Static map can not display messages
*/
$markers->setMessage('<h1>Hello world</h1>', false);
$markers->deleteMarkers();
$markers->isMarkerClusterer(); // neer markers group to one cluster
$markers->fitBounds(); // zoom as mutch as possible, but every marker will be displaied
$markers->setColor('red'); // Can set color of markers to 10 diferent colors
$markers->setDefaultIconPath('img/'); // Path which will be prepend icon path

/**
* Icon from www folder.
* If default path was not set, setIcon would be '/www/someIcon.png'
*/
$markers->setIcon('someIcon.png');
```
Complex example
---------------

config.neon

map:
key: my_key
width: 750px
height: 450px
markers:
defaultIconPath: images/mapPoints

Presenter
``` php
protected function createComponentGoogleMap()
{
$map = $this->map->create();

$map->setCoordinates(array(50.250718,14.583435))
->setZoom(4)
->setType(MapAPI::TERRAIN);

$markers = $this->markers->create();
$markers->fitBounds();

if(count($markersFromDb) > 30)
{
$markers->isMarkerClusterer();
}

foreach ($markersFromDb as $marker)
{
$markers->addMarker(array($marker->lat, $marker->lng), Marker::DROP)
->setMessage(
'<h1>'.$marker->title.'</h1><br />'.$marker->description
)->setIcon($marker->icon);
}
$map->addMarkers($markers);
return $map;
}
extensions:
map: Oli\GoogleAPI\MapApiExtension
```
Template

{block content}
{control map:HTML}
{/block}
The last step is to link `client-side/googleMapAPI.js` to your page.

{block scripts}
{control map:JS}
{/block}
[Documentation](https://github.com/Olicek/GoogleMapAPI/tree/master/docs)
88 changes: 72 additions & 16 deletions client-side/googleMapAPI.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
/**
* Copyright (c) 2015 Petr Olišar (http://olisar.eu)
*
* For the full copyright and license information, please view
* the file LICENSE.md that was distributed with this source code.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/


var GoogleMap = GoogleMap || {};

GoogleMap = function(element)
{
this.element = element;
this.map;
this.directionsDisplay;
this.directionsService;
this.markers = [];
this.options = {};
this.boundsProperty;
Expand Down Expand Up @@ -43,6 +45,7 @@ GoogleMap.prototype = {
this.options.key = properties.key;
this.options.bound = properties.bound;
this.options.cluster = properties.cluster;
this.options.waypoints = properties.waypoint;

this.URL = this.element.dataset.markersfallback;

Expand All @@ -66,6 +69,11 @@ GoogleMap.prototype = {
base.map = new google.maps.Map(base.element, mapOptions);
base.map.setTilt(45);
base.loadMarkers();

if (base.options.waypoints !== null)
{
base.drawDirections();
}
},

loadMarkers: function()
Expand Down Expand Up @@ -96,6 +104,64 @@ GoogleMap.prototype = {
request.send();
},

drawDirections: function ()
{
var base = this;
if (base.options.waypoints.start === undefined ||
base.options.waypoints.end === undefined) {
console.log('You must define start and end point of the way!');
}
var start = base.options.waypoints.start;
var end = base.options.waypoints.end;
var waypoints = [];

if (base.options.waypoints.waypoints !== undefined) {
for (var i = 0; i < base.options.waypoints.waypoints.length; i++) {
waypoints.push({
location: new google.maps.LatLng(
base.options.waypoints.waypoints[i].position[0],
base.options.waypoints.waypoints[i].position[1]),
stopover:true});
}
}

base.directionsDisplay = new google.maps.DirectionsRenderer();
base.directionsService = new google.maps.DirectionsService();
base.directionsDisplay.setMap(base.map);
var request = {
origin: new google.maps.LatLng(start.position[0], start.position[1]),
destination: new google.maps.LatLng(end.position[0], end.position[1]),
waypoints: waypoints,
optimizeWaypoints: true,
travelMode: google.maps.TravelMode[base.options.waypoints.travelmode]
};

function merge_options(obj1,obj2){
var obj3 = {};
for (var attrname in obj1) {
if (attrname !== 'start' && attrname !== 'end' &&
attrname !== 'travelmode') {
obj3[attrname] = obj1[attrname];
}
}
for (var attrname in obj2) {
if (attrname !== 'start' && attrname !== 'end' &&
attrname !== 'travelmode' && attrname !== 'waypoints') {
obj3[attrname] = obj2[attrname];
}
}
return obj3;
}

request = merge_options(request, base.options.waypoints);
base.directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
base.directionsDisplay.setDirections(response);
var route = response.routes[0];
}
});
},

insertMarkers: function(markers)
{
var base = this;
Expand Down Expand Up @@ -189,17 +255,7 @@ GoogleMap.prototype = {
var animation;
if ("animation" in marker)
{
switch(marker['animation'])
{
case 'DROP':
animation = google.maps.Animation.DROP;
break;
case 'BOUNCE':
animation = google.maps.Animation.BOUNCE;
break;
default:
null;
}
animation = google.maps.Animation[marker.animation];
}

return animation;
Expand Down
Loading

0 comments on commit 1d4a3e2

Please sign in to comment.