GoogleMapV3HelperTest.php 10 KB

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