Search code examples
androidmapsdirections

Google Map V2 API Directions : no error but not work well


I'm new in Android. I'm trying to make MAP project.. I find the code from some blog, and i edit it with the way that may work, but in the and the Output just keep doing progressDialog...never end

this is my code

public class LokasiActivity extends FragmentActivity{ 

public static final String USER_CURRENT_LAT = "user_current_lat";
public static final String USER_CURRENT_LONG = "user_current_long";
public static final String DESTINATION_LAT = "destination_lat";
public static final String DESTINATION_LONG = "destination_long";
public static final String DIRECTIONS_MODE = "directions_mode";
private static final LatLng AMSTERDAM = new LatLng(52.37518, 4.895439);
private static final LatLng PARIS = new LatLng(48.856132, 2.352448);
private Exception exception;
private ProgressDialog progressDialog;
private GoogleMap googleMap;
Button btnjalur;
ArrayList<LatLng> directionPoints;
SupportMapFragment fragment;
private Polyline newPolyline;
private int width, height;
private LatLngBounds latlngBounds;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lokasi_layout);

    btnjalur = (Button)findViewById(R.id.btnJalur);

    getSreenDimanstions();

      fragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
      googleMap = fragment.getMap();

    btnjalur.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            findDirections( AMSTERDAM.latitude, AMSTERDAM.longitude, PARIS.latitude, PARIS.longitude, LokasiDirection.MODE_DRIVING );   

        }
    });
}


public void handleGetDirectionsResult() {
    PolylineOptions rectLine = new PolylineOptions().width(5).color(Color.RED);

    for(int i = 0 ; i < directionPoints.size() ; i++) 
    {          
        rectLine.add(directionPoints.get(i));
    }
    if (newPolyline != null)
    {
        newPolyline.remove();
    }
    newPolyline = googleMap.addPolyline(rectLine);

        latlngBounds = createLatLngBoundsObject(AMSTERDAM, PARIS);
        googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(latlngBounds, width, height, 150));


}

private void getSreenDimanstions()
{
    Display display = getWindowManager().getDefaultDisplay();
    width = display.getWidth(); 
    height = display.getHeight(); 
}

private LatLngBounds createLatLngBoundsObject(LatLng firstLocation, LatLng secondLocation)
{
    if (firstLocation != null && secondLocation != null)
    {
        LatLngBounds.Builder builder = new LatLngBounds.Builder();    
        builder.include(firstLocation).include(secondLocation);

        return builder.build();
    }
    return null;
}

public class GetDirectionsAsyncTask extends AsyncTask<Map<String, String>, Object, ArrayList<LatLng>>
{

    public void onPreExecute()
    {
        progressDialog = new ProgressDialog(LokasiActivity.this);
        progressDialog.setMessage("Calculating directions");
        progressDialog.show();
    }


    @Override
    protected ArrayList<LatLng> doInBackground(Map<String, String>... params)
    {
        Map<String, String> paramMap = params[0];
        try
        {
            LatLng fromPosition = new LatLng(Double.valueOf(paramMap.get(USER_CURRENT_LAT)) , Double.valueOf(paramMap.get(USER_CURRENT_LONG)));
            LatLng toPosition = new LatLng(Double.valueOf(paramMap.get(DESTINATION_LAT)) , Double.valueOf(paramMap.get(DESTINATION_LONG)));
            LokasiDirection md = new LokasiDirection();
            Document doc = md.getDocument(fromPosition, toPosition, paramMap.get(DIRECTIONS_MODE));
            directionPoints = md.getDirection(doc);
            return directionPoints;
        }
        catch (Exception e)
        {
            exception = e;
            return null;
        }
    }

    public void onPostExecute()
    {
        progressDialog.dismiss();
        if (exception == null)
        {
            handleGetDirectionsResult();
        }
        else
        {
            processException();
        }
    }


    private void processException()
    {
        Toast.makeText(LokasiActivity.this, "Error retriving data", 3000).show();
    }
}

public void findDirections(double fromPositionDoubleLat, double fromPositionDoubleLong, double toPositionDoubleLat, double toPositionDoubleLong, String mode)
{
    Map<String, String> resultdestination = new HashMap<String, String>();
    resultdestination.put(USER_CURRENT_LAT, String.valueOf(fromPositionDoubleLat));
    resultdestination.put(USER_CURRENT_LONG, String.valueOf(fromPositionDoubleLong));
    resultdestination.put(DESTINATION_LAT, String.valueOf(toPositionDoubleLat));
    resultdestination.put(DESTINATION_LONG, String.valueOf(toPositionDoubleLong));
    resultdestination.put(DIRECTIONS_MODE, mode);

    new GetDirectionsAsyncTask().execute(resultdestination);
}
@Override
protected void onResume() {
    super.onResume();
    latlngBounds = createLatLngBoundsObject(AMSTERDAM, PARIS);
    googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latlngBounds, width, height, 150));


}

}

there is something wrong with my code.... please tell me if u realize that..


Solution

  • Output just keep doing progressDialog...never end

    You never dismiss the progress dialog. The code in onPostExecute() is never executed since the method signature is wrong.

    Change

    public void onPostExecute()
    

    to

    @Override
    public void onPostExecute(ArrayList<LatLng> result)
    

    The @Override annotation helps the compiler make sure you're in fact overriding a method by emitting an error in case you're not.