我想从json(在线)读取纬度和经度,json文件看起来像这样。纬度是名称,经度是国家。
[
{
"name": "13.0714562",
"country": "77.55946348",
"twitter": "Current Location"
},
{
"name": "13.0714562",
"country": "77.55946348",
"twitter": "Current Location"
},
{
"name": "13.0714562",
"country": "77.55946348",
"twitter": "Current Location"
},
...................
]
我想通过上面的json在android中的谷歌地图上绘制纬度和经度。
json的网址是: - http://hmkcode.appspot.com/jsonservlet
我正在尝试使用asynctask来下载json。下面是我的android代码。
public class Maps extends Activity {
private ProgressDialog pDialog;
private static String url = "http://hmkcode.appspot.com/jsonservlet";
private static final String latitude = "name";
private static final String longitude = "country";
private GoogleMap googleMap;
float lat, lon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maps);
new GetMap().execute();
}
private class GetMap extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Maps.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONArray jsonArr = new JSONArray(jsonStr);
for (int i = 0; i < jsonArr.length(); i++) {
JSONObject c = jsonArr.getJSONObject(i);
String name = c.getString(latitude);
String time = c.getString(longitude);
double LAT = Double.parseDouble(name);
double LON = Double.parseDouble(time);
final LatLng Marker = new LatLng(LAT, LON);
}
} 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();
try {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Marker, 15));
@SuppressWarnings("unused")
Marker TP = googleMap.addMarker(new MarkerOptions().position(Marker).title("Revamp 15,click on the arrow below for directions"));
}
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
@SuppressWarnings("unused")
Marker TP = googleMap.addMarker(new MarkerOptions().position(Marker).title("Revamp 15,click on the arrow below for directions"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
但是我在asnctask的 onPostExecute 部分出错了 Marker无法解析为变量。所以我尝试用null初始化marker。如果我这样做,json就不会加载到Marker变量中。
如何在标记变量中加载json?
答案 0 :(得分:1)
它说“标记无法解析为变量”。 基本上,在onPostExecute中,您发送的Marker不是变量。这是一堂课。
我不确定你是否可以编译这段代码?你得到编译错误或什么?
final LatLng Marker = new LatLng(LAT, LON);
而不是“Marker”尝试将其命名为“marker”。并将其作为全局变量。然后在后执行中再次使用它作为“标记”。
不要将类名用作变量名。来自naming convention:
局部变量,实例变量和类变量也写在lowerCamelCase
中
此外,您可以在异步任务中将其作为结果发送,而不是使用全局变量。像这样的东西
private class GetMap extends AsyncTask<Void, Void, LatLng>{
@Override
protected LatLng doInBackground(Void... voids) {
// code...
return new LatLng(LAT, LON);
}
@Override
protected void onPostExecute(LatLng markerLatLng) {
// code
Marker TP = googleMap.addMarker(new MarkerOptions().position(markerLatLng).title("Revamp 15,click on the arrow below for directions"));
// code
}
}
编辑:也许尝试这样的事情?只有在您正确设置谷歌地图,其他一切正常(您可以查看帖子上的评论)之后,这才有效。
public class GetMap extends AsyncTask<Void, Void, List<LatLng>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Maps.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected List<LatLng> doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
List<LatLng> latLngList = new ArrayList<>();
JSONArray jsonArr = new JSONArray(jsonStr);
for (int i = 0; i < jsonArr.length(); i++) {
JSONObject c = jsonArr.getJSONObject(i);
String name = c.getString(latitude);
String time = c.getString(longitude);
double LAT = Double.parseDouble(name);
double LON = Double.parseDouble(time);
latLngList.add(new LatLng(LAT, LON));
}
return latLngList;
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(List<LatLng> result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
if (result == null){
// Error occured, handle it as you wish (log, toast, dialog)
return;
}else if(result.size() == 0){
// There was no any result, handle this as you wish (log, toast, dialog)
return;
}
try {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
//there is no point of moving camera from one marker to another, so we will just move to first one
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(result.get(0), 15));
}
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
for(LatLng latLng : result)
googleMap.addMarker(new MarkerOptions().position(latLng).title("Revamp 15,click on the arrow below for directions"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 1 :(得分:-2)
为何不使用Google Maps API https://developers.google.com/maps/documentation/android/start
然后,您必须设置地图,从服务器调用服务以获取您的信息,然后设置标记。
欢呼声