Android 不将json数据获取到mapview中,仅获取空白映射
可能重复:Android 不将json数据获取到mapview中,仅获取空白映射,android,android-mapview,Android,Android Mapview,可能重复: 我正在将JSON数据提取到MapView中以显示位置,但每当我运行程序时,没有将数据提取到地图中,只是获取没有JSON数据的空白地图,我将JSON数据和活动代码放在这里,请告诉我哪里出错了 { “地图”:[ { 活动代码:- public class Main extends MapActivity { public GeoPoint point; TapControlledMapView mapView; // use the custom TapControlledMapVie
我正在将JSON数据提取到MapView中以显示位置,但每当我运行程序时,没有将数据提取到地图中,只是获取没有JSON数据的空白地图,我将JSON数据和活动代码放在这里,请告诉我哪里出错了 { “地图”:[ { 活动代码:-
public class Main extends MapActivity {
public GeoPoint point;
TapControlledMapView mapView; // use the custom TapControlledMapView
List<Overlay> mapOverlays;
Drawable drawable;
SimpleItemizedOverlay itemizedOverlay;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (TapControlledMapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
// dismiss balloon upon single tap of MapView (iOS behavior)
mapView.setOnSingleTapListener(new OnSingleTapListener() {
public boolean onSingleTap(MotionEvent e) {
itemizedOverlay.hideAllBalloons();
return true;
}
});
mapOverlays = mapView.getOverlays();
drawable = getResources().getDrawable(R.drawable.ic_launcher);
itemizedOverlay = new SimpleItemizedOverlay
(drawable, mapView);
itemizedOverlay.setShowClose(false);
itemizedOverlay.setShowDisclosure(true);
itemizedOverlay.setSnapToCenter(false);
class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
HttpClient client = new DefaultHttpClient();
// Perform a GET request for a JSON list
HttpUriRequest request = new HttpGet
("https://dl.!!!!.com/maps.json");
// Get the response that sends back
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Convert this response into a readable string
String jsonString = null;
try {
jsonString = StreamUtils.convertToString
(response.getEntity().getContent());
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Create a JSON object that we can use from the String
JSONObject json = null;
try {
json = new JSONObject(jsonString);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
JSONArray jsonArray = json.getJSONArray("maps");
Log.e("log_tag", "Opening JSON Array ");
for(int i=0;i < jsonArray.length();i++)
{
JSONObject jsonObject = jsonArray.getJSONObject(i);
String latitude = jsonObject.getString("latitude");
String longitude = jsonObject.getString("longitude");
String title = jsonObject.getString("title");
String country = jsonObject.getString("country");
double lat = Double.parseDouble(latitude);
double lng = Double.parseDouble(longitude);
Log.e("log_tag", "ADDING GEOPOINT"+title);
point = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
OverlayItem overlayItem = new OverlayItem(point, title,
country);
itemizedOverlay.addOverlay(overlayItem);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
itemizedOverlay.populateNow();
mapOverlays.add(itemizedOverlay);
if (savedInstanceState == null) {
MapController controller = mapView.getController();
controller.setCenter(point);
controller.setZoom(7);
} else {
// example restoring focused state of overlays
int focused;
focused = savedInstanceState.getInt("focused_1", -1);
if (focused >= 0) {
itemizedOverlay.setFocus
(itemizedOverlay.getItem(focused));
}
}
return jsonString; }
}
}
public类主活动{
公共地质点;
TapControlledMapView地图视图;//使用自定义TapControlledMapView
列出地图覆盖图;
可拉伸;
简单的项目化覆盖;
@凌驾
创建时的公共void(最终捆绑包savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView=(点击ControlledMapView)findViewById(R.id.mapView);
mapView.SetBuilTinZoomControl(真);
mapView.setSatellite(假);
//单击MapView时关闭引出序号(iOS行为)
setOnSingleTapListener(新的OnSingleTapListener(){
公共布尔onSingleTap(运动事件e){
itemizedOverlay.hidealballons();
返回true;
}
});
mapOverlays=mapView.getOverlays();
drawable=getResources().getDrawable(R.drawable.ic_启动器);
itemizedOverlay=新的SimpleItemizedOverlay
(可绘制,地图视图);
itemizedOverlay.setShowClose(假);
逐项显示。设置显示披露(真实);
itemizedOverlay.setSnapToCenter(false);
类下载WebPagetTask扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…URL){
HttpClient=new DefaultHttpClient();
//对JSON列表执行GET请求
HttpUriRequest请求=新建HttpGet
("https://dl.!!.com/maps.json);
//获取发送回的响应
HttpResponse响应=null;
试一试{
响应=客户端。执行(请求);
}捕获(客户端协议例外e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
//将此响应转换为可读字符串
字符串jsonString=null;
试一试{
jsonString=StreamUtils.convertToString
(response.getEntity().getContent());
}捕获(非法状态例外e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
//创建一个可以从字符串中使用的JSON对象
JSONObject json=null;
试一试{
json=新的JSONObject(jsonString);
}捕获(JSONException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
试一试{
JSONArray JSONArray=json.getJSONArray(“映射”);
e(“Log_标记”,“打开JSON数组”);
for(int i=0;i=0){
itemizedOverlay.setFocus
(itemizedOverlay.getItem(聚焦));
}
}
返回jsonString;}
}
}
例外说明一切:您试图通过在UI线程中加载映射来执行网络操作。这是一个坏主意,因为网络访问速度慢,会在UI运行时阻塞它
您应该将映射加载移动到不同的线程中,例如使用AsyncTask
。
基本上,您可以定义一个扩展AsyncTask
的类,然后在doInBackground()
中运行下载,并将结果填充到onPostExecute()中的视图中。
有关更多信息,请参阅
在您的情况下,需要将此代码块放入doInBackground()
方法:
HttpClient client = new DefaultHttpClient();
// Perform a GET request for a JSON list
HttpUriRequest request = new HttpGet("https://dl.###.com/maps.json");
// Get the response that sends back
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Create a JSON object that we can use from the String
JSONObject json = null;
try {
json = new JSONObject(jsonString);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
并确保它返回json
对象,以便在onPostExecute()
请查看文档中的示例。您还可以查看更多示例。您能告诉我我在哪里吗missing@Stanley不要在主UI线程中编写与网络相关的任务,使用单独的线程来完成与网络相关的任务。嗨,迪帕克,谢谢你的回复,但我不知道我需要在哪里以及如何编写代码,你能帮我写吗我,请用上面的代码,
HttpClient client = new DefaultHttpClient();
// Perform a GET request for a JSON list
HttpUriRequest request = new HttpGet("https://dl.###.com/maps.json");
// Get the response that sends back
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Create a JSON object that we can use from the String
JSONObject json = null;
try {
json = new JSONObject(jsonString);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}