gpt4 book ai didi

java - Android 将 Json 对象添加到 google 标记

转载 作者:行者123 更新时间:2023-12-02 06:12:50 26 4
gpt4 key购买 nike

我正在尝试将从 API 读取的数据添加为我的标记的纬度和经度。

我似乎无法弄清楚它应该如何工作,并且我想到的任何事情都会失败。

这是我的代码:包 com.fly.plane;

    import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.fly.plane.R;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;

import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;


import android.R.array;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MyMapActivity extends ListActivity {

private ProgressDialog pDialog;

// URL to get data JSON
private static String url = "http://edmundgentle.com/snippets/flights/api.php";

// JSON Node speeds
private static final String TAG_data = "data";
private static final String TAG_BEARING = "bearing";
private static final String TAG_speed = "speed";
private static final String TAG_ARR = "arr";
private static final String TAG_ARR_TIME = "time";
private static final String TAG_ARR_LAT = "lat";
private static final String TAG_ARR_LON = "lon";
private static final String TAG_DEP = "dep";
private static final String TAG_DEP_TIME = "time";
private static final String TAG_DEP_LAT = "lat";
private static final String TAG_DEP_LON = "lon";

// data JSONArray
JSONArray data = null;

// Hashmap for ListView (just for testing)
ArrayList<HashMap<String, String>> contactList;

// Hashmap for ListView
ArrayList<String> ct;

private GoogleMap mMap;
public double latt = 0;
public double lng = 0;
public ArrayList<Integer> dLat;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_map);


// create markers
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
List<Marker> markers = new ArrayList<Marker>();
for (int i = 0; i < 5 ; i++)
{
Marker marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(latt, lng))
.title("Hello world")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.planemarker)));
markers.add(marker);
latt = Double.parseDouble(ct.get(i));
lng = i;

}


contactList = new ArrayList<HashMap<String, String>>();

ct = new ArrayList<String>();

//ListView lv = getListView();

new Getdata().execute();


}

/**
* Async task class to get json by making HTTP call
* */
private class Getdata extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MyMapActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();

}

@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
HTTPHandler sh = new HTTPHandler();

// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, HTTPHandler.GET);

Log.d("Response: ", "> " + jsonStr);

if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);

// Getting JSON Array node
data = jsonObj.getJSONArray(TAG_data);

// looping through All data
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);

String bearing = c.getString(TAG_BEARING);
String speed = c.getString(TAG_speed);

// departure node is JSON Object
JSONObject dep = c.getJSONObject(TAG_DEP);
String dtime = dep.getString(TAG_DEP_TIME);
String dlat = dep.getString(TAG_DEP_LAT);
String dlon = dep.getString(TAG_DEP_LON);

//dLat.add( Integer.parseInt(dep.getString(TAG_DEP_LAT)));

// arrival node is JSON Object
JSONObject arr = c.getJSONObject(TAG_ARR);
String atime = arr.getString(TAG_ARR_TIME);
String alat = arr.getString(TAG_ARR_LAT);
String alon = arr.getString(TAG_ARR_LON);

ArrayList<String> test = new ArrayList<String>();

test.add(alat);

ct.addAll(test);

// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();

// adding each child node to HashMap key => value
contact.put(TAG_BEARING, bearing);
contact.put(TAG_speed, speed);
contact.put(TAG_DEP_TIME, dtime);
contact.put("atime", atime);


// adding contact to contact list
contactList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}

return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MyMapActivity.this, contactList,
R.layout.list_item, new String[] { TAG_speed,TAG_DEP_TIME, "atime"
}, new int[] { R.id.speed,
R.id.departure, R.id.arrival });

setListAdapter(adapter);
}

}

}

Json

{
"data": [
{
"dep": {
"time": "06:00:00",
"lat": 51.469603,
"lon": -0.453566
},
"arr": {
"time": "08:35:00",
"lat": 38.770043,
"lon": -9.128165
},
"speed": 605.78072182431,
"bearing": -150.48169437461
},
{
"dep": {
"time": "06:00:00",
"lat": 51.469603,
"lon": -0.453566
},
"arr": {
"time": "08:35:00",
"lat": 38.770043,
"lon": -9.128165
},
"speed": 605.78072182431,
"bearing": -150.48169437461
}

我能够读取所有数据,并将其显示在屏幕上(目前仅用于验证目的)我遇到的麻烦是让该数据成为每个标记的纬度和经度。

编辑:我能够阻止它引起错误,但它不会再打印标记。 (我的猜测是,当它尝试制作标记时,contactList.size 为 0,导致它永远不会运行 for 循环,如果我将其更改为 i<10,它会崩溃,我猜是因为当它尝试从 contactList 获取数据时, 它是空的)。我不知道在 contactList 填满后如何让它运行。我可能是错的,这可能是一个完全不同的问题。

        mMap = ((MapFragment)   getFragmentManager().findFragmentById(R.id.map)).getMap();
List<Marker> markers = new ArrayList<Marker>();
for (int i = 0; i < contactList.size() ; i++)
{
if (i == 30) i=0;

String lat = contactList.get(i).get(TAG_ARR_LAT);
String lng = contactList.get(i).get(TAG_ARR_LON);

// Now parse it in double

double latitude = Double.parseDouble(lat);
double longitude = Double.parseDouble(lng);

Marker marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
//.position(new LatLng(latitude, longitude))
.title("Hello world")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.planemarker)));
markers.add(marker);

}

在那里,我试图从我的列表中获取拉特线,将其转换为整数,但不想玩球。

对此的任何帮助将不胜感激。

干杯

最佳答案

您应该将其放入 HashMap 字符串列表中

 String alat = arr.getString(TAG_ARR_LAT);
String alon = arr.getString(TAG_ARR_LON);

contact.put(TAG_ARR_LAT, atime);
contact.put(TAG_ARR_LON, atime);

之后在检索纬度和经度时使用此...

   for (int i = 0; i < contactList.size ; i++)
{
String lat = contactList.get(i).get(TAG_ARR_LAT);
String lng = contactList.get(i).get(TAG_ARR_LON);

// Now parse it in double

double latitude = Double.parseDouble(lat);
double longitude = Double.parseDouble(lng);

Marker marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title("Hello world")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.planemarker)));
markers.add(marker);

}

关于java - Android 将 Json 对象添加到 google 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21739668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com