Skip to content

snoblucha/Kohana-Google-Maps-Module

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kohana Google Maps Module V2.0

With this module you can easily add a Google Map to your Kohana installation!

The Google Map module has NO dependency to other modules! Just jQuery. For multiple maps compatibility.

Features

  • Display a Google-Map by just echo'ing an instance of the "Gmap" class
  • Setting several map-types (road, satellite, hybrid and terrain)
  • Setting sensor-parameter (for mobile devices)
  • Adding markers to the map (including custom icon and Google Maps popups)
  • Adding polylines to the map
  • Adding polygons to the map
  • Setting the positions and types for the map-controls
  • Auto zooming on added objects
  • Auto centering the map

Usage

The usage of this module is as easy as it could be! Simply activate the module in your bootstrap.php

/**
 * Enable modules. Modules are referenced by a relative or absolute path.
 */
Kohana::modules(array(
	// ...
	'gmap'       => MODPATH.'gmap',       // A simple google-maps module
));

Then you'll just echo an instance of the gmap class in your action... For example:

public function action_index()
{
	$this->template->map = Gmap::factory();
} // function

This is a more advanced example with usage of various options...

public function action_index()
{
        $gmap = Gmap::factory('map1');

        //set map type
        $gmap->setMaptype(Gmap_Maptype::HYBRID());

        $gmap->getControls()->getMaptype()->setType(Gmap_Controls_Maptype::TYPE_HORIZONTAL);

        //positions of controls - maptype and navigation
        $gmap->getControls()->getMaptype()->setPosition(Gmap_Controls_Maptype::POSITION_BOTTOM_LEFT);

        //hide the controls
        $gmap->getControls()->getNavigation()->setDisplay(false);

        $gmap->setZoom(11);
        $gmap->addMarker(Gmap_Marker::factory('test', 50.6, 14.0)->setContent('Test content'));
        $gmap->addMarker(Gmap_Marker::factory('test_no_content', 50.6, 14.5));
        $gmap->addMarker(Gmap_Marker::factory('test_no_content_icon', 50.2, 14.4)->setIcon('/img/heating-and-aircon.png'));

        //geocoding
        $find_address = Gmap_Geocode::geocode('adresa','Anezky Ceske 629/3, Usti nad Labem');

        //on geocode success will be called javascript function saveGps(request)
        $find_address->setOnSuccess('saveGps');
        $gmap->addGeocode($find_address);

        //polyline

        $polygon = new Gmap_Polygon('square');
        $polygon->addPoint(49, 14)->addPoint(48, 14)->addPoint(48, 13)->addPoint(49, 13);
        $polygon->setFillColor('#ff00ff')->setFillOpacity(0.25)->setStrokeColor("#00ff00")->setStrokeOpacity(0.75)->setStrokeWeight(2.5);
        $gmap->addPolygon($polygon);


        //second map
        $gmap2 = Gmap::factory('map2')->addMarker(Gmap_Marker::factory('Test on second map', 50.7, 14.0)->setContent('Marker on second map'));

        $this->template->content = $gmap->render() . $gmap2->render();
    } // function

Enabling map

Map is enabled on first map render, or if you want to have better control, where the initial script should be you can call Gmap::enable(); at particular location.

If you want the best control, you can call Gmap::setEnabled() which can set the flag to desired value. Or you can set disableAutoEnable in config.

But then you have to take care about javascript initialization. See view/gmap_enable.php. Generally it takes two steps. First is to include google map api.

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=<?php echo Gmap::getSensor() ? 'true' : 'false'; ?>"></script>

And then you must include also js/gmap.js javascript as well.

Javascript access

In javascript the gmaps variable is created. It is javascript object see views/gmap_enable.php. Each map instance is accessible in javascript by gmaps['map_id']. If map_id is not explicitly set in constructor or later in the code, autogenerated value is used.

###Javascript constructor

function Gmap(options){

    this.options = options;
    this.polyline_coords = {};
    this.polylines = {};
    this.polygons = {};
    this.polygon_coords = {};
    this.markers = {};
    this.info_windows = {};
    this.geocode_request = {};
    this.geocode_result = {};
};

##Variables in javascript

After the initialization of the map you may access all added objects for further processing. Usefull is access to geocode results in case you want to save geocoded values.

Gmap::factory('map_id')
        ->add_marker_address('my_marker','address, city')
        ->render();
  • In javascript you will then have variable gmaps['map_id'].
  • gmaps['map_id'].map is instance of google.maps.Map.
  • gmaps['map_id']['markers']['my_marker'] is instance of google.maps.Marker.
  • In gmaps['map_id'].geocode_result['my_marker'] is result from the call to geocoder.geocode

and so on.

Example

You have initialized instance of the map Gmap::factory('map'). And added some addresses to geocode. In app you have button <a href="#" id="save_gps">save result</a>. And javascript given below. Also you have outputted list of markers withe rel attribute marker_MARKERID on the map. Eg. $gmap->addMarker('marker1') <a href="#" rel="marker_marker1">Marker 1</a> and you then can control map movement and geocode saving from the links.

<script type="text/javascript">
    $(function(){
        $('#save_gps').click(function(){

            for(i in gmaps['map'].geocode_result){

                var loc = gmaps['map'].geocode_result[i][0].geometry.location;
                var lat = loc.lat();
                var lng = loc.lng();
                var gps = ((lat > 0 ) ? 'N' : 'S')+Math.abs(lat)+' '+((lng > 0 ) ? 'E' : 'W')+Math.abs(lng);
                $.post('updategps',{id:i,gps:gps});
            }
            return false;
        });

        $('.marker').click(function(){
            google.maps.event.trigger(gmaps['map'].map,'resize');
            var marker_name = $(this).attr('rel');
            var id = marker_name.split('_');
            var marker = gmaps['map'].markers[id[1]];
            if(marker) gmaps['map'].map.setCenter(marker.getPosition());
            return false;
        });
    });
</script>

About

A Kohana module for integrating google maps easily to your website.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 88.2%
  • JavaScript 11.8%