Java 如何显示距离用户最近的标记';什么地方?
我有一个方法“onPostExecute()”,它显示从JSON解析的标记,并且运行良好。现在我想显示离我当前位置最近的标记。我该怎么办 下面是代码Java 如何显示距离用户最近的标记';什么地方?,java,android,Java,Android,我有一个方法“onPostExecute()”,它显示从JSON解析的标记,并且运行良好。现在我想显示离我当前位置最近的标记。我该怎么办 下面是代码 public void onPostExecute(String json) { try { // De-serialize the JSON string into an array of branch objects JSONArray jsonArray = new JSONA
public void onPostExecute(String json) {
try {
// De-serialize the JSON string into an array of branch objects
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
LatLng latLng = new LatLng(jsonObj.getDouble("latitude"),
jsonObj.getDouble("longitude"));
// Create a marker for each branch in the JSON data.
map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
.title(jsonObj.getString("name"))
// .snippet(Integer.toString(jsonObj.getInt("snippet")))
.snippet(jsonObj.getString("snippet"))
.position(latLng));
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Error processing JSON", e);
}
}
public void onPostExecute(字符串json){
试一试{
//将JSON字符串反序列化为分支对象数组
JSONArray JSONArray=新JSONArray(json);
for(int i=0;i
您应该阅读本指南。您需要跟踪用户位置,当您获得它时,您可以将您的JSON
标记与它进行比较。试着这样做:
public void onPostExecute(String json) {
Location closestMark;
try {
// De-serialize the JSON string into an array of branch objects
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
LatLng latLng = new LatLng(jsonObj.getDouble("latitude"),
jsonObj.getDouble("longitude"));
Location currentMark = new Location("");
currentMark.setLatitude(latLng.latitude);
currentMark.setLongitude(latLng.longitude);
if (closestMark == null || (userLocation.distanceTo(currentMark)
< userLocation.distanceTo(closestMark))
{
closestMark = currentMark;
}
// Create a marker for each branch in the JSON data.
map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
.title(jsonObj.getString("name"))
// .snippet(Integer.toString(jsonObj.getInt("snippet")))
.snippet(jsonObj.getString("snippet"))
.position(latLng));
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Error processing JSON", e);
}
}
public void onPostExecute(字符串json){
地点封闭标志;
试一试{
//将JSON字符串反序列化为分支对象数组
JSONArray JSONArray=新JSONArray(json);
for(int i=0;i
希望这有帮助。以下是我解决问题的方法。它可能也会帮助其他人
public void onPostExecute(String json) {
try {
// De-serialize the JSON string into an array of branch objects
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
LatLng latLng = new LatLng(jsonObj.getDouble("latitude"), jsonObj.getDouble("longitude"));
float[] distance = new float[1];
LatLng currentLatLng = mCurrLocationMarker.getPosition();
//move CameraPosition on user Location
if (i == 0) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(currentLatLng).zoom(13).build();
map.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
// Create a marker for each branch in the JSON data.
Marker currentMarker = map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
.title(jsonObj.getString("name"))
.snippet(jsonObj.getString("snippet"))
.position(latLng)
.visible(false)
);
Location.distanceBetween(currentLatLng.latitude, currentLatLng.longitude, latLng.latitude, latLng.longitude, distance);
if (i == 0) {
minDist = distance[0];
} else if (minDist > distance[0]) {
minDist = distance[0];
mClosestMarker = currentMarker;
}
}
Toast.makeText(NearestBranch.this, "Nearest Branch Distance: "+ mClosestMarker.getTitle() + " " + minDist, Toast.LENGTH_LONG).show();
map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
.title(mClosestMarker.getTitle())
.snippet(mClosestMarker.getSnippet())
.position(mClosestMarker.getPosition())
);
}
catch (JSONException e) {
Log.e(LOG_TAG, "Error processing JSON", e);
}
}
public void onPostExecute(字符串json){
试一试{
//将JSON字符串反序列化为分支对象数组
JSONArray JSONArray=新JSONArray(json);
for(int i=0;i距离[0]){
Minist=距离[0];
mClosestMarker=currentMarker;
}
}
Toast.makeText(NearestBranch.this,“最近的分支距离:”+mClosestMarker.getTitle()+“+minDist,Toast.LENGTH_LONG.show();
addMarker(新的MarkerOptions()
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
.title(mClosestMarker.getTitle())
.snippet(mClosestMarker.getSnippet())
.position(mClosestMarker.getPosition())
);
}
捕获(JSONException e){
e(Log_标记,“错误处理JSON”,e);
}
}
浏览标记列表。为每个标记计算标记与用户之间的距离。然后确定最近的距离one@vincrichaud谢谢你的帮助。但是我该怎么做呢?而且。我花了5分钟的研究。这是你自己可以做的