我想通过webservice以及googlemap中的标记标记,以json格式从我的数据库中获取我的位置。我已经删除了这段代码。当我调试它,我可以看到解析的数据,但我添加标记错误。 在发布之前我已经看过所有类似的问题了。 这是我的代码,我想知道问题出在哪里。
public class MyLocation extends Fragment{
public MyLocation(){}private GoogleMap mGoogleMap;private String urlnearby="http://myserver/api/relations/relations_nearby/latitude/48.8633216/longitude/2.337843399999997";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.mylocation, container, false);
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity().getBaseContext());
if(status!= ConnectionResult.SUCCESS){ // Google Play Services are not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, getActivity(), requestCode);
dialog.show();
}
SupportMapFragment fm = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mGoogleMap = fm.getMap();
if (mGoogleMap != null) {
LocationManager service = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
boolean enabledGPS = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean enabledWiFi = service.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!enabledGPS) {
Toast.makeText(getActivity(), "GPS signal not found", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
new NearbyAsyncTask().execute(urlnearby);
return view;
}
class NearbyAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
dialog.cancel();
if (result == false){
//show a msg to user that data not parsed
Toast.makeText(getActivity().getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
@Override
protected Boolean doInBackground(String... params) {
try {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(params[0]);
HttpResponse response = client.execute(get);
int status = response.getStatusLine().getStatusCode();
if(status ==200){
//get the response and pars it
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jObj = new JSONObject(data);
JSONArray jArray = jObj.getJSONArray("nearby");
for(int i=0;i<jArray.length();i++ ){
JSONObject jRealObject = jArray.getJSONObject(i);
String title = jRealObject.getString("title");
String img = jRealObject.getString("image");
String lat= jRealObject.getString("latitude");
String lng= jRealObject.getString("longitude");
double latitude=Double.parseDouble(lat);
double longitude= Double.parseDouble(lng);
MarkerOptions near = new MarkerOptions().position(new LatLng(latitude,longitude)).title(title);
mGoogleMap.addMarker(near);
}
return true; //verifie the parse
}
}catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (JSONException e) {
e.printStackTrace();
}
//http call
return false;
}
}
}
答案 0 :(得分:0)
您需要重新构建代码才能正常工作。
您需要让异步任务在onPostExecute函数中返回一个包含所需数据的对象。
然后,在活动中,您将创建一个函数来加载从异步任务返回的标记。
您可以看到使用异步任务here
的一个很好的示例