0

Hesham Saeed によって書かれたコードがあります。AsyncTask は Network を使っているので方向機能を入れたい。Androidが初めてなので、どうすればそれを行うことができますか。AsyncTask で方向を移動する必要があります。

メイン アクティビティ コード。

package com.example.googlemaptst;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

public class MapsActivity extends MapActivity
{    

/** Called when the activity is first created. */
Button b;
private ProgressDialog pDialog;
private  Route directions(final GeoPoint start, final GeoPoint dest)  {
   Parser parser;
    String jsonURL = "http://maps.google.com/maps/api/directions/json?";
    final StringBuffer sBuf = new StringBuffer(jsonURL);
    sBuf.append("origin=");
    sBuf.append(start.getLatitudeE6()/1E6);
    sBuf.append(',');
    sBuf.append(start.getLongitudeE6()/1E6);
    sBuf.append("&destination=");
    sBuf.append(dest.getLatitudeE6()/1E6);
    sBuf.append(',');
    sBuf.append(dest.getLongitudeE6()/1E6);
    sBuf.append("&sensor=true&mode=driving");
    Log.v("I came in URL", sBuf.toString());
    parser = new GoogleParser(sBuf.toString());
    Route r =  parser.parse();
    return r;
}

@Override
public void onCreate(Bundle savedInstanceState)
{
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_map);
     MapView mapView = (MapView) findViewById(R.id.mapView); //or you can declare it directly with the API key
     Route route = directions(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new GeoPoint((int)(26.3*1E6),(int)(50.7*1E6)));
     RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
     mapView.getOverlays().add(routeOverlay);

}

@Override
protected boolean isRouteDisplayed() {

    return false;
}
}
4

2 に答える 2

1

わかりました、ここにあなたがしなければならないことがあります:

    MapView mapView = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_map);
     mapView = (MapView) findViewById(R.id.mapView); //or you can declare it directly with the API key
     DirectionsTask getDirectionsTask = new DirectionsTask();
     getDirectionsTask.execute(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new GeoPoint((int)(26.3*1E6),(int)(50.7*1E6)));

}

private class DirectionsTask extends AsyncTask<GeoPoint, Void, Route> {

    protected Route doInBackground(GeoPoint... geoPointParams) {
        GeoPoint start = geoPointParams[0];
        GeoPoint dest = geoPoint[1];

           Parser parser;
            String jsonURL = "http://maps.google.com/maps/api/directions/json?";
            final StringBuffer sBuf = new StringBuffer(jsonURL);
            sBuf.append("origin=");
            sBuf.append(start.getLatitudeE6()/1E6);
            sBuf.append(',');
            sBuf.append(start.getLongitudeE6()/1E6);
            sBuf.append("&destination=");
            sBuf.append(dest.getLatitudeE6()/1E6);
            sBuf.append(',');
            sBuf.append(dest.getLongitudeE6()/1E6);
            sBuf.append("&sensor=true&mode=driving");
            Log.v("I came in URL", sBuf.toString());
            parser = new GoogleParser(sBuf.toString());
            Route r =  parser.parse();
            return r;
    }


}

protected void onPostExecute(Route route) {
    RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
     mapView.getOverlays().add(routeOverlay);
}

いくつかのコメント。mapView を OnCreate の外に配置して、onPostExecute がアクセスできるようにします。onPostExecute は UI スレッドで呼び出されるので、問題ありません。

于 2012-09-17T16:28:28.973 に答える
0
package Logic;

public class ReadConfig extends AsyncTask<String, Integer, String> {

    public ReadConfig(Context context) {
        this.context = context; 
    }

    protected void onPreExecute() {

        //initialize progressbar
    }

    @Override
    protected String doInBackground(String... params) {

        //do you processing here, or call a method to do it for you.
        return "";
    }

    protected void onPostExecute(String result) {

      //cancel progressdialog and do whatever you need to do after background job has completed
    }
}

AsyncTaskこれを次のように呼び出します。

new ReadConfig(getApplicationContext()).execute("");
于 2012-09-17T13:18:03.837 に答える