【问题标题】:how to covert coordinates of a selected location to long lat in php?如何在php中将选定位置的坐标转换为long lat?
【发布时间】:2016-06-16 15:30:32
【问题描述】:

下面是我所选位置的坐标

(31.41804293752012,73.0742385238409),(31.41414820024420457,73.0790450423959)

我想将这些坐标转换为经度和纬度..请帮助我

我已将每个 x 坐标存储到单独的数组中 n y 坐标存储到单独的数组中 下面是我的代码,它将灌输一些感觉,我到底在做什么...... 我只需要知道如何将这些坐标转换为纬度和经度,然后将那些经纬度转换为街道地址.....

   function check($point,$x_cordinates,$y_cordinates,$lat,$lon)
{
  $i=$j=$k=0;
   for ($i = 0, $j = $point-1; $i < $point; $j = $i++)
   {
                                                                                       if (($y_cordinates[$i] >  $lat_y != ($y_cordinates[$j] > $lat_y)) &&          ($lon_x < ($x_cordinates[$j] - $x_cordinates[$i]) * ($lat_y - $y_cordinates[$i])  /  ($y_cordinates[$j] - $y_cordinates[$i]) + $x_cordinates[$i]))
                {
             $k = !$k;
                }
   }
   return $k;
}
      $office_id=24;
       $sql="select office_loc from office where office_id='$office_id'LIMIT 1";
       $que=mysql_query($sql);
        if($que)
   {
           $row=mysql_fetch_array($que);
           $loc=$row['office_loc'];
           $loc_wdot_rbrac=explode('(',$loc);
           $loc_wdot_lbrac=str_replace(')', '', $loc_wdot_rbrac);
            $x_cordinates=array();
           $y_cordinates=array();
           foreach ($loc_wdot_lbrac as  $value) 
      {
          # code...
      if (empty($value)) 
       {
           # code...
       contine;
       }
       $loc_wdot_comma=explode(',',$value);
       $x_cordinates[]=$loc_wdot_comma[0];
       $y_cordinates[]=$loc_wdot_comma[1];

       }
        echo '---- Result x ---- <br /><pre>';
       print_r($x_cordinates);
       echo '</pre>';

      echo '---- Result y ---- <br /><pre>';
      print_r($y_cordinates);
      echo '</pre>';
      $lat=31.5546;
      $lon=74.3572;
      $point=count($x_cordinates);
      if(check($point,$x_cordinates,$y_cordinates,$lat,$lon))
         {
            echo"inside";
         }
           else
         {
            echo"out";
         }


          }
            else
         {
                 echo mysql_error(db);
          }

这是输出.....

                ---- Result x ---- 
                   Array
             (
               [0] => 
               [1] => 31.41804293752012
               [2] => 31.414820024420457
               [3] => 31.419031763657962
               [4] => 31.422877099540145
              )
               ---- Result y ---- 
               Array
               (
               [0] => 
               [1] =>  73.0742385238409
               [2] =>  73.07904504239559
               [3] =>  73.08410905301571
               [4] =>  73.0788304656744
               )
                      out

【问题讨论】:

  • 预期输出是什么?
  • @Andreas 我已经在代码下方的问题中发布了输出...问题是我想将这些坐标转换为 lon lat 或者如果可行则直接转换为地址
  • 你可以尝试使用 Google APi 来做同样的事情
  • 在下面更新了我的答案
  • @Andreas 谢谢兄弟

标签: php


【解决方案1】:

不确定这是否可行,因为您将获得一个额外的完整匹配数组。

preg_match_all("/\((\d+\.\d+),\s(\d+\.\d+)\)/", $input, $output);

https://3v4l.org/MjUMA

编辑:要在编辑中获得所需的 x 和 y 数组,您可以这样做:https://3v4l.org/Llvpg

$input = "(31.56215484936523, 74.35487531125546),(31.556669638119804, 74.35487531125546),(31.558351803858574, 74.3627717345953),(31.562886186493845, 74.36109803617)";

preg_match_all("/\((\d+\.\d+),\s(\d+\.\d+)\)/", $input, $output);

$x = $output[1];
$y = $output[2];

Var_dump($x);
var_dump($y);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多