【问题标题】:Calculation of Distance between user location & Restaurant location via json (url)通过 json (url) 计算用户位置和餐厅位置之间的距离
【发布时间】:2016-08-17 08:16:05
【问题描述】:

我有来自 URL (JSON) 的 latitudelongitude。我必须计算从用户位置(需要首先确定)到特定latitudelongitude 基础(来自 URL)的距离,并显示它们之间的距离。

JSON 数据见附图:

获取 JSON 数据的 URL(餐厅的纬度和经度):https://comida-95724.herokuapp.com/api/v1/restaurants/get_featured_restaurants

ListActiivty.java

 public class ListViewActivity extends Activity {
    // Log tag
    private static final String TAG = ListViewActivity.class.getSimpleName();
    // change here url of server api
    private static final String url = "https://comida-95724.herokuapp.com/api/v1/restaurants/get_featured_restaurants";

    private ProgressDialog pDialog;
    private List<Movie> movieList = new ArrayList<Movie>();
    private ListView listView;

    private CustomListAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);
        listView = (ListView) findViewById(R.id.list);
        adapter = new CustomListAdapter(this, movieList);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Movie movie = movieList.get(position);
                Intent intent = new Intent(ListViewActivity.this, SecondActivity.class);
                intent.putExtra("name", movie.getName());
                intent.putExtra("average_ratings", movie.getAverage_ratings());
                intent.putExtra("full_address", movie.getAddress());
                intent.putExtra("image_url", movie.getThumbnailUrl());
                intent.putExtra("cuisine",movie.getCuisine());
                intent.putExtra("cost",movie.getCost());
                startActivity(intent);

            }
        });
        listView.setAdapter(adapter);
         pDialog = new ProgressDialog(this);
        // Showing progress dialog before making http request
        pDialog.setMessage("Please Keep patience.Its loading...");

        pDialog.show();

        JsonArrayRequest movieReq = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();
                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {
                                JSONObject obj = response.getJSONObject(i);
                                Movie movie = new Movie();
                                //movie.setTitle(obj.getString("title"));
                                movie.setName(obj.getString("name"));
                                //movie.setThumbnailUrl(obj.getString("image"));
                                movie.setThumbnailUrl(obj.getString("org_image_url"));
                                movie.setAverage_ratings(obj.getString("average_ratings"));
                                movie.setCuisine(obj.getString("cuisine"));
                                movie.setAddress(obj.getJSONObject("address").getString("area"));
                                movie.setlatitude(obj.getJSONObject("address").getString("latitude"));
                                movie.setlongitude(obj.getJSONObject("address").getString("longitude"));
                                movieList.add(movie);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                        adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePDialog();

            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(movieReq);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePDialog();
    }

    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }

}

list_row.xml

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/ly_root"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="12dp"
        android:background="#FEFEFE"
        app:cardCornerRadius="8dp">
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <com.android.volley.toolbox.NetworkImageView
                android:id="@+id/thumbnail"
                android:layout_width="120dp"
                android:layout_height="100dp"
                android:layout_marginRight="8dp"
                android:scaleType="centerCrop" />

            <!-- Restaurant name  -->
            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_toRightOf="@+id/thumbnail"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/area"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/name"
                android:layout_toRightOf="@+id/thumbnail"
                android:textColor="#D2691E"/>

            <!-- Rating -->
            <TextView
                android:id="@+id/average_ratings"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/area"
                android:layout_toRightOf="@+id/thumbnail"
                android:textColor="#D2691E" />

            <TextView
                android:id="@+id/cuisine"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/average_ratings"
                android:layout_toRightOf="@+id/thumbnail"
                android:textColor="#D2691E" />
            <TextView
                android:id="@+id/cost"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/cuisine"
                android:layout_toRightOf="@+id/thumbnail"
                android:textColor="#D2691E" />
    <TextView
            android:id="@+id/latitude"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/cost"
            android:layout_toRightOf="@+id/thumbnail"
            android:textColor="#D2691E" />
        <TextView
            android:id="@+id/longitude"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/latitude"
            android:layout_toRightOf="@+id/thumbnail"
            android:textColor="#D2691E" />
            <TextView
                android:id="@+id/Distance"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/cost"
                android:layout_toRightOf="@+id/thumbnail"
                android:textColor="#D2691E" />
        </RelativeLayout>

    </android.support.v7.widget.CardView>

我现在得到的结果如下图所示。在此我必须显示用户与特定餐厅之间的距离:

示例:用户与 Yuuka -The St. Regis Mumbai =1.9 公里的距离

我怎样才能显示这个?

谁能帮帮我?

【问题讨论】:

  • 已经回答了。通过@Chuck 检查answer
  • 我的问题是,如果纬度和经度是通过 json (URL) 来的,如何计算距离?
  • 你检查答案了吗?这是一个计算两个纬度和经度位置之间距离的函数。您必须使用设备找出用户的当前位置以及从服务器返回的点。然后用那个函数计算距离。
  • 是的,我已经检查过了,但是我必须在代码中的哪个位置进行编辑才能得到结果?你能告诉我得到想要的结果吗?
  • 首先,当用户打开应用或特定活动时 -> 找出他的位置。其次,当您收到服务器的包含位置的响应时,调用上述函数来计算用户与对象之间的距离。然后将该值呈现给用户。

标签: android json google-maps google-places-api


【解决方案1】:

如果你知道你当前的位置,你可以计算出Android中两个Location对象之间的距离如下:

Location myLocation = new Location("my loc");

myLocation.setLatitude(myLat);
myLocation.setLongitude(myLng);

Location locFromJson = new Location("loc from json");

locFromJson.setLatitude(latFromJson);
locFromJson.setLongitude(lngFromJson);

float distance = myLocation.distanceTo(locFromJson);

这里,distance 以米为单位。因此,distance/1000 将以公里为单位。

【讨论】:

  • 你能告诉我在哪里编辑这段代码吗?
猜你喜欢
  • 1970-01-01
  • 2011-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-28
  • 2017-04-20
  • 2015-10-26
相关资源
最近更新 更多