在安卓上使用Algolia获取我的位置和获取的记录位置之间的距离



我正在使用Algolia的InstantSearch Android库。这是我的Query

searcher.setQuery(new Query().setAroundLatLng(new AbstractQuery.LatLng(lat, lng)).setAroundRadius(5000)).

如何显示提取的记录与当前位置之间的距离?

基于@Raphi的出色答案,以下是使用InstantSearch Android显示距离的方法:您只需编写一个自定义的HitView

  • 创建自定义匹配视图:这里是实施AlgoliaHitView的专用TextView
  • onUpdateView,按照拉菲的建议获得_rankingInfo.matchedGeoLocation.distance
  • 在视图中使用此值,例如setText(distance + " meters away.")

实际上,您可以通过将参数getRankingInfo=true添加到搜索查询来在响应中找到每次匹配的距离。

我不熟悉InstantSearch Android,但如果您可以访问原始响应,请查看记录中的_rankingInfo.matchedGeoLocation.distance

{
[...] // your record fields
"_rankingInfo": {
"nbTypos": 0,
"firstMatchedWord": 0,
"proximityDistance": 0,
"userScore": 11,
"geoDistance": 468,
"geoPrecision": 1,
"nbExactWords": 1,
"words": 1,
"filters": 0,
"matchedGeoLocation": {
"lat": 48.86,
"lng": 2.3443,
"distance": 468
}
}
}

见 https://www.algolia.com/doc/guides/searching/geo-search/?language=rails#identifying-the-matching-geo-search-with-rankinginfo

创建一个名为CalculateDistanceTime的新类并粘贴以下代码:

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import com.google.android.gms.maps.model.LatLng;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
class CalculateDistanceTime {
private taskCompleteListener mTaskListener;
private Context mContext;

CalculateDistanceTime(Context context) {
mContext = context;
}
void setLoadListener(taskCompleteListener taskListener) {
mTaskListener = taskListener;
}

void getDirectionsUrl(LatLng origin, LatLng dest) {
// Origin of route
String str_origin = "origin=" + origin.latitude + "," + origin.longitude;
// Destination of route
String str_dest = "destination=" + dest.latitude + "," + dest.longitude;

// Sensor enabled
String sensor = "sensor=false";
// Building the parameters to the web service
String parameters = str_origin + "&" + str_dest + "&" + sensor;
// Output format
String output = "json";
// Building the url to the web service
String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;

DownloadTask downloadTask = new DownloadTask();
// Start downloading json data from Google Directions API
downloadTask.execute(url);
}
private String downloadUrl(String strUrl) throws IOException {
String data = "";
HttpURLConnection urlConnection;
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
try (InputStream iStream = urlConnection.getInputStream()) {
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
data = sb.toString();
br.close();
} catch (Exception e) {
Log.d("Excp. while downloading", e.toString());
} finally {
urlConnection.disconnect();
}
return data;
}

interface taskCompleteListener {
void taskCompleted(String[] time_distance);
}
private class DownloadTask extends AsyncTask<String, Void, String> {
// Downloading data in non-ui thread
@Override
protected String doInBackground(String... url) {
// For storing data from web service
String data = "";
try {
// Fetching the data from web service
data = downloadUrl(url[0]);
} catch (Exception e) {
Log.d("Background Task", e.toString());
}
return data;
}
// Executes in UI thread, after the execution of
// doInBackground()
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
ParserTask parserTask = new ParserTask();
// Invokes the thread for parsing the JSON data
parserTask.execute(result);
}
}
private class ParserTask extends AsyncTask<String, Integer, List<HashMap<String, String>>> {
// Parsing the data in non-ui thread
@Override
protected List<HashMap<String, String>> doInBackground(String... jsonData) {
JSONObject jObject;
List<HashMap<String, String>> routes = null;
try {
jObject = new JSONObject(jsonData[0]);
DistanceTimeParser parser = new DistanceTimeParser();
// Starts parsing data
routes = parser.parse(jObject);
} catch (Exception e) {
e.printStackTrace();
}
return routes;
}
// Executes in UI thread, after the parsing process
@Override
protected void onPostExecute(List<HashMap<String, String>> result) {
String duration_distance = "";

if (result.size() < 1) {
Log.e("Error : ", "No Points found");
return;
}

String[] date_dist = new String[2];
// Traversing through all the routes
for (int i = 0; i < result.size(); i++) {
// Fetching i-th route
HashMap<String, String> tmpData = result.get(i);
Set<String> key = tmpData.keySet();
Iterator it = key.iterator();
while (it.hasNext()) {
String hmKey = (String) it.next();
duration_distance = tmpData.get(hmKey);
System.out.println("Key: " + hmKey + " & Data: " + duration_distance);
it.remove(); // avoids a ConcurrentModificationException
}
date_dist[i] = duration_distance;
}
mTaskListener.taskCompleted(date_dist);
}
}
}

然后在要显示距离和时间的任何地方使用以下代码。 创建全局变量,CalculateDistanceTime distance_task;然后,使用此代码

distance_task.getDirectionsUrl(latLng1, latLng2);
distance_task.setLoadListener(new CalculateDistanceTime.taskCompleteListener() {
@Override
public void taskCompleted(String[] time_distance) {
v1.setVisibility(View.VISIBLE);
text1.setText(time_distance[0]); //Distance
text2.setText(time_distance[1]); //Time
}
});

请参阅,此处的文本 1 和文本 2 是分别用于显示距离和时间的两个文本视图。

另外,最重要的是在这里看到类调用,distance_task.getDirectionsUrl(latLng1, latLng2);,这里latLng1是源,latLng2是目的地,你应该提供这两个latLngs才能工作。

最新更新