Google Map V2 API说明:没有错误,但效果不佳

时间:2014-09-15 20:08:50

标签: android maps directions

我是Android的新手。我正在尝试制作MAP项目..我从一些博客中找到代码,并且我用可能的方式编辑它,但是在输出中只是继续进行progressDialog ...永远不会结束

这是我的代码

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));


}

}

我的代码有问题....请告诉我你是否意识到这一点......

1 个答案:

答案 0 :(得分:0)

  

输出只是继续进行progressDialog ...永远不会结束

您永远不会忽略进度对话框。由于方法签名错误,onPostExecute()中的代码永远不会被执行。

更改

public void onPostExecute()

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

@Override注释有助于编译器确保您实际上通过发出错误来覆盖方法,以防万一。