如何获得附近的用户和距离?

时间:2018-09-05 12:17:47

标签: laravel geolocation

首先,我想向您展示当前的数据库结构。有两个表:

用户

  • id
  • 名称
  • location_id

位置

  • id
  • 城市
  • lat
  • lng

现在,我想实现一个API,该API可以获取附近的用户及其距离。像附近的用户一样:

<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:layout_height="match_parent"
tools:openDrawer="start">


<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"

    app:headerLayout="@layout/nav_header_main">
    app:menu="@menu/activity_main_drawer">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:scrollbars="vertical"
        android:orientation="vertical">

        <ExpandableListView
            android:id="@+id/expandableListView"
            android:layout_width="match_parent"
            android:background="@drawable/side_nav_bar"
            android:layout_height="wrap_content"
            android:groupIndicator="@null"
            android:layout_marginTop="@dimen/nav_header_height"
            android:dividerHeight="0dp" />



    </LinearLayout>

</android.support.design.widget.NavigationView>

1 个答案:

答案 0 :(得分:0)

您可以这样尝试

用户模型

public function location(){
    return $this->belongsTo('App\Location');
}

在位置模型中,创建一个函数名称rsine,然后创建一个以scopeIsWithinMaxDistance命名的本地范围

位置模型

/**
 * Scope for adding distance filter
 */
public function scopeIsWithinMaxDistance($query, $rsine, $radius = 5)
{
    return $query->selectRaw("*, {$rsine} AS distance")
                 ->whereRaw("{$rsine} < ?", [$radius])
                 ->orderBy('distance');
}

/**
 * Sql command for finding distance between 2 coordinates
 */
public static function rsine($coordinates)
{
    return '(6371 * acos(cos(radians(' . $coordinates['latitude'] . ')) 
    * cos(radians(`lat`)) 
    * cos(radians(`lng`) 
    - radians(' . $coordinates['longitude'] . ')) 
    + sin(radians(' . $coordinates['latitude'] . ')) 
    * sin(radians(`lat`))))';
}

public static function convertKMToMiles($dist)
{
    return $dist * 1.609344;
}

现在在控制器中调用它

$coordinates = ['latitude' => '28.392200', 'longitude' => '77.320801'];

$rsine = Location::rsine($coordinates);

$radius = Location::convertKMToMiles(5); //convert KM to miles

$users = User::with(['location' => function ($query) use ($rsine) {
               $query->selectRaw("*, {$rsine} AS distance");
         }])->whereHas('location', function ($query) use ($radius, $rsine) {
               $query->isWithinMaxDistance($rsine, $radius);
         })->get();

现在您可以像这样打印它了

foreach($users as $user){
   $user->name;
   $user->location->city;
   $user->location->distance;
}