我一直在尝试使用openmap库显示shapefile。在第一个活动上单击按钮时,应用程序转到第二个活动(也是第二个页面),但随后突然崩溃。
我运行调试器,意识到mapFragment变量仍然为null,它是从findFragmentById获取值的。 我最终打算在应用程序的第二页上显示一个shapefile。
public class ShapeFileParser extends AppCompatActivity implements OnMapReadyCallback {
GoogleMap mMap;
ShapeFile shapeFile;
File file;
public void setUpMap() {
try {
org.apache.commons.io.FileUtils.copyInputStreamToFile((ShapeFileParser.this.getAssets().open("healthsites.shp")), file);
} catch (IOException e) {
e.printStackTrace();
}
try {
shapeFile = new ShapeFile(file);
} catch (IOException e) {
e.printStackTrace();
}
try {
for (ESRIRecord esriRecord = shapeFile.getNextRecord(); esriRecord!=null; esriRecord = shapeFile.getNextRecord()){
String shapeTypeStr = ShapeUtils.getStringForType(esriRecord.getShapeType());
Log.v("myapp","shape type = " + esriRecord.getRecordNumber() + "-" + shapeTypeStr);
if (shapeTypeStr.equals("POLYGON")) {
// cast type after checking the type
ESRIPolygonRecord polyRec = (ESRIPolygonRecord)esriRecord;
Log.v("myapp","number of polygon objects = " + polyRec.polygons.length);
for (int i=0; i<polyRec.polygons.length; i++){
// read for a few layers
ESRIPoly.ESRIFloatPoly poly = (ESRIPoly.ESRIFloatPoly)polyRec.polygons[i];
PolygonOptions polygonOptions = new PolygonOptions();
polygonOptions.strokeColor(Color.argb(150,200,0,0));
polygonOptions.fillColor(Color.argb(150,0,0,150));
polygonOptions.strokeWidth(2.0f);
Log.v("myapp","Points in the polygon = " + poly.nPoints);
for (int j=0; j<poly.nPoints; j++){
//Log.v("myapp",poly.getY(j) + "," + poly.getX(j));
polygonOptions.add(new LatLng(poly.getY(j), poly.getX(j)));
}
mMap.addPolygon(polygonOptions);
Log.v("myapp","polygon added");
}
}
else {
Log.v("myapp","error polygon not found (type = " + esriRecord.getShapeType() + ")");
}
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initializeMap();
}
private void initializeMap() {
if (mMap == null) {
SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// mapFrag is null on checking with the debugger
mapFrag.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUpMap();
}
}
这是我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ShapeFileParser" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
我只是在尝试将形状文件渲染到第二页上的示例,但我觉得自最近两天以来我再也没有去过。请提出一些建议。
我是android开发的新手,请耐心等待。非常感谢!
答案 0 :(得分:1)
据我所知,OpenMap库用于Java Swing和JFrame应用程序,不能在Android应用程序中使用。 Google Map SDK提供了在Android应用程序中显示地图所需的一切,包括获取设备位置,显示位置信息,设置地图标记,折线和多边形。在背景中获取位置更新等。要进行设置,请从图库中创建一个新的Maps Activity。它将为您生成所有需要的文件,并为您提供获取API密钥的说明列表,以便可以在您的应用中使用Maps。 Here是一个问题,解释了我不久前回答的api密钥的安全性。 Google Maps Docs解释了如何进行地图设置的所有基本功能。我建议您访问YouTube Tutorials,了解如何在Android应用程序中设置地图。
<-编辑->
Here是大约两年前我在Stack Overflow上找到的答案。它实际上并没有使用OpenMap Libray,但这是一种解决方法。您可以提取ShapeFile数据并在Google Maps Polygon,Polyline和LatLng类中使用它们,并在Android应用程序的地图上显示这些数据。