Chinaunix首页 | 论坛 | 博客
  • 博客访问: 85129
  • 博文数量: 11
  • 博客积分: 187
  • 博客等级: 入伍新兵
  • 技术积分: 136
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-29 10:43
文章分类

全部博文(11)

文章存档

2016年(4)

2014年(1)

2012年(6)

我的朋友

分类: Python/Ruby

2012-02-29 11:08:02

php根据google地图的经纬度坐标获取此点周围的12张图片

附件下载地址为:

运行文件get_picture.php代码如下:
  1. <?php


  2. require( './latitudeLongitudeClass.php' );

  3. $latitude = $_GET['latitude'] ? $_GET['latitude'] : 0;
  4. $longitude = $_GET['longitude'] ? $_GET['longitude'] : 0;
  5. $zoom = $_GET['zoom'] ? $_GET['zoom'] : 0;

  6. $LatitudeLongitudeClass = new LatitudeLongitudeClass( $latitude, $longitude, $zoom );

  7. $centrepoint_info = $LatitudeLongitudeClass->getCentrepointInfo();
  8. $time = time();
  9. ?>

  10. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
  11. <html xmlns="">
  12. <head>
  13. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  14. <title>路况地图显示</title>
  15. </head>
  16. <body>

  17. <table width="1024" border="0" cellspacing="0" cellpadding="0" style="position: absolute; top: 20px; left: 20px; z-index: 1;">
  18. <tr>
  19. <td colspan="4" height="50" align="center">左上角坐标(<?php echo $centrepoint_info['upper_left']['left_up']['latitude']; ?>,<?php echo $centrepoint_info['upper_left']['left_up']['longitude']; ?>)
  20. 原始点坐标(<?php echo $centrepoint_info['centrepoint']['original']['latitude']; ?>,<?php echo $centrepoint_info['centrepoint']['original']['longitude']; ?>)</td>
  21. </tr>
  22. <tr>
  23. <td width="256"><img src=" echo $centrepoint_info['00']['x']; ?>&y=&z=&t=" /></td>
  24. <td width="256"><img src=" echo $centrepoint_info['01']['x']; ?>&y=&z=&t=" /></td>
  25. <td width="256"><img src=" echo $centrepoint_info['02']['x']; ?>&y=&z=&t=" /></td>
  26. <td width="256"><img src=" echo $centrepoint_info['03']['x']; ?>&y=&z=&t=" /></td>
  27. </tr>
  28. <tr>
  29. <td width="256"><img src=" echo $centrepoint_info['10']['x']; ?>&y=&z=&t=" /></td>
  30. <td width="256"><img src=" echo $centrepoint_info['11']['x']; ?>&y=&z=&t=" /></td>
  31. <td width="256"><img src=" echo $centrepoint_info['12']['x']; ?>&y=&z=&t=" /></td>
  32. <td width="256"><img src=" echo $centrepoint_info['13']['x']; ?>&y=&z=&t=" /></td>
  33. </tr>
  34. <tr>
  35. <td width="256"><img src=" echo $centrepoint_info['20']['x']; ?>&y=&z=&t=" /></td>
  36. <td width="256"><img src=" echo $centrepoint_info['21']['x']; ?>&y=&z=&t=" /></td>
  37. <td width="256"><img src=" echo $centrepoint_info['22']['x']; ?>&y=&z=&t=" /></td>
  38. <td width="256"><img src=" echo $centrepoint_info['23']['x']; ?>&y=&z=&t=" /></td>
  39. </tr>
  40. <tr>
  41. <td width="256"><img src=" echo $centrepoint_info['30']['x']; ?>&y=&z=&t=" /></td>
  42. <td width="256"><img src=" echo $centrepoint_info['31']['x']; ?>&y=&z=&t=" /></td>
  43. <td width="256"><img src=" echo $centrepoint_info['32']['x']; ?>&y=&z=&t=" /></td>
  44. <td width="256"><img src=" echo $centrepoint_info['33']['x']; ?>&y=&z=&t=" /></td>
  45. </tr>
  46. </table>


  47. <table width="1024" border="0" cellspacing="0" cellpadding="0" style="position: absolute; top: 20px; left: 20px; z-index: 2;">
  48. <tr>
  49. <td colspan="4" height="50" align="center">左上角坐标(<?php echo $centrepoint_info['upper_left']['left_up']['latitude']; ?>,<?php echo $centrepoint_info['upper_left']['left_up']['longitude']; ?>)
  50. 原始点坐标(<?php echo $centrepoint_info['centrepoint']['original']['latitude']; ?>,<?php echo $centrepoint_info['centrepoint']['original']['longitude']; ?>)</td>
  51. </tr>
  52. <tr>
  53. <td width="256"><img src=" echo $centrepoint_info['00']['x']; ?>&y=&zoom=&t=" /></td>
  54. <td width="256"><img src=" echo $centrepoint_info['01']['x']; ?>&y=&zoom=&t=" /></td>
  55. <td width="256"><img src=" echo $centrepoint_info['02']['x']; ?>&y=&zoom=&t=" /></td>
  56. <td width="256"><img src=" echo $centrepoint_info['03']['x']; ?>&y=&zoom=&t=" /></td>
  57. </tr>
  58. <tr>
  59. <td width="256"><img src=" echo $centrepoint_info['10']['x']; ?>&y=&zoom=&t=" /></td>
  60. <td width="256"><img src=" echo $centrepoint_info['11']['x']; ?>&y=&zoom=&t=" /></td>
  61. <td width="256"><img src=" echo $centrepoint_info['12']['x']; ?>&y=&zoom=&t=" /></td>
  62. <td width="256"><img src=" echo $centrepoint_info['13']['x']; ?>&y=&zoom=&t=" /></td>
  63. </tr>
  64. <tr>
  65. <td width="256"><img src=" echo $centrepoint_info['20']['x']; ?>&y=&zoom=&t=" /></td>
  66. <td width="256"><img src=" echo $centrepoint_info['21']['x']; ?>&y=&zoom=&t=" /></td>
  67. <td width="256"><img src=" echo $centrepoint_info['22']['x']; ?>&y=&zoom=&t=" /></td>
  68. <td width="256"><img src=" echo $centrepoint_info['23']['x']; ?>&y=&zoom=&t=" /></td>
  69. </tr>
  70. <tr>
  71. <td width="256"><img src=" echo $centrepoint_info['30']['x']; ?>&y=&zoom=&t=" /></td>
  72. <td width="256"><img src=" echo $centrepoint_info['31']['x']; ?>&y=&zoom=&t=" /></td>
  73. <td width="256"><img src=" echo $centrepoint_info['32']['x']; ?>&y=&zoom=&t=" /></td>
  74. <td width="256"><img src=" echo $centrepoint_info['33']['x']; ?>&y=&zoom=&t=" /></td>
  75. </tr>
  76. </table>
  77. </body>
  78. </html>

  79. 处理类文件latitudeLongitudeClass.php代码如下:
  80. <?php


  81. class LatitudeLongitudeClass
  82. {
  83.    
  84.     var $longitude_input;
  85.    
  86.    
  87.     var $latitude_input;
  88.    
  89.    
  90.     var $zoom;
  91.    
  92.    
  93.     var $pi = M_PI;
  94.    
  95.    
  96.     var $centrepoint_around_detail;
  97.    
  98.    
  99.     function __construct( $lati, $longi, $zoom )
  100.     {
  101.         $this->latitude_input = $lati;
  102.         $this->longitude_input = $longi;
  103.         $this->zoom = $zoom;
  104.     }
  105.    
  106.    
  107.     function getMercatorLongitude()
  108.     {
  109.         $longitude = 180 + $this->longitude_input;
  110.        
  111.         $long_tile_size = 360 / ( pow( 2, ( 17 - $this->zoom ) ) );
  112.    
  113.         $tile_x=(int)( $longitude / $long_tile_size );
  114.        
  115.         return $tile_x;
  116.     }
  117.    
  118.    
  119.     function getMercatorLatitude()
  120.     {
  121.         $latitude = $this->latitude_input;
  122.       
  123.         $latitude = ( $latitude > 90 ) ? $latitude - 180 : $latitude;
  124.        
  125.         $latitude = ( $latitude < -90 ) ? $latitude + 180 : $latitude;
  126.    
  127.         $phi = $this->pi * $latitude / 180;
  128.    
  129.         $res = 0.5 * log( ( 1 + sin( $phi ) ) / ( 1 - sin( $phi ) ) );
  130.        
  131.         $tile_y = (int)( ( ( 1 - $res / $this->pi ) / 2 ) * pow( 2, ( 17 - $this->zoom ) ) );
  132.    
  133.         return $tile_y;
  134.     }
  135.    
  136.    
  137.     function aGetMercatorLongitude( $tile_x )
  138.     {
  139.         $min_longitude = round( 360 * $tile_x / pow( 2, ( 17 - $this->zoom ) ) - 180, 6 );
  140.        
  141.         $max_longitude = round( 360 * ( $tile_x + 1) / pow( 2, ( 17 - $this->zoom ) ) - 180, 6 );
  142.        
  143.         $average_longitude = ( $max_longitude + $min_longitude ) / 2;
  144.        
  145.         $longitude_min_max = array(
  146.                 'min' => $min_longitude,
  147.                 'max' => $max_longitude,
  148.                 'average' => $average_longitude
  149.         );
  150.        
  151.         return $longitude_min_max;
  152.     }
  153.    
  154.    
  155.     function aGetMercatorLatitude( $title_y )
  156.     {
  157.         $r_min = -( $title_y + 1 - pow( 2, ( 17 - $this->zoom - 1 ) ) ) * $this->pi / pow( 2, ( 17 - $this->zoom - 1 ) );
  158.         $r_max = -( $title_y - pow( 2, ( 17 - $this->zoom - 1 ) ) ) * $this->pi / pow( 2, ( 17 - $this->zoom - 1 ) );
  159.        
  160.         $log_min = 2 * $r_min;
  161.         $log_max = 2 * $r_max;
  162.        
  163.         $log_N_min = exp( $log_min );
  164.         $log_N_max = exp( $log_max );
  165.        
  166.         $sin_min = ( $log_N_min - 1 ) / ( $log_N_min + 1 );
  167.         $sin_max = ( $log_N_max - 1 ) / ( $log_N_max + 1 );
  168.        
  169.         $min_latitude = round( asin( $sin_min ) * 180 / $this->pi, 6 );
  170.         $max_latitude = round( asin( $sin_max ) * 180 / $this->pi, 6 );
  171.        
  172.         $average_latitude = ( $max_latitude + $min_latitude ) / 2;
  173.        
  174.         $latitude_min_max = array(
  175.                 'min' => $min_latitude,
  176.                 'max' => $max_latitude,
  177.                 'average' => $average_latitude
  178.         );
  179.        
  180.         return $latitude_min_max;
  181.     }
  182.    
  183.    
  184.     function getCentrepointInfo()
  185.     {
  186.         $title_x = $this->getMercatorLongitude();
  187.         $title_y = $this->getMercatorLatitude();
  188.        
  189.         $longitude_interval = $this->aGetMercatorLongitude( $title_x );
  190.         $latitude_interval = $this->aGetMercatorLatitude( $title_y );
  191.        
  192.         // 中心点信息
  193.         $this->centrepoint_around_detail['centrepoint'] = array(
  194.                     'x' => $title_x,
  195.                     'y' => $title_y,
  196.                     'left_up' => array( 'latitude' => $latitude_interval['max'], 'longitude' => $longitude_interval['min'] ),
  197.                     'left_down' => array( 'latitude' => $latitude_interval['min'], 'longitude' => $longitude_interval['min'] ),
  198.                     'right_up' => array( 'latitude' => $latitude_interval['max'], 'longitude' => $longitude_interval['max'] ),
  199.                     'right_down' => array( 'latitude' => $latitude_interval['min'], 'longitude' => $longitude_interval['max'] ),
  200.                     'midpoint' => array( 'latitude' => $latitude_interval['average'], 'longitude' => $longitude_interval['average'] ),
  201.                     'original' => array( 'latitude' => $this->latitude_input, 'longitude' => $this->longitude_input )
  202.         );
  203.        
  204.         $this->setCentrepointAroundBlock();
  205.        
  206.         return $this->centrepoint_around_detail;
  207.     }
  208.    
  209.    
  210.     function setCentrepointAroundBlock()
  211.     {
  212.         $title_x = $this->centrepoint_around_detail['centrepoint']['x'];
  213.         $title_y = $this->centrepoint_around_detail['centrepoint']['y'];
  214.        
  215.         $mid_latitude = $this->centrepoint_around_detail['centrepoint']['midpoint']['latitude'];
  216.         $mid_longitude = $this->centrepoint_around_detail['centrepoint']['midpoint']['longitude'];
  217.        
  218.         $cur_latitude = $this->latitude_input;
  219.         $cur_longitude = $this->longitude_input;
  220.        
  221.         //由中心点在图片位置确定左上角图片的偏移量
  222.         if( $mid_longitude - $cur_longitude > 0 ) {
  223.            
  224.             //瓦片左边
  225.             if( $mid_latitude - $cur_latitude > 0 ) {
  226.                
  227.                 // 瓦片左下
  228.                 $x_start = -2;
  229.                 $y_start = -1;
  230.             } else {
  231.                 $x_start = -2;
  232.                 $y_start = -2;
  233.             }
  234.         } else {
  235.            
  236.             //瓦片右边
  237.             if( $mid_latitude - $cur_latitude > 0 ) {
  238.                
  239.                 //瓦片右下
  240.                 $x_start = -1;
  241.                 $y_start = -1;
  242.             } else {
  243.                 $x_start = -1;
  244.                 $y_start = -2;
  245.             }
  246.         }
  247.        
  248.         // 12张图中图片左上角点信息
  249.         $lu_title_x = $title_x + $x_start;
  250.         $lu_title_y = $title_y + $y_start;
  251.         $lu_longitude_interval = $this->aGetMercatorLongitude( $lu_title_x );
  252.         $lu_latitude_interval = $this->aGetMercatorLatitude( $lu_title_y );
  253.        
  254.         $this->centrepoint_around_detail['upper_left'] = array(
  255.                     'x' => $lu_title_x,
  256.                     'y' => $lu_title_y,
  257.                     'left_up' => array( 'latitude' => $lu_latitude_interval['max'], 'longitude' => $lu_longitude_interval['min'] ),
  258.                     'left_down' => array( 'latitude' => $lu_latitude_interval['min'], 'longitude' => $lu_longitude_interval['min'] ),
  259.                     'right_up' => array( 'latitude' => $lu_latitude_interval['max'], 'longitude' => $lu_longitude_interval['max'] ),
  260.                     'right_down' => array( 'latitude' => $lu_latitude_interval['min'], 'longitude' => $lu_longitude_interval['max'] )
  261.         );
  262.        
  263.         for( $i = 0; $i < 4; $i++ )
  264.         {
  265.             $y_extent = $y_start + $i;
  266.             for( $j = 0; $j < 4; $j++ )
  267.             {
  268.                 $x_extent = $x_start + $j;
  269.                 $key = $i . $j;
  270.                 $this->centrepoint_around_detail[$key] = array( 'x' => $title_x + $x_extent, 'y' => $title_y + $y_extent );
  271.             }
  272.         }
  273.     }
  274.    
  275. }
  276. ?>
阅读(3180) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~