BIOSTALL / CodeIgniter-Google-Maps-V3-API-Library

The library enables you to create a map and overlay multiple markers, polylines, polygons, rectangles, ground overlays and/or circles, all of which are fully customisable. The library also supports showing directions between two points, including the ability to show the textual directions alongside the map too, and marker clustering. The first stages of integration with the Google Places API are available for use too.
http://biostall.com/codeigniter-google-maps-v3-api-library
288 stars 199 forks source link

Codeigniter Google Maps API V3 Toggle Markers #20

Closed Matrix1990 closed 11 years ago

Matrix1990 commented 11 years ago

I am using codeigniter and this library file to display the google map settings:

https://github.com/BIOSTALL/CodeIgniter-Google-Maps-V3-API-Library/blob/master/application/libraries/Googlemaps.php

In my header the js is displayed as this: <?php echo $map['js']; ?>

and my data is stored in a database and pulled in using the contollers and models:

model:

function get_coordinates() { $return = array(); $query = $this->db->get('markers');

    if ($query->num_rows()>0) {
        foreach ($query->result() as $row) {
        array_push($return, $row);
        }
    }
return $return;
} 

Controller:

public function index() {

    $this->load->library('googlemaps');
    $this->load->model('home_model', '', TRUE);

    $this->googlemaps->initialize();

    $coords = $this->home_model->get_coordinates();

    foreach ($coords as $coordinate) {
        if($coordinate->type == "restaurant"){
            $image = 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=A|FF0000|000000';
        }

        if($coordinate->type == "bar"){
            $image = 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=B|FFFFFF|000000';
        }

        $marker = array('id'=> $coordinate->type, 'position' => $coordinate->lat.','.$coordinate->lng, 'icon' => $image, 'infowindow_content' =>$coordinate->address);

        $this->googlemaps->add_marker($marker);

    }

    $data = array();

    $data['map'] = $this->googlemaps->create_map();

    $data['page_title'] = "Home";
    $data['content'] = "home";  
    $this->load->view('template', $data);

} I am trying to add a toggle feature in the view so if you click the bar button, the bar markers show and so on. I am having difficulty grabbing the marker id and types to even start this due to the js being stored in a variable and when i relocate the js to a file it keeps refreshing so it removes my code.

Does anyone have any idea with this library to add a toggle feature in as i cant even do a click event and alert anything out!

I am attempting something similar to this without the sidebar: http://www.geocodezip.com/v3_MW_example_categories.html

Just unsure of where and how to edit and keep what i write as the js variable keeps changing it!

Thanks!

BIOSTALL commented 11 years ago

Hi Matrix,

My only thoughts on this are that you give each 'type' of marker (ie. bars and restaurants) different and unique IDs. In the library, when adding a marker, you can specify it's ID like so:

$marker = array();
$marker['position'] = 'lat, lng';
$marker['id'] = 'MY_ID';
$this->googlemaps->add_marker($marker);

The above would produce a marker with ID 'marker_MY_ID'.

It looks like you already kind of attempted to do this based on the code you provided, however in your code each marker would have the same ID.

The way I have been able to achieve this was as follows:

Step 1 In your view initialise some JavaScript variables, one for each type:

var marker_bar = new Array;
var marker_restaurant = new Array;

Step 2 Set the ID of the markers to use these arrays:

$marker = array();
$marker['id'] = "bar[0]";
$marker['position'] = 'lat, lng';     
$this->googlemaps->add_marker($marker);

$marker = array();
$marker['id'] = "bar[1]";
$marker['position'] = 'lat, lng';
$this->googlemaps->add_marker($marker);

$marker = array();
$marker['id'] = "restaurant[0]";
$marker['position'] = 'lat, lng';
$this->googlemaps->add_marker($marker);

$marker = array();
$marker['id'] = "restaurant[1]";
$marker['position'] = 'lat, lng';
$this->googlemaps->add_marker($marker);

Notice how the id is turning the markers into an array? Our marker IDs are now marker_bar[0], marker_bar[1], marker_restaurant[0] and marker_restaurant[1].

Step 3 We can now toggle these with JS. For example, to hide just the bars we can do this:

for (var i in marker_bar)
{
   marker_bar[i].setMap(null);
}

I hope that helps.

Saddamk commented 7 years ago

Sir, I am trying to get a marker by id to put a single marker on the map but I am unable to do the task, will you please help me get this out ? I can show you the code snippet if you want to see it ?