What is this repository for?
- This product is a wrapper for google maps api based on angular.
- Stable Version 0.1.14
For developers
- node.js and npm
npm install grunt-cli -g npm install bower -g npm install grunt logicifyGmap
Usage
JSFiddle example
bower install logicify-gmap
or
npm install logicify-gmap
At first you need include google api reference into your html file.
Something like that:
You need to be sure that this api is loaded before angular.js
Inject module into your angular app
var app = angular;
Inject map (directive)
place it in your html file
- center - center map (is LatLng object from google api);
- gm-options - (javascript object) is google maps options google api
- gm-ready - (function) callback function fires when directive is rendered and passes on gmap Object.
From controller
var app = angular;app;
- css-options - is javascript object is needed for injecting css into map element
Custom X (close) button for info window.
where .gm-style-iw is a class of container element, and next div is close button!
Inject map controls (directive)
html
Push me
script
$scopeindex = 1;$scopeposition = googlemapsControlPositionBOTTOM_LEFT;$scopecontrolEvents = { //it's google.maps.event } { //this method binds on file input inside logicify-gmap-control directive //it's DOM event } ;
controlEvents - it's just a javascript object. Each key should be an event name ('click','mouseover'...) and value is a callback function.
Info window
It's angular supported. So you can use angular inside info window template (directives, scope, controller...).
Inject 'InfoWindow' service from 'logicify-gmap' api.
module;
template.html
{{$infoWND.anchor.id}} Close me
- when you try to create info window object
var infowindow = templateUrl: 'template.html';
It's not an infowindow yet. Because rendering template and apply scope digest takes some time.
infowindow;
And now 'wnd' is info window object.
you can use $infoWND object in the template.html. $infoWND.anchor is a marker!
Many info windows in one time:
$scope { { var infowindow = templateUrl: 'template.html'; //create new infowindow for each marker googlemapsevent; } //loop all markers for var i = 10; i < 15; i++ var pos = -1 + 1 / i 1 + 1 / i;//random position var marker = //create new marker id: 'marker_' + i name: 'is_' + i position: pos map: map ; $scopemarkers; ;//attach listener };
If you want more than one info window on google map, you just need create it for each marker!
var infowindow = templateUrl: 'template.html'; //in the loop
Closing info window can be done in two ways:
- Destroy scope and element. Please careful with this param, because to render it again - takes more time then just apply scope digest.
infowindow
- Just hide window (proper way).
infowindow;
Note that after opening info window (wnd.open(map)) you can access to wnd.$scope (property "$scope" only, not "scope")!
why only after opening? Because "open" method rewrites infowindow scope and applies digest. So you can access the scope only after calling method "open".
XML overlays support
There is a way to display xml overlays on google map using "xml-overlays" directive. Note that we are using geoxml3 library to parse xml files. XML files can be: .zip, .kmz, .kml, or just a string. Kml\kmz object can contain:
var list = url:'some url of kml or kmz file here' file: instance of Blob here content: just string value
Basic usage:
include this file to your html:
- geoxml3.js
HTML
Push me
JS
app;
Events
var kmlEvents = { //doc - array with documents } { } { } { //all mvc objects has methods "get" & "set" } { };
Also you can include all events from geoxml3 lib
Options:
all options described on geoxml3 repository There is one more option fit-bounds-afterAll
This option is true by default. When you are disabling this option the last layer will be displayed on the map. To view all layers you need modify zoom and center of the map by mouse. If this options is enabled, then all layers will be displayed on the map, and you don't need to scroll and dragging the map to view all layers
Progress callback
Html example
Progress object structure
{ progress = done: Integer errors: Integer total: Integer ;}
Progress callback calls each time when xml file downloaded and parsed (or parsing is failed).
Infowindow
You can create and inject infowindow to your overlays. But if you want to be able to access overlay MVC object from infowindow scope then you need just add property to infowindow object.
$scopeoverlaysInfowindow = templateUrl: 'infowindow.html'; $scopeoverlaysInfowindow; //w8 for downloading template { wnd { //method "open" of infowindow calls by geoxml3 parser, so you don't need call "infowindow.open(map,marker)" like for markers wnd$scopemvcObject = gObj; //when infowindow opened then calls "$onOpen" callback with google mvc object gObj; }; }
HTML
{{mvcObject.title}} {{mvcObject.get('fillColor')}} ...etc
see more information about google mvc object
Load kml\kmz file via HTML file input:
How it works
The kml\kmz collection is instance of "SmartCollection" (you can inject it just by adding "SmartCollection"-service to your dependency). SmartCollection - is angular service. SmartCollection instanceOf Array === true.
var file1 = url:'http://some url' file2 = url:'http://some url' file3 = url:'http://some url';scopekmlCollection = file1file2file3; //create new collectionscopekmlCollection = file1 file2 file3 //delete old collection and crete new one (all those files will be downloaded again)scopekmlCollection; //only this file will be downloaded and parsed.//next examplescopekmlCollection // delete last 2 items (file1 wouldn't be reloaded)scopekmlCollection = file1; //reload file1 and delete rest
Drawing support
####### First of all you need to see
Usage:
- Add dependency injection for google maps drawing api (taken from google maps drawing spec):
As can you see dependency injection it's just url param.
- Include this html into gmap directive:
- Implement JS logic to be able manipulate the map (controller code):
scopedraw = //put all drawing things in one place events: drawing: {} overlays: { var self = this; //note that "this" can be overlay or marker, you need to be careful, because marker doesn't have "center" property and overlay has. if scopeoverlaysInfowindow scopeoverlaysInfowindow; } //see google maps spec about drawing options options: drawingMode: googlemapsdrawingOverlayTypeMARKER drawingControl: true drawingControlOptions: position: googlemapsControlPositionTOP_CENTER drawingModes: googlemapsdrawingOverlayTypeMARKER googlemapsdrawingOverlayTypeCIRCLE googlemapsdrawingOverlayTypePOLYGON googlemapsdrawingOverlayTypePOLYLINE googlemapsdrawingOverlayTypeRECTANGLE markerOptions: icon: 'beachflag.png' circleOptions: fillColor: '#ffff00' fillOpacity: 1 strokeWeight: 5 editable: true zIndex: 1 ; scopecssOpts = width: '80%' height: '60%' 'min-width': '400px' 'min-height': '200px' ; scopegmOpts = zoom: 16 center: -1 1 ; { /** * Redraw overlay */ mvcObject; mvcObject; } scope { scopegmap = map; scopeoverlaysInfowindow = templateUrl: 'infowindow.html'; };
Custom lines (requires drawing manager)
If you want custom lines, overlay borders then you need to do the next:
- include css file to your html (gmap-minimum-ui.css) to allow dropdown to work correctly (gmap-dropdown based on css transitions)
- include html code to your gmap directive (into logicify-gmap-draw directive):
gmap-dropdown-template="dropDownContent">
if you wouldn't define line-types-control-position attribute, then directive will append this dropdown to current element, so you will be able to draw it out of map
- write some javascript in your controller:
scopelineTypesControlPosition = googlemapsControlPositionTOP_CENTER; //let's say that it will be at top-center//scope.dropDownTemplate = 'dropdown.html';//you can define your own dropdown template(bootstrap for example)//scope.dropDownContent = <div>Dropdown here</div>//define dropdown template as string//if you wouldn't define any template, then 'gmap-extended-draw' directive will use internal directive 'gmap-dropdown',//so you don't need define your own dropdownscopedraw = //draw options here, see example above};scopecssOpts = width: '90%' height: '90%' 'min-width': '400px' 'min-height': '200px'; scopegmOpts = zoom: 16 center: -1 1;scope { scopegmap = map;};
Note that if you would use your own dropdown, then please keep following rules:
- "onSelectPolyLineType(item)" use this callback name in your html to select;
- Use "current" object name to access selected item;
- "polyLineTypes" use this object name to display poly line types in the dropdown; Example for custom bootstrap dropdown:
Extending line types
You can extend list of supported line types (dotted, dashed, arrow-dotted etc.)
As can you see there are few attributes added. override-line-types="overrideLineTypes" and on-after-drawing-overlay="onAfterDraw" Controller code below:
scopelineTypesControlPosition = googlemapsControlPositionTOP_CENTER;scope { var icon = path: 'M0 0, L5 10, 10 -5' //svg path definition strokeWeight: 2 scale: 04 ; var layer = name: 'Nice line' icons: icon: icon offset: '100%' repeat: '20px' parentOptions: strokeOpacity: 0 ; lineTypesArray; return lineTypesArray;//return array back to directive (Required!!!!!)};//will not fires if marker or circle was addedscope { this; //this - overlay //lineType - is an item from array of line types //this.border - is poly line around rectangle or polygon, because only those figures can't be styled with strokeStyle (strokeOpacity of overlay is 0) //border can be null if overlay is polyLine!!!! Otherwise it will be google MVC object always ifthisborder!=null //do something here };
What is "icons" in lineTypesArray exactly? For example:
var dottedIcon = path: 'M 0,-1 0,1' //it's svg path definition, please see w3 spec //options below strokeOpacity: 1 strokeWeight: 4 scale: 02var arrow = //arrow definition here}var lineType = name: 'My name is dotted line' //name displayed in dropdown list icons: dottedIcon arrow //set dotted line here //those options will be applied to overlay parentOptions: strokeOpacity: 0 //should be a 0, because you are drawing dotted line, we don't need any border
You should check w3 org spec at first
Please see on-after-drawing-overlay callback.
This callback fires when custom lines applied to overlay (rectangle, polyline, polygon only). Border of shapes can't be styled as dotted or dashed for example, so we decided remove border of the shape overlay, and draw polyline instead. So each "overlay" object passed via this callback should have "overlay.border" property (it's poly line).
Editable mode.
You need to put all overlays to some collection. And then by clicking on some control, just set "editable" to true for all overlays.
scopedrawOptions = events: drawing: { var self = this; overlaysCollection; } ;scope { overlaysCollection;};scope{ scopeisEditModeEnabled = !scopeisEditModeEnabled; overlaysCollection;};
If you want remove overlay you can do the next:
$scopedraw = events: drawing: { //add listener googlemapsevent; } ;
Please don't forget cleanup after you self. Remove all listeners from google instance if it's not needed any more.
Color picker
You can pick a color for lines and shapes. You need to do the next:
Or
scopecolorPickerControlPosition = googlemapsControlPositionTOP_CENTER;//scope.colorPickerTemplate = 'colorPicker.html';//you can define your own color picker template(bootstrap for example)//scope.colorPickerContent = <div>Color picker here</div>//define colorPicker template as string//if you wouldn't define any template, then 'gmap-color-picker' directive will use internal html,//so you don't need define your own color picker
you can override destinations (border, fill). Attribute "override-destinations" contains overrideCallback. This callback calls when directive initialize.
scope{ destinations; return destinations; //required!!! You need return array back to directive.}
For custom color pickers you need to keep next rules:
- if there's opacity input, then "onSelectOpacity" callback should be called in your html
- ng-model is "destinations[destination].opacity.value" or "destinations[destination].color.value"
- Destination name could be "Border" or "Fill" (or you can override it, but only two cases), for example in your html: "destinations[destination].name" Example html:
- Button "toggle destination" should call "toggleDestination" callback (you can use any DOM event for this, "ng-click" for example)
- For color picker you should use "onSelectColor" callback in your html
- Note that all examples are with ng-model. So all callbacks ("onSelectOpacity" or "onSelectColor") calls to update drawing manager only, because it's in a parent directive
Example for color picker and change destination button:
Internal color picker - it's html5 input (type="color"). Please see browser capability
Auto complete address search support
Note that "places" library is required.
Your html
Controller code:
scopeautoCompleteControlPosition = googlemapsControlPositionTOP_CENTER;scopeplaceHolder = 'Enter location';scopeenableDefaultMarker = false;scope {//if no infowindow then create if !scopeplacesInfoWindow scopeplacesInfoWindow = templateUrl: 'place.html'; //if no marker then create if !scopeplaceMarker scopeplaceMarker = id: 'places_marker' map: map ; var position = null; if !placegeometry //if no geometry then check for latitude and longitude in address box var searchFor = inputValue; if typeof searchFor === 'string' && searchForlength > 0 var splitLatLon = searchFor latitude = splitLatLon0 * 1 longitude = splitLatLon1 * 1; if splitLatLonlength == 2 && angular && angular && ! && ! position = lat: latitude lng: longitude; map; //if no position then do nothing if !position scopeplaceMarker; scopeplacesInfoWindow; return; else //if there's geometry position = placegeometrylocation; scopeplaceMarker; scopeplaceMarker; scopeplacesInfoWindow;};
Reverse auto complete search
For example we handle 'dragend' event of marker.
scope { return searchResults0formatted_address;};scopeplaceMarker;googlemapsevent;
Take a look please on "onReverseAddressComplete" callback. This callback fires each time if there are some results while searching by position (only for reverse). You just need define attribute in the directive element (on-reverse-address-complete="callback") and you can modify address string that will be displayed in the input.
Marker cluster
We don't provide any support for markers and clustering. If you want to use markers, please use standard google maps api (such as google.maps.Marker). See examples on top of this page (for markers creation).
Why we don't support this feature?
Because markers collection can be huge. And watching this collection by angular "$watch" can cause performance issues. So all markers staff rely on user.
Example
How to use clustering: At first you need include script "markerclusterer.js" (direct raw link)
Here is git reference
You can use bower install to get this library:
bower install js-marker-cluster
And then you can find this script in "src" folder!
here is examples page