Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JGoogleEmbedMaps::addMarker with event(s) #6381

Closed
wants to merge 13 commits into from
Closed

Conversation

itbra
Copy link
Contributor

@itbra itbra commented Mar 10, 2015

I found that when adding a marker to the map it is not considered to bind events to it. Isn't it a tyical scenario to click on a marker to show a balloon with short info or a link to more details?

The Google Maps API presents as an example to create the marker and immediately create the desired event which expects to receive the marker created as reference to bind to. I extended those functions responsible for marker creation by adding them another argument for the events (find a PR for map events here). Also, the function creating the script was extended to evaluate the currently set markers not only for their options but also for events to bind.

I tested this functionality by taking a view template and paste the following:

// Target element
echo '<h1>JGoogleEmbedMaps Test</h1>';
echo '<div class="row">';
echo    '<div class="col col-xs-12">';
echo        '<div id="map" style="height:500px; background:gainsboro; border:gray">LOADING ...</div>';
echo    '</div>';
echo    '<div class="col col-xs-12">';
echo        '<h5>Monitor</h5>';
echo        '<p id="monitor" style="padding:1em 0.5em; border:1px solid gainsboro; border-radius:3px"></p>';
echo    '</div>';
echo '</div>';

// Config
$options = new JRegistry(array(
    'key'         => '',
    'mapid'       => 'map',
    'mapclass'    => 'mapclass'
));

// Get instance
$google = new JGoogle($options);
$map = $google->embed('Maps');

// Define load type
$map->setAutoload('jquery');

$map->setMapType('HYBRID');
$map->setZoom('3');

// Marker configuration
$markerOptions = array(
    'clickable' => true,
    'draggable' => false,
    'opacity'   => 0.75,
    'optimized' => true,
    'visible'   => true
);

$map->setCenter(array('51.165691','10.451526'), 'Germany', $markerOptions,
array(
    'click' => 'function(e) {
        console.log("event: ", e);
        map.setCenter(this.getPosition());
        document.getElementById("monitor").innerHTML = "Clicked:&nbsp;" + this.title;
    }'
));

// Add markers
$map->addMarker(array('46.227638','2.213749'),  'France');  
$map->addMarker(array('60.472023','8.468946'),  'Norway', $markerOptions);
$map->addMarker(array('51.919437','19.145136'), 'Poland', $markerOptions, array());
$map->addMarker(array('41.871941','12.567380'), 'Italy',  $markerOptions, array(
    'click' => 'function(e) {
        console.log("event: ", e);
        map.setCenter(this.getPosition());
        document.getElementById("monitor").innerHTML = "Clicked:&nbsp;" + this.title;
    }'
));

// Prepare callback
$callback =
<<<ENDSCRIPT
window.gm = map;   // Will execute after initialisation to add reference to the created map as a global.
(function($, gm, element) {
    $('#monitor').append('<em>Callback executed</em>')
})(jQuery.noConflict(), window.google || {}, document.getElementById({$options->get('mapid')}));
ENDSCRIPT;

// Register custom script(s)
$map->setAdditionalJavascript($callback);

$map->echoHeader();
$map->echoBody();

So far this works. I wonder why this hasn't been considered yet. What do you think about this?

I found that when adding a marker to the map it is not considered to bind events to it. Isn't it a tyical scenario to click on a marker to show a balloon with short info or a link to more details? 

The [Google Maps API](https://developers.google.com/maps/documentation/javascript/reference) presents as an example to create the marker and the create the desired event which expects to receive the marker as a parameter. I extended those functions responsible for marker creation by adding them another argument for the events. Also, the function creating the script was extended to evaluate the currently set markers not only for their options but also for events to bind.

I tested this functionality by taking a view template and writing down the following:
```
// Target element
echo '<h1>JGoogleEmbedMaps Test</h1>';
echo '<div class="row">';
echo 	'<div class="col col-xs-12">';
echo 		'<div id="map" style="height:500px; background:gainsboro; border:gray">LOADING ...</div>';
echo 	'</div>';
echo 	'<div class="col col-xs-12">';
echo 		'<h5>Monitor</h5>';
echo 		'<p id="monitor" style="padding:1em 0.5em; border:1px solid gainsboro; border-radius:3px"></p>';
echo 	'</div>';
echo '</div>';

// Config
$options = new JRegistry(array(
	'extrascript' => 'locationmap=map;',
	'key'         => '',
	'mapclass'    => 'mapclass',
	'mapid'       => 'map'
));

// Get instance
$google = new JGoogle($options);
$map = $google->embed('Maps');

// Define load type
$map->setAutoload('jquery');

$map->setMapType('HYBRID');
$map->setZoom('3');

$map->setCenter(array('49','10'), 'Center Marker', array(
	'clickable' => true
),
array(
	'click' => 'function(e) {
		map.setCenter(this.getPosition());
		document.getElementById("monitor").innerHTML = this.title;
	}'
));

// Add markers
$map->addMarker(array('52','10'), 'Test Marker', array(
	'clickable' => true
),
array(
	'click' => 'function(e) {
		map.setCenter(this.getPosition());
		document.getElementById("monitor").innerHTML = this.title;
	}'
));

// Prepare callback
$callback =
<<<ENDSCRIPT
(function($, gm, element) {
	$('#monitor').append('<em>Callback executed</em>')
})(jQuery.noConflict(), window.google || {}, document.getElementById({$options->get('mapid')}));
ENDSCRIPT;

// Register custom script(s)
$map->setAdditionalJavascript($callback);

$map->echoHeader();
$map->echoBody();
```

So far this works. I wonder why this hasn't been considered yet. Also, i am yet missing to pass events for the map itself.

What do you think about this?
@itbra
Copy link
Contributor Author

itbra commented Mar 11, 2015

The tests appear to fail although the code works properly. Can somebody please tell me what causes the test to fail? Is there probably a change required in the test class? Mabe current tests don't consider the new arguments and therefore cause the test to fail?
I have no idea, but this message from travis Failed asserting that two arrays are equal. makes me to assume that.

After adding additional function parameter to `setCenter()` and `addMarker()` the test have to be extended as well.
@Hackwar
Copy link
Member

Hackwar commented Mar 11, 2015

The tests fail because you have a PHP syntax error in your changed test file on line 360. You are doing a "somethin" => "value" outside of an array scope. Please fix that.

@itbra
Copy link
Contributor Author

itbra commented Mar 12, 2015

Thanks for pointing me to the issue. Finally its passing all tests. I'll add one more test case with a non-empty events-array.

@itbra
Copy link
Contributor Author

itbra commented Mar 12, 2015

There we go. Extra test for empty or non-empty events array added and passed.

@coolcat-creations
Copy link
Contributor

Sorry i couldn´t test successfully. By using your code above with the patch nothing happens by click on the markers.


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/6381.

@itbra
Copy link
Contributor Author

itbra commented Mar 14, 2015

Of course it works. I tested it right this second via copy+paste the above example 1:1. You have to apply the patch to JGoogleEmbedMaps first so that the events are registered on marker creation. Click the markers and watch the text changing in the monitor container or on the map - when you click one of the two having an event bound.
NOTE:
Only 2 out of the 5 markers receive an event in my example just to demonstrate the different ways of marker creation. One changes the map center and the other outputs Clicked: Test Marker 4 to the monitor container.

@coolcat-creations
Copy link
Contributor

Oh sorry, don´t know how to apply something to JGoogleEmbedMaps. I think i tested it wrong.


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/6381.

@itbra itbra changed the title Bind event(s) to map marker Bind event(s) to map and marker Mar 16, 2015
@itbra
Copy link
Contributor Author

itbra commented Mar 16, 2015

After integrating the event binding to markers and fixing the corresponding unit tests i have now created another PR to add event handlers for map events.

@itbra itbra changed the title Bind event(s) to map and marker JGoogleEmbedMaps::addMarker with event(s) Mar 17, 2015
$setup .= substr(json_encode($options), 1, -1);
$setup .= '});';

if (isset($marker['events']))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should test here, if it's an array:

if (isset($marker['events']) && is_array($marker['events']))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. I will update this line.

@Erftralle
Copy link
Contributor

What do you think about this?

I find it a usefull addition, thanks!

@test: Works fine for me.

@itbra
Copy link
Contributor Author

itbra commented Mar 19, 2015

@Erftralle thanks for testing and your feedback.
Are there maybe more testers willing to cpy and paste the above example and report back?

@Rivaner
Copy link

Rivaner commented Oct 24, 2015

I have tested this item ✅ successfully on 7bb7f93

It works.


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/6381.

@DavidTrenkle
Copy link

I have tested this item ✅ successfully on 7bb7f93

@test works with the example code


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/6381.

@KlausKrippeit
Copy link

I have tested this item ✅ successfully on 7bb7f93

@test successful


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/6381.

1 similar comment
@KlausKrippeit
Copy link

I have tested this item ✅ successfully on 7bb7f93

@test successful


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/6381.

@zero-24 zero-24 added this to the Joomla! 3.5.0 milestone Oct 24, 2015
@zero-24
Copy link
Contributor

zero-24 commented Oct 24, 2015

RTC. Thanks


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/6381.

@joomla-cms-bot joomla-cms-bot added RTC This Pull Request is Ready To Commit Unit/System Tests labels Oct 24, 2015
@roland-d
Copy link
Contributor

Thanks everybody. Merged into 3.5-dev with commit 269b781

@roland-d roland-d closed this Oct 25, 2015
@wilsonge wilsonge removed the RTC This Pull Request is Ready To Commit label Jan 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet