GoogleMapV3HelperTest.php 9.8 KB

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