"object reference not set to an instance of an object" when trying to get lat, lng from a KML-layer to Google Maps [Xamarin Android] -
i working kml-file via plugin: https://github.com/sushihangover/sushihangover.android.maps.utils
the kml-file using gets added succesfully via codesnippet:
var kmllayer = new kmllayer(googlemap, resource.raw.campus, android.app.application.context); kmllayer.addlayertomap(); movecameratokml(kmllayer);
when it's added run movecameratokml
function try lat, lng of every point crash on row foreach (latlng latlng in ((kmllinestring)geo).geometryobject);
errormessage: object reference not set instance of object
void movecameratokml(kmllayer kmllayer) { //retrieve first container in kml layer var container = (kmlcontainer)kmllayer.containers.iterator().next(); //retrieve nested container within first container container = (kmlcontainer)container.containers.iterator().next(); //retrieve first placemark in nested container var placemark = (kmlplacemark)container.placemarks.iterator().next(); var geo = placemark.geometry; if (geo kmllinestring) { foreach (latlng latlng in ((kmllinestring)geo).geometryobject) //object reference not set instance of object { system.diagnostics.debug.writeline(latlng); } } }
any idea why giving me crash? following example of nuget downloaded above.
the idea store lat, lngs in list , use polylineoptions
create routes.
that google sample (movecameratokml
) assumes using campus kml example. since kml use unique app, need review kml/xml elements , write code suit usage.
here example using grand canyon kml linestring
hiking path:
so looking @ kml, need to:
- get first
container
- get first
placemark
in container - check if has geometry , kml
linestring
- obtain
latlng
array viageometryjavaobject()
- use latlngs build camera viewport , move it.
- "drive" camera along individual points
grand canyon kmllinestring example:
void movecameratokml(kmllayer kmllayer) { var container = (kmlcontainer)kmllayer.containers.iterator().next(); var placemark = (kmlplacemark)container.placemarks.iterator().next(); if (placemark.hasgeometry && placemark.geometry kmllinestring) { var linestring = placemark.geometry kmllinestring; var latlngarray = linestring.geometryjavaobject() java.util.arraylist; using (var builder = new latlngbounds.builder()) { foreach (latlng latlng in latlngarray.toenumerable()) { builder.include(latlng); } googlemap.movecamera(cameraupdatefactory.newlatlngbounds( builder.build(), mapfragment.view.width, mapfragment.view.height, 1) ); } task.run(async () => // run camera along kmllinestring { foreach (latlng latlng in latlngarray.toenumerable()) { await task.delay(2000); runonuithread(() => googlemap.movecamera(cameraupdatefactory.newlatlng(latlng))); } }); } }
Comments
Post a Comment