When I run this code, it shows error. Please, help me out. I’m trying to find route from one place to another in Google map in android operating system.
I’ve submitted all my code. I think I don’t need to submit my xml code of layout. Let’s say, it’s simply map view.
@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);
}
}
}
Finally I did it. I’m posting this answer so that it can be useful for you.
I did it by using JSON(fetched latitude and longitude in the EditText) and drew the route between 2 places using Google map services.
Here’s the code: