GoogleMapV3HelperTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. namespace Tools\TestCase\View\Helper;
  3. use Tools\View\Helper\GoogleMapV3Helper;
  4. use Tools\TestSuite\TestCase;
  5. use Cake\View\View;
  6. use Cake\Core\Configure;
  7. class GoogleMapV3HelperTest extends TestCase {
  8. public function setUp() {
  9. parent::setUp();
  10. Configure::delete('Google');
  11. $this->GoogleMapV3 = new GoogleMapV3Helper(new View(null));
  12. }
  13. /**
  14. * GoogleMapV3HelperTest::testConfigMerge()
  15. *
  16. * @return void
  17. */
  18. public function testConfigMerge() {
  19. $config = array(
  20. 'map' => array(
  21. 'type' => 'foo',
  22. )
  23. );
  24. Configure::write('Google.zoom', 8);
  25. $this->GoogleMapV3 = new GoogleMapV3Helper(new View(null), $config);
  26. $result = $this->GoogleMapV3->config();
  27. $this->assertEquals('foo', $result['map']['type']);
  28. $this->assertEquals(8, $result['map']['zoom']);
  29. }
  30. public function testMapUrl() {
  31. $url = $this->GoogleMapV3->mapUrl(array('to' => 'Munich, Germany'));
  32. $this->assertEquals('http://maps.google.com/maps?daddr=Munich%2C+Germany', $url);
  33. $url = $this->GoogleMapV3->mapUrl(array('to' => '<München>, Germany'));
  34. $this->assertEquals('http://maps.google.com/maps?daddr=%3CM%C3%BCnchen%3E%2C+Germany', $url);
  35. }
  36. public function testMapLink() {
  37. $result = $this->GoogleMapV3->mapLink('<To Munich>!', array('to' => '<Munich>, Germany'));
  38. $expected = '<a href="http://maps.google.com/maps?daddr=%3CMunich%3E%2C+Germany">&lt;To Munich&gt;!</a>';
  39. //echo $result;
  40. $this->assertEquals($expected, $result);
  41. }
  42. public function testLinkWithMapUrl() {
  43. $url = $this->GoogleMapV3->mapUrl(array('to' => '<München>, Germany'));
  44. $result = $this->GoogleMapV3->Html->link('Some title', $url);
  45. $expected = '<a href="http://maps.google.com/maps?daddr=%3CM%C3%BCnchen%3E%2C+Germany">Some title</a>';
  46. //echo $result;
  47. $this->assertEquals($expected, $result);
  48. }
  49. public function testStaticPaths() {
  50. $m = $this->pathElements = array(
  51. array(
  52. 'path' => array('Berlin', 'Stuttgart'),
  53. 'color' => 'green',
  54. ),
  55. array(
  56. 'path' => array('44.2,11.1', '43.1,12.2', '44.3,11.3', '43.3,12.3'),
  57. ),
  58. array(
  59. 'path' => array(array('lat' => '48.1', 'lng' => '11.1'), array('lat' => '48.4', 'lng' => '11.2')), //'Frankfurt'
  60. 'color' => 'red',
  61. 'weight' => 10
  62. )
  63. );
  64. $is = $this->GoogleMapV3->staticPaths($m);
  65. //echo pr(h($is));
  66. $options = array(
  67. 'paths' => $is
  68. );
  69. $is = $this->GoogleMapV3->staticMapLink('My Title', $options);
  70. //echo h($is).BR.BR;
  71. $is = $this->GoogleMapV3->staticMap($options);
  72. //echo $is;
  73. }
  74. public function testStaticMarkers() {
  75. $m = $this->markerElements = array(
  76. array(
  77. 'address' => '44.3,11.2',
  78. ),
  79. array(
  80. 'address' => '44.2,11.1',
  81. )
  82. );
  83. $is = $this->GoogleMapV3->staticMarkers($m, array('color' => 'red', 'char' => 'C', 'shadow' => 'false'));
  84. //debug($is);
  85. $options = array(
  86. 'markers' => $is
  87. );
  88. $is = $this->GoogleMapV3->staticMap($options);
  89. //debug($is);
  90. //echo $is;
  91. }
  92. // http://maps.google.com/staticmap?size=500x500&maptype=hybrid&markers=color:red|label:S|48.3,11.2&sensor=false
  93. // http://maps.google.com/maps/api/staticmap?size=512x512&maptype=roadmap&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318&markers=color:red|color:red|label:C|40.718217,-73.998284&sensor=false
  94. public function testStatic() {
  95. //echo '<h2>StaticMap</h2>';
  96. $m = array(
  97. array(
  98. 'address' => 'Berlin',
  99. 'color' => 'yellow',
  100. 'char' => 'Z',
  101. 'shadow' => 'true'
  102. ),
  103. array(
  104. 'lat' => '44.2',
  105. 'lng' => '11.1',
  106. 'color' => '#0000FF',
  107. 'char' => '1',
  108. 'shadow' => 'false'
  109. )
  110. );
  111. $options = array(
  112. 'markers' => $this->GoogleMapV3->staticMarkers($m)
  113. );
  114. //debug($options['markers']).BR;
  115. $is = $this->GoogleMapV3->staticMapUrl($options);
  116. //echo h($is);
  117. //echo BR.BR;
  118. $is = $this->GoogleMapV3->staticMapLink('MyLink', $options);
  119. //echo h($is);
  120. //echo BR.BR;
  121. $is = $this->GoogleMapV3->staticMap($options);
  122. //echo h($is).BR;
  123. //echo $is;
  124. //echo BR.BR;
  125. $options = array(
  126. 'size' => '200x100',
  127. 'center' => true
  128. );
  129. $is = $this->GoogleMapV3->staticMapLink('MyTitle', $options);
  130. //echo h($is);
  131. //echo BR.BR;
  132. $attr = array(
  133. 'title' => '<b>Yeah!</b>'
  134. );
  135. $is = $this->GoogleMapV3->staticMap($options, $attr);
  136. //echo h($is).BR;
  137. //echo $is;
  138. //echo BR.BR;
  139. $pos = array(
  140. array('lat' => 48.1, 'lng' => '11.1'),
  141. array('lat' => 48.2, 'lng' => '11.2'),
  142. );
  143. $options = array(
  144. 'markers' => $this->GoogleMapV3->staticMarkers($pos)
  145. );
  146. $attr = array('url' => $this->GoogleMapV3->mapUrl(array('to' => 'Munich, Germany')));
  147. $is = $this->GoogleMapV3->staticMap($options, $attr);
  148. //echo h($is).BR;
  149. //echo $is;
  150. //echo BR.BR.BR;
  151. $url = $this->GoogleMapV3->mapUrl(array('to' => 'Munich, Germany'));
  152. $attr = array(
  153. 'title' => 'Yeah'
  154. );
  155. $image = $this->GoogleMapV3->staticMap($options, $attr);
  156. $link = $this->GoogleMapV3->Html->link($image, $url, array('escape' => false, 'target' => '_blank'));
  157. //echo h($link).BR;
  158. //echo $link;
  159. }
  160. public function testStaticMapWithStaticMapLink() {
  161. //echo '<h2>testStaticMapWithStaticMapLink</h2>';
  162. $markers = array();
  163. $markers[] = array('lat' => 48.2, 'lng' => 11.1, 'color' => 'red');
  164. $mapMarkers = $this->GoogleMapV3->staticMarkers($markers);
  165. $staticMapUrl = $this->GoogleMapV3->staticMapUrl(array('center' => 48 . ',' . 11, 'markers' => $mapMarkers, 'size' => '640x510', 'zoom' => 6));
  166. //echo $this->GoogleMapV3->Html->link('Open Static Map', $staticMapUrl, array('class'=>'staticMap', 'title'=>__d('tools', 'click for full map'))); //, 'escape'=>false
  167. }
  168. public function testMarkerIcons() {
  169. $tests = array(
  170. array('green', null),
  171. array('black', null),
  172. array('purple', 'E'),
  173. array('', 'Z'),
  174. );
  175. foreach ($tests as $test) {
  176. $is = $this->GoogleMapV3->iconSet($test[0], $test[1]);
  177. //echo $this->GoogleMapV3->Html->image($is['url']).BR;
  178. }
  179. }
  180. /**
  181. * Test some basic map options
  182. */
  183. public function testMap() {
  184. $options = array(
  185. 'autoScript' => true,
  186. 'inline' => true,
  187. );
  188. $result = $this->GoogleMapV3->map($options);
  189. $result .= $this->GoogleMapV3->script();
  190. $expected = '<div id="map_canvas" class="map"';
  191. $this->assertTextContains($expected, $result);
  192. $expected = '<script src="http://maps.google.com/maps/api/js?sensor=false';
  193. $this->assertTextContains($expected, $result);
  194. $expected = 'var map0 = new google.maps.Map(document.getElementById("map_canvas"), myOptions);';
  195. $this->assertTextContains($expected, $result);
  196. }
  197. /**
  198. * With default options
  199. */
  200. public function testDynamic() {
  201. //echo '<h2>Map 1</h2>';
  202. //echo '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>';
  203. //echo $this->GoogleMapV3->map($defaul, array('style'=>'width:100%; height: 800px'));
  204. //echo '<script src="'.$this->GoogleMapV3->apiUrl().'"></script>';
  205. //echo '<script src="'.$this->GoogleMapV3->gearsUrl().'"></script>';
  206. $options = array(
  207. 'zoom' => 6,
  208. 'type' => 'R',
  209. 'geolocate' => true,
  210. 'div' => array('id' => 'someothers'),
  211. 'map' => array('navOptions' => array('style' => 'SMALL'), 'typeOptions' => array('style' => 'HORIZONTAL_BAR', 'pos' => 'RIGHT_CENTER'))
  212. );
  213. $result = $this->GoogleMapV3->map($options);
  214. $this->GoogleMapV3->addMarker(array('lat' => 48.69847, 'lng' => 10.9514, 'title' => 'Marker', 'content' => 'Some Html-<b>Content</b>', 'icon' => $this->GoogleMapV3->iconSet('green', 'E')));
  215. $this->GoogleMapV3->addMarker(array('lat' => 47.69847, 'lng' => 11.9514, 'title' => 'Marker2', 'content' => 'Some more Html-<b>Content</b>'));
  216. $this->GoogleMapV3->addMarker(array('lat' => 47.19847, 'lng' => 11.1514, 'title' => 'Marker3'));
  217. /*
  218. $options = array(
  219. 'lat'=>48.15144,
  220. 'lng'=>10.198,
  221. 'content'=>'Thanks for using this'
  222. );
  223. $this->GoogleMapV3->addInfoWindow($options);
  224. //$this->GoogleMapV3->addEvent();
  225. */
  226. $result .= $this->GoogleMapV3->script();
  227. //echo $result;
  228. }
  229. /**
  230. * More than 100 markers and it gets reaaally slow...
  231. */
  232. public function testDynamic2() {
  233. //echo '<h2>Map 2</h2>';
  234. $options = array(
  235. 'zoom' => 6, 'type' => 'H',
  236. 'autoCenter' => true,
  237. 'div' => array('id' => 'someother'), //'height'=>'111',
  238. 'map' => array('typeOptions' => array('style' => 'DROPDOWN_MENU'))
  239. );
  240. //echo $this->GoogleMapV3->map($options);
  241. $this->GoogleMapV3->addMarker(array('lat' => 47.69847, 'lng' => 11.9514, 'title' => 'MarkerMUC', 'content' => 'Some more Html-<b>Content</b>'));
  242. for ($i = 0; $i < 100; $i++) {
  243. $lat = mt_rand(46000, 54000) / 1000;
  244. $lng = mt_rand(2000, 20000) / 1000;
  245. $this->GoogleMapV3->addMarker(array('id' => 'm' . ($i + 1), 'lat' => $lat, 'lng' => $lng, 'title' => 'Marker' . ($i + 1), 'content' => 'Lat: <b>' . $lat . '</b><br>Lng: <b>' . $lng . '</b>', 'icon' => 'http://google-maps-icons.googlecode.com/files/home.png'));
  246. }
  247. $js = "$('.mapAnchor').live('click', function() {
  248. var id = $(this).attr('rel');
  249. var match = matching[id];
  250. /*
  251. map.panTo(mapPoints[match]);
  252. mapMarkers[match].openInfoWindowHtml(mapWindows[match]);
  253. */
  254. gInfoWindows1[0].setContent(gWindowContents1[match]);
  255. gInfoWindows1[0].open(map1, gMarkers1[match]);
  256. });";
  257. $this->GoogleMapV3->addCustom($js);
  258. //echo $this->GoogleMapV3->script();
  259. //echo '<a href="javascript:void(0)" class="mapAnchor" rel="m2">Marker2</a> ';
  260. //echo '<a href="javascript:void(0)" class="mapAnchor" rel="m3">Marker3</a>';
  261. }
  262. public function testDynamic3() {
  263. //echo '<h2>Map with Directions</h2>';
  264. $options = array(
  265. 'zoom' => 5,
  266. 'type' => 'H',
  267. 'map' => array()
  268. );
  269. //echo $this->GoogleMapV3->map($options);
  270. $this->GoogleMapV3->addMarker(array('lat' => 48.69847, 'lng' => 10.9514, 'content' => '<b>Bla</b>', 'title' => 'NoDirections'));
  271. $this->GoogleMapV3->addMarker(array('lat' => 47.69847, 'lng' => 11.9514, 'title' => 'AutoToDirections', 'content' => '<b>Bla</b>', 'directions' => true));
  272. $this->GoogleMapV3->addMarker(array('lat' => 46.69847, 'lng' => 11.9514, 'title' => 'ManuelToDirections', 'content' => '<b>Bla</b>', 'directions' => array('to' => 'Munich, Germany')));
  273. $this->GoogleMapV3->addMarker(array('lat' => 45.69847, 'lng' => 11.9514, 'title' => 'ManuelFromDirections', 'content' => '<b>Bla</b>', 'directions' => array('from' => 'Munich, Germany')));
  274. //echo $this->GoogleMapV3->script();
  275. }
  276. }