我想绘制一条线(将一些LatLong坐标连接到mapforge-map)。 但我不知道如何为latLongs列表创建叠加层并将叠加层与地图连接起来。 我使用的是mapsforge 0.5.0,其中包括: mapsforge-map-android-0.5.0.jar,mapsforge-map-0.5.0.jar, mapsforge-core-android-0.5.0.jar,mapsforge-map-reader-0.5.0.jar
映射文件存在于SD卡上并显示。
代码:
package com.example.ad.mapsforge;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import org.mapsforge.core.graphics.GraphicFactory;
import org.mapsforge.core.graphics.Paint;
import org.mapsforge.core.graphics.Style;
import org.mapsforge.core.model.LatLong;
import org.mapsforge.map.android.graphics.AndroidGraphicFactory;
import org.mapsforge.map.android.graphics.AndroidResourceBitmap;
import org.mapsforge.map.android.util.AndroidUtil;
import org.mapsforge.map.android.view.MapView;
import org.mapsforge.map.layer.cache.TileCache;
import org.mapsforge.map.layer.overlay.Polyline;
import org.mapsforge.map.layer.renderer.TileRendererLayer;
import org.mapsforge.map.rendertheme.InternalRenderTheme;
import java.io.File;
import java.util.List;
public class MainActivity extends ActionBarActivity {
// name of the map file in the external storage
private static final String MAPFILE = "saarland.map";//sachsen-anhalt.map";
private MapView mapView;
private TileCache tileCache;
private TileRendererLayer tileRendererLayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidGraphicFactory.createInstance(this.getApplication());
this.mapView = new MapView(this);
setContentView(this.mapView);
this.mapView.setClickable(true);
this.mapView.getMapScaleBar().setVisible(true);
this.mapView.setBuiltInZoomControls(true);
this.mapView.getMapZoomControls().setZoomLevelMin((byte) 10);
this.mapView.getMapZoomControls().setZoomLevelMax((byte) 20);
// create a tile cache of suitable size
this.tileCache = AndroidUtil.createTileCache(this, "mapcache",
mapView.getModel().displayModel.getTileSize(), 1f,
this.mapView.getModel().frameBufferModel.getOverdrawFactor());
}
@Override
protected void onStart() {
super.onStart();
GraphicFactory gf=AndroidGraphicFactory.INSTANCE;
Paint paint=gf.createPaint();
// paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(7);
paint.setColor(Color.BLUE);
this.mapView.getModel().mapViewPosition.setCenter(new LatLong(49.2333, 7.0));
this.mapView.getModel().mapViewPosition.setZoomLevel((byte) 12);
// tile renderer layer using internal render theme
this.tileRendererLayer = new TileRendererLayer(tileCache,
this.mapView.getModel().mapViewPosition, false,true, AndroidGraphicFactory.INSTANCE);
tileRendererLayer.setMapFile(getMapFile());
tileRendererLayer.setXmlRenderTheme(InternalRenderTheme.OSMARENDER);
Polyline pl=new Polyline(paint,gf);
List latLongs=pl.getLatLongs();
latLongs.add(new LatLong(49.2333, 7.0));
latLongs.add(new LatLong(49.2333, 7.02));
// only once a layer is associated with a mapView the rendering starts
this.mapView.getLayerManager().getLayers().add(tileRendererLayer);
}
@Override
protected void onStop() {
super.onStop();
this.mapView.getLayerManager().getLayers().remove(this.tileRendererLayer);
this.tileRendererLayer.onDestroy();
}
@Override
protected void onDestroy() {
super.onDestroy();
this.tileCache.destroy();
this.mapView.getModel().mapViewPosition.destroy();
this.mapView.destroy();
AndroidResourceBitmap.clearResourceBitmaps();
}
private File getMapFile() {
File file = new File(Environment.getExternalStorageDirectory(), MAPFILE);
return file;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
请帮忙!
关心Wicki
答案 0 :(得分:0)
来自graphhopper来源:
private Polyline createPolyline(GHResponse response)
{
Paint paintStroke = Utils.createPaint(AndroidGraphicFactory.INSTANCE
.createColor(org.mapsforge.core.graphics.Color.GREEN), 1,
Style.STROKE);
paintStroke.setDashPathEffect(new float[] { 25, 15 });
paintStroke.setStrokeWidth(5);
paintStroke.setStrokeWidth(3);
Polyline line = new Polyline(paintStroke,
AndroidGraphicFactory.INSTANCE);
List<LatLong> geoPoints = line.getLatLongs();
PointList tmp = response.getPoints();
for (int i = 0; i < response.getPoints().getSize(); i++) {
geoPoints.add(new LatLong(tmp.getLatitude(i), tmp.getLongitude(i)));
}
return line;
}
根据您的需要调整......