2016-10-08 38 views
1

我使用Google Maps API代替簡單的應用程序,並且我有一個帶有1000標記的KML文件,我想在我的應用程序的MAP中顯示該文件。如何使用GMaps API將KML文件導入Android應用程序

如何導入我的地圖中的KML文件?

我使用Android工作室

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 


    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    mMap.setMyLocationEnabled(true); 


} 

回答

2

你需要渲染地圖,並在其頂部添加KML層。例如:

KmlLayer kmlLayer = new KmlLayer(mMap, R.raw.coordinates, getApplicationContext()); 
kmlLayer.addLayerToMap(); 

參考this GitHub的樣本項目的完整樣本

2

如果您想從本地資源加載KML數據集,它是這樣的:

KmlLayer layer = new KmlLayer(getMap(), R.raw.kmlFile, getApplicationContext()); 
layer.addLayerToMap(); 

如果你想從本地流加載KML數據集,如下所示:

KmlLayer layer = new KmlLayer(getMap(), kmlInputStream, getApplicationContext()); 
layer.addLayerToMap(); 

Mor詳情請見Maps Andriod API documentation

相關問題