I'm working on an extremely basic application which essentially gets your current postion and then allows the user to select a Overlay icon and eventually "get directions to location"
What i can do atm is Show the users current location and all the possible destinations they can possibly want to go to The problem lies im my basic implementation of showing a route What i have done thus far is gotten a route from 2 static locations ( i eventually will change this to the users location and a static location but for testing have it as 2 static) however when this route is shown the Other overlay of the current position and the markers disappears.
Here are my java files any help would be appreciated
MapViewActivity
public class MapViewActivity extends MapActivity implements LocationListener {
private static final String TAG = null;
LocationManager locman;
Location loc;
private MapView map;
private MapController controller;
public MapOverLay MyOverlay;
MyLocationOverlay overlay;
LinearLayout linearLayout;
MapView mapView;
private Road mRoad;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
initMapView();
initMyLocation();
//route();
/* *********************
* There seems to be a problem in here that when route() is run
* the HelloItemizedArray disappears and the route from the 2 static locations
* is shown , While this wouldn't be so bad for the application as there's no need
* to show any other stations when the user has picked one ... the problem is that the
* marker for the current location disappears or the user might need to pick a different station
* and i would like them to be able to do so without having to do the onCreate() again.
* **********************
*/
}
private void initMapView() {
map = (MapView) findViewById(R.id.mapview);
controller = map.getController();
map.setSatellite(false);
map.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = map.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
GeoPoint point = new GeoPoint((int)(53.34675984552593 * 1E6), (int)(-6.258945465087891 * 1E6));
// O'Conell Bridge
OverlayItem overlayitem = new OverlayItem(point, "O'Connell Bridge", "");
GeoPoint point2 = new GeoPoint((int)(53.33842 * 1E6), (int)(-6.25365 * 1E6));
//53.33842; -6.25365 -- Stephens green
OverlayItem overlayitem2 = new OverlayItem(point2, "Stephens green", "");
itemizedoverlay.addOverlay(overlayitem);
itemizedoverlay.addOverlay(overlayitem2);
mapOverlays.add(itemizedoverlay);
}
private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
overlay.enableMyLocation();
overlay.enableCompass(); // does not work in emulator
overlay.runOnFirstFix(new Runnable() {
public void run() {
// Zoom in to current location
controller.setZoom(15);
controller.animateTo(overlay.getMyLocation());
}
});
map.getOverlays().add(overlay);
}
public void route() {
double fromLat = 53.34675984552593, fromLon = -6.258945465087891, toLat = 53.33842, toLon = -6.25365;
String url = RoadProvider
.getUrl(fromLat, fromLon, toLat, toLon);
InputStream is = getConnection(url);
mRoad = RoadProvider.getRoute(is);
mHandler.sendEmptyMessage(0);
}
Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
TextView textView = (TextView) findViewById(R.id.description);
textView.setText(mRoad.mName + " " + mRoad.mDescription);
MapOverLay mapOverlay = new MapOverLay(mRoad, mapView);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
};
};
private InputStream getConnection(String url) {
InputStream is = null;
try {
URLConnection conn = new URL(url).openConnection();
is = conn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return is;
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
public void onLocationChanged(Location location) {
controller.animateTo(overlay.getMyLocation());
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
HelloItemizedOverlay.java
public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem>
{
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker, Context context)
{
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void addOverlay(OverlayItem overlay)
{
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i)
{
return mOverlays.get(i);
}
@Override
public int size()
{
return mOverlays.size();
}
@Override
protected boolean onTap(int index)
{
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setIcon(R.drawable.petrol);
dialog.setMessage(item.getSnippet());
dialog.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Implement the route to this location from this LISTENER
}
});
dialog.setNegativeButton("cancel", null);
dialog.show();
return true;
}
}
MapOverLay.java
class MapOverLay extends com.google.android.maps.Overlay {
Road mRoad;
ArrayList<GeoPoint> mPoints;
public MapOverLay(Road road, MapView mv) {
mRoad = road;
if (road.mRoute.length > 0) {
mPoints = new ArrayList<GeoPoint>();
for (int i = 0; i < road.mRoute.length; i++) {
mPoints.add(new GeoPoint((int) (road.mRoute[i][1] * 1000000),
(int) (road.mRoute[i][0] * 1000000)));
}
int moveToLat = (mPoints.get(0).getLatitudeE6() + (mPoints.get(
mPoints.size() - 1).getLatitudeE6() - mPoints.get(0)
.getLatitudeE6()) / 2);
int moveToLong = (mPoints.get(0).getLongitudeE6() + (mPoints.get(
mPoints.size() - 1).getLongitudeE6() - mPoints.get(0)
.getLongitudeE6()) / 2);
GeoPoint moveTo = new GeoPoint(moveToLat, moveToLong);
MapController mapController = mv.getController();
mapController.animateTo(moveTo);
mapController.setZoom(15);
}
}
@Override
public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
super.draw(canvas, mv, shadow);
drawPath(mv, canvas);
return true;
}
public void drawPath(MapView mv, Canvas canvas) {
int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
for (int i = 0; i < mPoints.size(); i++) {
Point point = new Point();
mv.getProjection().toPixels(mPoints.get(i), point);
x2 = point.x;
y2 = point.y;
if (i > 0) {
canvas.drawLine(x1, y1, x2, y2, paint);
}
x1 = x2;
y1 = y2;
}
}
}
Any help tips or advice would be truly appreciated thank you.