GoogleMapV3HelperTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. * with default options
  170. * 2010-12-18 ms
  171. */
  172. public function testDynamic() {
  173. echo '<h2>Map 1</h2>';
  174. echo '<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>';
  175. //echo $this->GoogleMapV3->map($defaul, array('style'=>'width:100%; height: 800px'));
  176. echo '<script type="text/javascript" src="'.$this->GoogleMapV3->apiUrl().'"></script>';
  177. echo '<script type="text/javascript" src="'.$this->GoogleMapV3->gearsUrl().'"></script>';
  178. $options = array(
  179. 'zoom'=>6,
  180. 'type'=>'R',
  181. 'geolocate' => true,
  182. 'div' => array('id'=>'someothers'),
  183. 'map' => array('navOptions'=>array('style'=>'SMALL'), 'typeOptions' => array('style'=>'HORIZONTAL_BAR', 'pos'=>'RIGHT_CENTER'))
  184. );
  185. $result = $this->GoogleMapV3->map($options);
  186. $this->GoogleMapV3->addMarker(array('lat'=>48.69847,'lng'=>10.9514, 'title'=>'Marker', 'content'=>'Some Html-<b>Content</b>', 'icon'=>$this->GoogleMapV3->iconSet('green', 'E')));
  187. $this->GoogleMapV3->addMarker(array('lat'=>47.69847,'lng'=>11.9514, 'title'=>'Marker2', 'content'=>'Some more Html-<b>Content</b>'));
  188. $this->GoogleMapV3->addMarker(array('lat'=>47.19847,'lng'=>11.1514, 'title'=>'Marker3'));
  189. /*
  190. $options = array(
  191. 'lat'=>48.15144,
  192. 'lng'=>10.198,
  193. 'content'=>'Thanks for using this'
  194. );
  195. $this->GoogleMapV3->addInfoWindow($options);
  196. //$this->GoogleMapV3->addEvent();
  197. */
  198. $result .= $this->GoogleMapV3->script();
  199. echo $result;
  200. }
  201. /**
  202. * more than 100 markers and it gets reaaally slow...
  203. * 2010-12-18 ms
  204. */
  205. public function testDynamic2() {
  206. echo '<h2>Map 2</h2>';
  207. $options = array(
  208. 'zoom'=>6, 'type'=>'H',
  209. 'autoCenter' => true,
  210. 'div' => array('id'=>'someother'), //'height'=>'111',
  211. 'map' => array('typeOptions' => array('style'=>'DROPDOWN_MENU'))
  212. );
  213. echo $this->GoogleMapV3->map($options);
  214. $this->GoogleMapV3->addMarker(array('lat'=>47.69847,'lng'=>11.9514, 'title'=>'MarkerMUC', 'content'=>'Some more Html-<b>Content</b>'));
  215. for ($i = 0; $i < 100; $i++) {
  216. $lat = mt_rand(46000, 54000) / 1000;
  217. $lng = mt_rand(2000, 20000) / 1000;
  218. $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'));
  219. }
  220. $js = "$('.mapAnchor').live('click', function() {
  221. var id = $(this).attr('rel');
  222. var match = matching[id];
  223. /*
  224. map.panTo(mapPoints[match]);
  225. mapMarkers[match].openInfoWindowHtml(mapWindows[match]);
  226. */
  227. gInfoWindows1[0].setContent(gWindowContents1[match]);
  228. gInfoWindows1[0].open(map1, gMarkers1[match]);
  229. });";
  230. $this->GoogleMapV3->addCustom($js);
  231. echo $this->GoogleMapV3->script();
  232. echo '<a href="javascript:void(0)" class="mapAnchor" rel="m2">Marker2</a> ';
  233. echo '<a href="javascript:void(0)" class="mapAnchor" rel="m3">Marker3</a>';
  234. }
  235. public function testDynamic3() {
  236. echo '<h2>Map with Directions</h2>';
  237. $options = array(
  238. 'zoom'=>5,
  239. 'type'=>'H',
  240. 'map' => array()
  241. );
  242. echo $this->GoogleMapV3->map($options);
  243. $this->GoogleMapV3->addMarker(array('lat'=>48.69847,'lng'=>10.9514, 'content'=>'<b>Bla</b>', 'title'=>'NoDirections'));
  244. $this->GoogleMapV3->addMarker(array('lat'=>47.69847,'lng'=>11.9514, 'title'=>'AutoToDirections', 'content'=>'<b>Bla</b>', 'directions'=>true));
  245. $this->GoogleMapV3->addMarker(array('lat'=>46.69847,'lng'=>11.9514, 'title'=>'ManuelToDirections', 'content'=>'<b>Bla</b>', 'directions'=>array('to'=>'Munich, Germany')));
  246. $this->GoogleMapV3->addMarker(array('lat'=>45.69847,'lng'=>11.9514, 'title'=>'ManuelFromDirections', 'content'=>'<b>Bla</b>', 'directions'=>array('from'=>'Munich, Germany')));
  247. echo $this->GoogleMapV3->script();
  248. }
  249. }