简体   繁体   English

在Xamarin Forms Android中循环出arraylist时,无法获取KML文件地标

[英]Unsuccessful in getting out KML-file placemarks when looping out an arraylist in Xamarin Forms Android

I have been working with a project that uses google maps and kml-routes. 我一直在使用Google地图和kml路线的项目中工作。 This project worked successfully a few months ago but now when I debug it the app crashes with the following errormessage: 该项目在几个月前成功运行,但是现在当我对其进行调试时,应用程序崩溃并出现以下错误消息:

System.ArgumentNullException

If I comment away... 如果我发表评论...

foreach (var data in lineString.ToEnumerable())
{

}

...then the app successfully runs meaning that it can't get the values inside ”Placemarks”. ...然后该应用程序成功运行,这意味着它无法获取“地标”中的值。

This is the code: 这是代码:

public async void OnMapReady(GoogleMap googleMap)
{
     InvokeOnMapReadyBaseClassHack(googleMap);
     map = googleMap;
     layer = new KmlLayer(map, Resource.Raw.mykmlfile, Android.App.Application.Context); 
}

async void MoveCameraToKmlTwo(KmlLayer kmlLayer)
{
        var list = new List<object>();

        if (kmlLayer.HasContainers)
        {
            void IterateProperties(KmlContainer containers)
            {
                foreach (var property in containers.Properties.ToEnumerable())
                {

                }
            }
            void IterateLineString(ArrayList lineString)
            {
                //when i try to get out the data in here i get the crash
                foreach (var data in lineString.ToEnumerable())
                {

                }
            }

            void IteratePlaceMarks(KmlContainer container)
            {
                foreach (KmlPlacemark placemark in container.Placemarks.ToEnumerable())
                {
                    IterateProperties(container);
                    if (placemark.HasGeometry & placemark.Geometry is KmlLineString)
                    {
                        IterateLineString(placemark.Geometry.GeometryJavaObject() as Java.Util.ArrayList);

                    }
                }
            }

            void IterateSubContainers(KmlContainer container)
            {
                IterateProperties(container);
                IteratePlaceMarks(container);
                if (container.HasContainers)
                {
                    foreach (KmlContainer subContainer in container.Containers.ToEnumerable())
                        IterateSubContainers(subContainer);
                }
            }

            foreach (KmlContainer container in kmlLayer.Containers.ToEnumerable())
                IterateSubContainers(container);

        }
}

My kml file looks like this: 我的kml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">

<Document>
<name>Routes</name>
<description></description>
    <visibility>1</visibility>
    <open>1</open>

    <Style id="dark_green">
        <LineStyle>
        <color>C8008C14</color>
        <width>4</width>
        </LineStyle>
    </Style>

    <Style id="route_dark_blue">
        <LineStyle>
        <color>96F01414</color>
        <width>4</width>
        </LineStyle>
    </Style>


  <Folder>
        <name>Tracks</name>
        <description>A list of tracks</description>
        <visibility>1</visibility>            
        <open>0</open>                                      
            <Placemark>
                <visibility>0</visibility>            
                <open>0</open> 
                <styleUrl>#red</styleUrl>
                <name>Around the lake</name>
                <description>Track no. 1</description>
                <LineString>
                    <extrude>true</extrude>
                    <tessellate>true</tessellate>
                    <altitudeMode>clampToGround</altitudeMode> 
                    <coordinates>
                        7.366653,42.281982,106.075562 7.366759,42.282024,99.504028 7.366846,42.282043,95.945312 7.366909,42.282085,94.900269 7.366982,42.282116,94.186218 7.367039,42.282150,90.530640 7.367124,42.282135,87.749268 7.367111,42.282192,89.794800 7.367088,42.282242,90.636597 7.367079,42.282299,89.214539                             
                    </coordinates>
                </LineString>
            </Placemark>                          
    </Folder>
  </Document>
 </kml>

I think the problem is this cast 我认为问题是这个演员

placemark.Geometry.GeometryJavaObject() as Java.Util.ArrayList

Instead you should use 相反,您应该使用

placemark.Geometry.GeometryJavaObject().JavaCast<Java.Util.ArrayList>()

That is if you are 100% certain what you get back is a ArrayList . 也就是说,如果您100%确定返回的是ArrayList

You can always figure out whether you need to use JavaCast<T>() by trying to cast explicitly. 您始终可以通过尝试显式JavaCast<T>()来弄清楚是否需要使用JavaCast<T>() Why JavaCast works while a normal cast doesn't can be read about in the Xamarin Documentation about JavaCast<T>() . 在Xamarin文档中有关JavaCast<T>()内容中可以找到为什么JavaCast可以正常运行而无法正常JavaCast<T>() In short terms: it is a hack... 简而言之:这是骇客...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM