我正在开发一个非常基本的应用程序,它基本上可以获取当前位置,然后允许用户选择叠加图标并最终“获取位置指示”
我可以做的是显示用户当前位置以及他们可能想要去的所有可能目的地 问题在于我显示路线的基本实现 到目前为止我所做的是从2个静态位置获得一条路线(我最终会将其更改为用户位置和静态位置,但测试时将其设置为2静态)但是当显示此路线时,当前位置的其他叠加并且标记消失了。
以下是我的java文件,任何帮助将不胜感激
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;
}
}
}
任何帮助提示或建议都将得到真正的赞赏 谢谢。
答案 0 :(得分:2)
抱歉在我使用的代码中发现了我的错误 listOfOverlays.clear();
在我的代码中太早,直到用户确认他们想要去那里(以后在应用程序中使用)并不意味着 只是想我会回答它,以防其他人想要使用代码并遇到类似的问题