如何打开 KML 文件(来自资产文件夹)
How to Open KML file (from assets folder)
我在资产文件夹中有一个 KML 文件。我想打开它进入 google 地球或 google 地图。但其中 none 有效。
这是我试过的:
对于Google地图
private GoogleMap map;
KmlLayer layer;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_locations);
try {
layer = new KmlLayer(map,getAssets().open("locations.kml"),context);
layer.addLayerToMap();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
为了Google地球
File KML = null;
try {
KML = new File(String.valueOf(getAssets().open("locations.kml")));
} catch (IOException e) {
e.printStackTrace();
}
Intent i =
getPackageManager().getLaunchIntentForPackage("com.google.earth");
i.setDataAndType(Uri.fromFile(KML), "xml");
startActivity(i);
- 使用 Google 地图 Activity 创建一个新项目。
现在在 onMapReady() 方法中使用 retrieveFileFromResource()
private void retrieveFileFromResource() {
try {
KmlLayer kmlLayer = new KmlLayer(mMap, R.raw.kml, getApplicationContext());
kmlLayer.addLayerToMap();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
3.Full代码
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
retrieveFileFromResource();
}
private void retrieveFileFromResource() {
try {
KmlLayer kmlLayer = new KmlLayer(mMap, R.raw.kml, getApplicationContext());
kmlLayer.addLayerToMap();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
}
我在资产文件夹中有一个 KML 文件。我想打开它进入 google 地球或 google 地图。但其中 none 有效。 这是我试过的:
对于Google地图
private GoogleMap map;
KmlLayer layer;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_locations);
try {
layer = new KmlLayer(map,getAssets().open("locations.kml"),context);
layer.addLayerToMap();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
为了Google地球
File KML = null;
try {
KML = new File(String.valueOf(getAssets().open("locations.kml")));
} catch (IOException e) {
e.printStackTrace();
}
Intent i =
getPackageManager().getLaunchIntentForPackage("com.google.earth");
i.setDataAndType(Uri.fromFile(KML), "xml");
startActivity(i);
- 使用 Google 地图 Activity 创建一个新项目。
现在在 onMapReady() 方法中使用 retrieveFileFromResource()
private void retrieveFileFromResource() { try { KmlLayer kmlLayer = new KmlLayer(mMap, R.raw.kml, getApplicationContext()); kmlLayer.addLayerToMap(); } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } }
3.Full代码
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; retrieveFileFromResource(); } private void retrieveFileFromResource() { try { KmlLayer kmlLayer = new KmlLayer(mMap, R.raw.kml, getApplicationContext()); kmlLayer.addLayerToMap(); } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } } }