2016-04-24 34 views
1

我讀谷歌Android地圖API - 訪問KML容器在KML文件中檢索從嵌套集裝箱數據

https://developers.google.com/maps/documentation/android-api/utility/kml#clear

要訪問的是嵌套在一個KmlLayer或KmlContainer容器:

Iterable containers = layer.getContainers(); 


public void accessContainers(containers) { 
for (KmlContainer container : containers) { 
// Do something to container 
if (container.hasContainers()) { 
    accessContainers(container.getContainers()); 
} 

}}

我應該在哪裏把這個方法的程序?

在演示progrma,我發現這些,但我仍然不知道如何使用它

//Retrieve the first container in the KML layer 
    KmlContainer container = kmlLayer.getContainers().iterator().next(); 
    //Retrieve a nested container within the first container 
    container = container.getContainers().iterator().next(); 

這裏是我的程序

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{ 

private GoogleMap mMap; 
private KmlLayer layer; 
private KmlContainer container; 
@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; 
    try { 
     layer = new KmlLayer(mMap, R.raw.canberra, getApplicationContext()); 
     layer.addLayerToMap(); 
     containers = layer.getContainers(); 

    } catch (XmlPullParserException e) 
    {e.printStackTrace();} 
    catch (IOException e) 
    { e.printStackTrace();} 

    LatLng sydney = new LatLng(-34, 151); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
} 

}

回答

0

在哪裏我應該把這個方法放在程序中嗎?

您可以將代碼中的異步任務中,並調用它的PostExecute步,如:

//function to be called inside Async Task 
private void moveCameraToKml(KmlLayer kmlLayer) { 
     //Retrieve the first container in the KML layer 
     KmlContainer container = kmlLayer.getContainers().iterator().next(); 
     //Retrieve a nested container within the first container 
     container = container.getContainers().iterator().next(); 
     //Retrieve the first placemark in the nested container 
     KmlPlacemark placemark = container.getPlacemarks().iterator().next(); 
     //Retrieve a polygon object in a placemark 
     KmlPolygon polygon = (KmlPolygon) placemark.getGeometry(); 
     //Create LatLngBounds of the outer coordinates of the polygon 
     LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
     for (LatLng latLng : polygon.getOuterBoundaryCoordinates()) { 
      builder.include(latLng); 
     } 
     getMap().moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 1)); 
    } 

    private class DownloadKmlFile extends AsyncTask<String, Void, byte[]> { 
     private final String mUrl; 

     public DownloadKmlFile(String url) { 
      mUrl = url; 
     } 

     protected byte[] doInBackground(String... params) { 
      try { 
       InputStream is = new URL(mUrl).openStream(); 
       ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 
       int nRead; 
       byte[] data = new byte[16384]; 
       while ((nRead = is.read(data, 0, data.length)) != -1) { 
        buffer.write(data, 0, nRead); 
       } 
       buffer.flush(); 
       return buffer.toByteArray(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     protected void onPostExecute(byte[] byteArr) { 
      try { 
       kmlLayer = new KmlLayer(mMap, new ByteArrayInputStream(byteArr), 
         getApplicationContext()); 
       kmlLayer.addLayerToMap(); 
       moveCameraToKml(kmlLayer); 
      } catch (XmlPullParserException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

全樣本here