How to add KMLlayer to Android GoogleMap

送分小仙女□ 提交于 2019-12-11 08:17:51

问题


based on this link "https://developers.google.com/maps/documentation/android-api/utility/kml" I have followed it until to the point where I have to type this code:

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

I'm having a red line in 'getMap()' and 'R.raw.kmlFile' and I can't understand what it's trying to say in the part where he says

***To import and render a KML dataset from a local resource, you need:

A GoogleMap object where the layer is to be rendered. A local resource file containing the KML data. A Context object, which is required to open a local resource file.***

Can you please guide me on what to do.


回答1:


You need to get map and then add a KML layer on top of it. Something like that:

...

private GoogleMap mMap;

...

@Override
public void onMapReady(GoogleMap googleMap) {

    mMap = googleMap;
    ...
}

...

public void addKML() {
    KmlLayer layer = new KmlLayer(mMap, R.raw.kmlFile, getApplicationContext());
    layer.addLayerToMap();
}  
...

For details take a look at this file in that repo.




回答2:


To get rid of the getMap() error, you need to create a Google Map object and pass it in. The code Andrii provided would solve the problem.

To get rid of the error at 'R.raw.kmlFile', you need to create a raw folder.

Right-click the res folder, then go to New -> Android resource directory. Set the Directory name and Directory type as "raw", then click OK. Here is a more detailed guide.



来源:https://stackoverflow.com/questions/45562083/how-to-add-kmllayer-to-android-googlemap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!