当我运行此代码时,它显示错误。请帮帮我。我试图在安卓操作系统的谷歌地图中找到从一个地方到另一个地方的路线。
我已经提交了我所有的代码。我想我不需要提交我的 xml 布局代码。比方说,它只是地图视图。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screenfive);
coder = new Geocoder(this);
EditText startedit =(EditText) findViewById(R.id.startedit);
EditText finishedit =(EditText) findViewById(R.id.finishedit);
Button go=(Button) findViewById(R.id.go);
Spinner spinnercategory = (Spinner) findViewById (R.id.spinnercategory);
Category =(TextView) findViewById(R.id.category);
mapView = (MapView) findViewById(R.id.mymapview2);
mapView.setBuiltInZoomControls(true);
mMapController = mapView.getController();
// mMapController.setZoom(18);
// Two points in Mexico about 1km apart
//Take any two points
GeoPoint point1 = new GeoPoint(19240000,-99120000);
GeoPoint point2 = new GeoPoint(19241000,-99121000);
mMapController.setCenter(point2);
// Pass the geopoints to the overlay class
mapOvlay = new MapOverlay(point1, point2);
mapView.getOverlays().add(mapOvlay);
spinnercategory.setOnItemSelectedListener(this);
ArrayAdapter aa=new ArrayAdapter(this, android.R.layout.simple_spinner_item,category);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnercategory.setAdapter(aa);
go.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new GetLatLong().execute();
}
});
}
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// Category.setText(category[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public class MapOverlay extends com.google.android.maps.Overlay {
private GeoPoint mGpt1;
private GeoPoint mGpt2;
protected MapOverlay(GeoPoint gp1, GeoPoint gp2 ) {
mGpt1 = gp1;
mGpt2 = gp2;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
Paint paint;
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(2);
Point pt1 = new Point();
Point pt2 = new Point();
Projection projection = mapView.getProjection();
projection.toPixels(mGpt1, pt1);
projection.toPixels(mGpt2, pt2);
canvas.drawLine(pt1.x, pt1.y, pt2.x, pt2.y, paint);
return true;
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
class GetLatLong extends AsyncTask<String, Void, String>
{
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
ProgressDialog=new ProgressDialog(MapActivityForScreen5.this);
ProgressDialog.setTitle("Loading");
ProgressDialog.show();
}
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try{
addressgone = coder.getFromLocationName(startedit.getText().toString(),5);
if (addressgone == null) {
}
Address locationgone = addressgone.get(0);
locationgone.getLatitude();
locationgone.getLongitude();
pone = new GeoPoint((int) (locationgone.getLatitude() * 1E6),
(int) (locationgone.getLongitude() * 1E6));
}
catch(Exception e)
{
}
try{
addressgtwo = coder.getFromLocationName(startedit.getText().toString(),5);
if (addressgtwo == null) {
}
Address locationgtwo = addressgtwo.get(0);
locationgtwo.getLatitude();
locationgtwo.getLongitude();
ptwo = new GeoPoint((int) (locationgtwo.getLatitude() * 1E6),
(int) (locationgtwo.getLongitude() * 1E6));
}
catch(Exception e)
{
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
ProgressDialog.cancel();
mMapController.setCenter(ptwo);
// Pass the geopoints to the overlay class
mapOvlay = new MapOverlay(pone, ptwo);
mapView.getOverlays().add(mapOvlay);
}
}
}