从laravel中的邮政编码搜索位置

时间:2019-05-29 05:49:56

标签: javascript php laravel-5 google-geocoder

        In User Login One User whos zipcode is 90001 and he wants to search Clinic zipcode 90003 around 5 km in area. after searching he gets 90001 90002 90003 location but in my case its not? why? 
        Check Following Code

在用户登录中,一位邮政编码为90001的用户想要在5公里范围内搜索诊所邮政编码90003。搜索后,他获得了90001 90002 90003的位置,但在我而言,不是吗?为什么?             检查以下代码

        //Search Controller
        public function filter(Request $request, Trial $trial)
            {
                //Fliter or Search The Gender,Age & zipcode/state/city
                $trial = Trial::query();
                if ($request->has('gender')) {
                    $trial->where('gender' ,$request->gender);
                };

                if ($request->has('age')) {
                    $trial->where('age', $request->age);
                }
                if ($request->has('zipcode')) {
                    $trial->where('zipcode', $request->zipcode)
                          ->orWhere('city', $request->city)
                          ->orWhere('state', $request->state);
                }
                $request = $trial->get();
                return view('search.searchhome',compact('trial'));
            }
        //Model For zipcode distance
 In User Login One User whos zipcode is 90001 and he wants to search Clinic zipcode 90003 around 5 km in area. after searching he gets 90001 90002 90003 location but in my case its not? why? 
    Check Following Code
        public function zipcode(){ //For The Distance Of Two zipcode
                $lat = 25.000;
                $lng = 25.000;

                $trial = Trial::selectRaw('*, ( 6367 * acos( cos( radians( ? ) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians( ? ) ) + sin( radians( ? ) ) * sin( radians( lat ) ) ) ) AS distance', [$lat, $lng, $lat])
                    ->having('distance', '<', 20)
                    ->orderBy('distance')
                    ->get();
            }

        //my Blade
        <form  method="post" action="{{route('searchhome')}}" class="form-inline"role="form">
                                    {{ csrf_field() }}

                                <div class="form-group">
                                    <label for="gender" class="sr-only">Gender</label>
                                        <select name="gender" class="form-control col-md-12" required="">
                                            <option value="">Gender</option>
                                            <option value="Male">Male</option>
                                            <option value="Female">Female</option>
                                        </select>
                                </div>
                                <div class="form-group">
                                    <label for="age" class="sr-only">Age</label>
                                        <select name="age" class="form-control" required="">
                                            <option value="">Age</option>

                                            @for ($age = 1; $age <= 100 ; $age++)                  
                                            <option value = "{{ $age }}">{{ $age }}</option>                  
                                            @endfor
                                        </select>
                                </div>

                                <div class="form-group">
                                    <label for="geo" class="sr-only">{{ Auth::user()->zipcode }}</label>
                                        <input type="text" class="form-control col-md-12" name="zipcode" placeholder="City/State or Zip" id = "search" value="{{ Auth::user()->zipcode }}" required="" >
                                </div>
                                <p class="clearfix"></p>
                                <div class="form-group">
                                    <label for="distance" class="sr-only">Distance</label>
                                        <select name="distance" class="form-control" required="">
                                            <option value="">Distance </option>
                                            @for ($dis = 1; $dis <= 20 ; $dis++)                  
                                            <option value = "Distance">{{ $dis."-"."km" }}</option>                  
                                            @endfor
                                        </select>

                                    </div>
                                    <p class="clearfix"></p>
                                    <div class="form-group">
                                        <div class="radio small pull-left">
                                            <label>
                                                <input type="radio" name="health" id="healthy" value="{{ Auth::user()->health }}" checked>
                                                I am generally healthy
                                            </label>
                                        </div>
                                        <div class="radio small pull-left">
                                            <label>
                                                <input type="radio" name="health" id="not-healthy" value="{{ Auth::user()->health }}">
                                                I have a diagnosed medical condition
                                            </label>
                                        </div>
                                    </div>
                                    <p style="margin-top: 1em; margin-bottom: 1em;">
                                        <button type="submit" class="btn btn-primary pull-right">Find Trials</button>
                                    </p>
                                    <p class="clearfix"></p>
                            </form>
In User Login One User whos zipcode is 90001 and he wants to search Clinic zipcode 90003 around 5 km in area. after searching he gets 90001 90002 90003 location but in my case its not? why? 
        Check Following Code
//has one relationship with User
public function trial(){
        return $this->hasOne('App\Trial','t_id', 'id');
    }

在用户登录中,一位邮政编码为90001的用户想要在5公里范围内搜索诊所邮政编码90003。搜索后,他获得了90001 90002 90003的位置,但在我而言,不是吗?为什么?             检查以下代码

 In User Login One User whos zipcode is 90001 and he wants to search Clinic zipcode 90003 around 5 km in area. after searching he gets 90001 90002 90003 location but in my case its not? why? 
    Check Following Code

1 个答案:

答案 0 :(得分:0)

要获取5公里左右的位置,您需要在试用表中保存纬度和经度列以及邮政编码。

从试用表中获取给定邮政编码的纬度和经度。

在您的控制器中。

    $data = Trial::where('zipcode',$request->zipcode)->select('latitude','longitude')->first(); 
    $lat = $data->latitude;
    $lng = $data->longitude;
    $distance = 5 // Diatanse in kms
    $query = Trial::getByDistance($lat, $lng, $distance); 

    $ids = []; 
    //Extract the id's
    if(!empty($query)) {
      foreach($query as $q)
      {
        array_push($ids, $q->id);
      }
   }

 // Now write final query 
   if(!empty($ids)){
    $results = DB::table('Trial')->whereIn( 'id', $ids)->where('your other conditions here '); 
   }

试用模式:

 public static function getByDistance($lat, $lng, $distance)
{
  $results = DB::select(DB::raw('SELECT id, ( 3959 * acos( cos( radians(' . $lat . ') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(' . $lng . ') ) + sin( radians(' . $lat .') ) * sin( radians(lat) ) ) ) AS distance FROM articles HAVING distance < ' . $distance . ' ORDER BY distance') );

  return $results;
}

注意:语法可能会更改,但登录可能会有用。