起初我尝试将我的.kml文件直接打开到谷歌地图,但我认为这是不可能的(如果它是请告诉我该怎么做)。然后我找到了this主题,我尝试应用bes答案,所以我写了这个。
这是 MapsActivity.java :
private GoogleMap mMap;
KmlLayer layer;
@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);
try
{
layer = new KmlLayer(mMap, R.raw.kml, getApplicationContext());
layer.addLayerToMap();
}
catch (Exception e)
{
e.printStackTrace();
}
}
(onMapReady方法为空) KmlLayer对象需要 kml.kml :
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Route code: 7FFC2D3811814A669BAA1132E6744CB0</name>
<Style id="street_sidewalk">
<LineStyle>
<color>
ff235523
</color>
<gx:physicalWidth>
10
</gx:physicalWidth>
<gx:outerColor>
ff55ff55
</gx:outerColor>
<gx:outerWidth>
0.25
</gx:outerWidth>
</LineStyle>
</Style>
<Placemark>
<styleUrl>
#street_sidewalk
</styleUrl>
<LineString>
<coordinates>
13.435562703221839,43.28938437138311
13.434956825868248,43.28839549601068
13.434658941830964,43.28772183874893
13.433823000117261,43.286668229942975
13.432504543423665,43.28589083880599
13.432473373695817,43.28468480185478
13.432433498842306,43.283785982847064
13.43298492291549,43.283089083348884
13.432850084751177,43.281928911447416
13.431985068565941,43.28077479256134
13.432291468687168,43.279320736893155
13.430996689956075,43.27824031114284
13.431267569267739,43.276764089684605
13.431312604893021,43.27515907089356
13.429398629924627,43.27418697639965
13.427423839789741,43.272970705214895
13.426088298846418,43.27133476603859
13.424925041132077,43.26971823334876
13.42377103443275,43.26811056405547
13.42268108914332,43.26660560727693
13.421663395458483,43.26522743400932
13.420725065017466,43.26392596843247
13.421103747155446,43.26281595152314
13.420445192382113,43.26264081584839
13.419699177054117,43.26263771669724
13.419033788762782,43.2628669545061
13.418953806658525,43.26301348140324
</coordinates>
</LineString>
</Placemark>
</Document>
</kml>
这是错误:
05-30 16:43:24.880 19860-19860/com.example.android.gpsv01 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.gpsv01, PID: 19860
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.gpsv01/com.example.android.gpsv01.MapsActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2924)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
Caused by: java.lang.RuntimeException: API key not found. Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
at com.google.maps.api.android.lib6.drd.q.b(:com.google.android.gms.dynamite_dynamitemodulesb@12685025@12.6.85 (040408-197041431):40)
at com.google.maps.api.android.lib6.auth.e.a(:com.google.android.gms.dynamite_dynamitemodulesb@12685025@12.6.85 (040408-197041431):11)
at com.google.maps.api.android.lib6.impl.e.a(:com.google.android.gms.dynamite_dynamitemodulesb@12685025@12.6.85 (040408-197041431):4)
at com.google.android.gms.maps.internal.b.a(:com.google.android.gms.dynamite_dynamitemodulesb@12685025@12.6.85 (040408-197041431):24)
at com.google.android.gms.maps.internal.CreatorImpl.a(:com.google.android.gms.dynamite_dynamitemodulesb@12685025@12.6.85 (040408-197041431):69)
at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(:com.google.android.gms.dynamite_dynamitemodulesb@12685025@12.6.85 (040408-197041431):32)
at com.google.android.gms.maps.internal.i.onTransact(:com.google.android.gms.dynamite_dynamitemodulesb@12685025@12.6.85 (040408-197041431):31)
at android.os.Binder.transact(Binder.java:507)
at com.google.android.gms.internal.maps.zza.transactAndReadException(Unknown Source)
at com.google.android.gms.maps.internal.zzf.zzc(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$zzb.zzc(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$zzb.createDelegate(Unknown Source)
at com.google.android.gms.dynamic.DeferredLifecycleHelper.zza(Unknown Source)
at com.google.android.gms.dynamic.DeferredLifecycleHelper.onInflate(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
at android.support.v4.app.Fragment.onInflate(Fragment.java:1208)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3697)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:350)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:784)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
at android.view.LayoutInflater.inflate(LayoutInflater.java:496)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:378)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:439)
at android.app.Activity.setContentView(Activity.java:2543)
at com.example.android.gpsv01.MapsActivity.onCreate(MapsActivity.java:34)
at android.app.Activity.performCreate(Activity.java:6912)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
最初我认为问题是由损坏的KML文件引起的,但我尝试在Google地球中打开它并且它有效。
这是我的 AndroidManifest.xml :
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<meta-data
android:name="com.google.android.geo.xxxxx"
android:value="@string/google_maps_key" />
</activity>
当然,在文件字符串中我设置了正确的值