I want to display downloaded layers on the map. The map which I am using is also an offline Esri map. The problem is that when I am adding layers on the map using myMap.OperationalLayers.Add();. I am getting an Exception i.e Object already owned.
Code:
if (featureLayerDict.Any())
{
// Remove the previously applied layers
if (myMap.OperationalLayers != null)
{
myMap.OperationalLayers.Clear();
}
// Select the LayerIds, which are based on if Parent Layer is selected
var selectedMapLayerIds = mapLayers.Select(l => l.ID).ToList();
foreach (var dictItem in featureLayerDict)
{
try
{
// Add the layer to maps operational layer only if its parent layer is selected and if its present in the userlayers
if (userLayers.Contains(dictItem.Key) && selectedMapLayerIds.Contains(dictItem.Key))
{
myMap.OperationalLayers.Add(dictItem.Value);
}
}
catch (Exception ex)
{
continue;
}
}
}
A FeatureLayer can only be owned by one map at the same time.
So after close the first map, you have to make null the references to their layers or just do:
myMap.OperationalLayers.Clear();
at the beginning of the method that adds the layers to the map.
Related
I have displayed a Line chart using fl_chart ^0.10.1 (Tested on Flutter ver 52.2.1 & 53.0.1).
I am updating the FlSpots from a csv file.
I am trying to get the index or Spot object if I touch somewhere in the plot.
But I am getting an empty list from the touchResponse if I touch (both tap or long-press) somewhere.
But I am getting the value in the plot itself. I am assuming it's there because of the built-in touch handler.
The code I am trying it:
lineTouchData: LineTouchData(
touchCallback: (LineTouchResponse touchResponse) {
if (touchResponse.touchInput is FlPanEnd ||
touchResponse.touchInput is FlLongPressEnd) {
var spots = touchResponse.lineBarSpots;
setState(() {
var lengthList = spots.length;
print(
'touched spot---> ${spots.toString()},,,len--->$lengthList');
});
} else {
print('wait');
}
}),
My end goal is to use and pass the touched index to open a new screen/widget.
touchCallback: (LineTouchResponse? lineTouch) {
final value = lineTouch.lineBarSpots![0].x;
// set state...
});
It's in the demo by fl_chart in github.
https://github.com/imaNNeoFighT/fl_chart/blob/master/example/lib/line_chart/samples/line_chart_sample3.dart
my development team and I have run into an issue on our Android distribution of our Xamarin project. The issue is as such: The application uses an observable collection of objects and represents these objects in the form of a list view and a map view with pins representing the objects. In the map view, our code is designed to subscribe to a messaging center call that periodically updates the observable collection of objects from our API (other part of project). The issue we are having is that when we call PlotPins method in the messaging center code block, the application should first retrieve the updated list and then access that list to plot pins on the map. Every time an update is received, the application will clear all pins from the map and then replot the pins based on the updated list (inefficient we know, but this is a temporary solution). However, the pins are never updated. Through the use of the debugger we have discovered that once map.Pins.Clear() within PlotPins() is called, the application jumps to the end of the RequestUpdatedListAsync method (which occurs periodically to retrieve the updated list and which triggers the Messaging Center) and then halts.
Our solution works for our GTK build, with the pins being cleared and redrawn on the map as intended, so this seems to be an Android specific issue.
Any help would be appreciated, thank you.
Relevant code located below:
MESSAGING CENTER:
MessagingCenter.Subscribe<object, ObservableCollection<MyObject>>(Application.Current, Constants.ListUpdateContract, (sender, newList) =>
{
//update list
listUpdater.UpdateList(newList);
//call method to plot pins again
PlotPins(map);
});
PLOTPINS:
private void PlotPins(Map map)
{
map.Pins.Clear();
foreach (MyObject in MyObjects)
{
var pin = new Pin
{
Label = MyObject.ID,
Address = "Latitude: " + MyObject.Latitude + " " + "Longitude: " + MyObject.Longitude,
Type = PinType.Place,
Position = new Position(Convert.ToDouble(MyObject.Latitude), Convert.ToDouble(MyObject.Longitude))
};
//event handler for when user clicks on pin's info window
pin.InfoWindowClicked += async (s, args) =>
{
//opens up detail page for pin associated with myObject
await Navigation.PushAsync(new DetailPage(MyObject));
};
map.Pins.Add(pin);
}
}
REQUEST UPDATED LIST ASYNC:
public static async System.Threading.Tasks.Task<bool> RequestUpdatedListAsync()
{
if (!_tokenIsGood)
return false;
var success = false;
var response = new HttpResponseMessage();
try
{
response = await _client.GetAsync(Constants. MyObjectDisplayUrl);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("Error requesting updated list.");
System.Diagnostics.Debug.WriteLine(e.Message);
System.Diagnostics.Debug.WriteLine(e.StackTrace);
return success;
}
response.EnsureSuccessStatusCode();
success = true;
var responseBody = await response.Content.ReadAsStringAsync();
// Update list
MyObjects.Clear();
MyObjects = JsonConvert.DeserializeObject<ObservableCollection< MyObject >>(responseBody);
//Alert subscribed ViewModels to update list
MessagingCenter.Send<object, ObservableCollection< MyObject >>(Application.Current, Constants.ListUpdateContract, units);
return success;
}
Since maps.Pins is UI related it has to be run in main UI thread.
MessagingCenter doesnt always publish/subscribe in main threads .
So to fix this issue call the maps.Pins.Clear() in main thread.
Device.BeginInvokeOnMainThread(()=> maps.Pins.Clear());
Credits: #shanranm for mentioning limitation of MessagingCenter for using main threads.
My goal is to create an Android app which download a map from ArcGIS portal when connected to internet, then use them offline. I would like to use service pattern, so later the app can have synchronization feature. I followed a tutorial from ArcGIS here.
I am currently stuck at downloading the map part. I expect the downloaded map is in mobile map package (.mmpk), but instead my download directory have a package.info file, and a folder of geodatabase and .mmap files as image shown here. Based on my understanding, I should have an .mmpk file to use them offline.
Following the tutorial steps, I am able to (1) create an offline map task, (2) specify the parameters, and (3) examine the offline capabilities. However in step (4) generate and download the offline map, I expect the downloaded map will be in mobile map package (.mmpk) but its not; as i mentioned above with image shown. In step (5) open and use the offline map, i am able to view offline map when using mobile map package (.mmpk) file that i transfer manually into the device. I also tried to open and use my downloaded (.mmap) file but no map showed up.
My full code by steps is shown below:
(1) create an offline map task
// Load map from a portal item
final Portal portal = new Portal("http://www.arcgis.com");
final PortalItem webmapItem = new PortalItem(portal, "acc027394bc84c2fb04d1ed317aac674");
// Create map and add it to the view
myMap = new ArcGISMap(webmapItem);
mMapView = (MapView) findViewById(R.id.mapView);
mMapView.setMap(myMap);
// Create task and set parameters
final OfflineMapTask offlineMapTask = new OfflineMapTask(myMap);
(2) specify the parameters
// Create default parameters
final ListenableFuture<GenerateOfflineMapParameters> parametersFuture = offlineMapTask.createDefaultGenerateOfflineMapParametersAsync(areaOfInterest);
parametersFuture.addDoneListener(new Runnable() {
#Override
public void run() {
try {
final GenerateOfflineMapParameters parameters = parametersFuture.get();
// Update the parameters if needed
// Limit maximum scale to 5000 but take all the scales above (use 0 as a MinScale)
parameters.setMaxScale(5000);
parameters.setIncludeBasemap(false);
// Set attachment options
parameters.setAttachmentSyncDirection(GenerateGeodatabaseParameters.AttachmentSyncDirection.UPLOAD);
parameters.setReturnLayerAttachmentOption(GenerateOfflineMapParameters.ReturnLayerAttachmentOption.EDITABLE_LAYERS);
// Request the table schema only (existing features won't be included)
parameters.setReturnSchemaOnlyForEditableLayers(true);
// Update the title to contain the region
parameters.getItemInfo().setTitle(parameters.getItemInfo().getTitle() + " (Central)");
// Create new item info
final OfflineMapItemInfo itemInfo = new OfflineMapItemInfo();
// Override thumbnail with the new image based on the extent
final ListenableFuture<Bitmap> exportImageFuture = mMapView.exportImageAsync();
exportImageFuture.addDoneListener(new Runnable() {
#Override
public void run() {
try {
Bitmap mapImage = exportImageFuture.get();
// Scale to thumbnail size
Bitmap thumbnailImage = Bitmap.createScaledBitmap(mapImage, 200, 133, false);
// Convert to byte[]
ByteArrayOutputStream stream = new ByteArrayOutputStream();
thumbnailImage.compress(Bitmap.CompressFormat.JPEG, 50, stream);
byte[] thumbnailBytes = stream.toByteArray();
stream.close();
// Set values to the itemInfo
itemInfo.setThumbnailData(thumbnailBytes);
itemInfo.setTitle("Water network (Central)");
itemInfo.setSnippet(webmapItem.getSnippet()); // Copy from the source map
itemInfo.setDescription(webmapItem.getDescription()); // Copy from the source map
itemInfo.setAccessInformation(webmapItem.getAccessInformation()); // Copy from the source map
itemInfo.getTags().add("Water network");
itemInfo.getTags().add("Data validation");
// Set metadata to parameters
parameters.setItemInfo(itemInfo);
} catch (Exception e) {
e.printStackTrace();
}
}
});
(3) examine the offline capabilities
final ListenableFuture<OfflineMapCapabilities> offlineMapCapabilitiesFuture =
offlineMapTask.getOfflineMapCapabilitiesAsync(parameters);
offlineMapCapabilitiesFuture.addDoneListener(new Runnable() {
#Override
public void run() {
try {
OfflineMapCapabilities offlineMapCapabilities = offlineMapCapabilitiesFuture.get();
if (offlineMapCapabilities.hasErrors()) {
// Handle possible errors with layers
for (java.util.Map.Entry<Layer, OfflineCapability> layerCapability :
offlineMapCapabilities.getLayerCapabilities().entrySet()) {
if (!layerCapability.getValue().isSupportsOffline()) {
showMessage(layerCapability.getKey().getName() + " cannot be taken offline.");
showMessage("Error : " + layerCapability.getValue().getError().getMessage());
}
}
// Handle possible errors with tables
for (java.util.Map.Entry<FeatureTable, OfflineCapability> tableCapability :
offlineMapCapabilities.getTableCapabilities().entrySet()) {
if (!tableCapability.getValue().isSupportsOffline()) {
showMessage(tableCapability.getKey().getTableName() + " cannot be taken offline.");
showMessage("Error : " + tableCapability.getValue().getError().getMessage());
}
}
} else {
// All layers and tables can be taken offline!
showMessage("All layers are good to go!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
(4) generate and download the offline map
String mExportPath = String.valueOf(getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS)) + File.separator + "New";
showMessage(mExportPath);
// Create and start a job to generate the offline map
final GenerateOfflineMapJob generateOfflineJob =
offlineMapTask.generateOfflineMap(parameters, mExportPath);
// Show that job started
final ProgressBar progressBarOffline = (ProgressBar) findViewById(R.id.progressBarOffline);
progressBarOffline.setVisibility(View.VISIBLE);
generateOfflineJob.start();
generateOfflineJob.addJobDoneListener(new Runnable() {
#Override
public void run() {
// Generate the offline map and download it
GenerateOfflineMapResult result = generateOfflineJob.getResult();
if (!result.hasErrors()) {
showMessage("no error");
mobileMapPackage = result.getMobileMapPackage();
// Job is finished and all content was generated
showMessage("Map " + mobileMapPackage.getItem().getTitle() +
" saved to " + mobileMapPackage.getPath());
// Show offline map in a MapView
mMapView.setMap(result.getOfflineMap());
// Show that job completed
progressBarOffline.setVisibility(View.INVISIBLE);
} else {
showMessage("error");
// Job is finished but some of the layers/tables had errors
if (result.getLayerErrors().size() > 0) {
for (java.util.Map.Entry<Layer, ArcGISRuntimeException> layerError : result.getLayerErrors().entrySet()) {
showMessage("Error occurred when taking " + layerError.getKey().getName() + " offline.");
showMessage("Error : " + layerError.getValue().getMessage());
}
}
if (result.getTableErrors().size() > 0) {
for (java.util.Map.Entry<FeatureTable, ArcGISRuntimeException> tableError : result.getTableErrors().entrySet()) {
showMessage("Error occurred when taking " + tableError.getKey().getTableName() + " offline.");
showMessage("Error : " + tableError.getValue().getMessage());
}
}
// Show that job completed
progressBarOffline.setVisibility(View.INVISIBLE);
}
}
});
(5) open and use the offline map
// Create the mobile map package
final MobileMapPackage mapPackage = new MobileMapPackage(mobileMapPackage.getPath());
// Load the mobile map package asynchronously
mapPackage.loadAsync();
// Add done listener which will invoke when mobile map package has loaded
mapPackage.addDoneLoadingListener(new Runnable() {
#Override
public void run() {
// Check load status and that the mobile map package has maps
if(mapPackage.getLoadStatus() == LoadStatus.LOADED && mapPackage.getMaps().size() > 0){
// Cdd the map from the mobile map package to the MapView
mMapView.setMap(mapPackage.getMaps().get(0));
}else{
// Log an issue if the mobile map package fails to load
showMessage(mapPackage.getLoadError().getMessage());
}
}
});
showMessage() in my code is showing Toast.
public void showMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
I worry if my .mmpk expectation is wrong, or my step goes wrong somewhere because I still not fully understand the whole process. This is my first time working with ArcGIS map in Android. I could not find much sample code to experiment, so really appreciate someone who could help.
Thank you!
The task created an exploded mobile map package, which works just the same as a .mmpk file. Open it like this:
final MobileMapPackage mapPackage =
new MobileMapPackage("/data/com.geoinfo.asmasyakirah.arcgis/files/Documents/New");
(If you can't access it there, you might want to generate the mobile map package in Environment.getExternalStorageDirectory() instead of Environment.DIRECTORY_DOCUMENTS.)
According to the documentation for the MobileMapPackage constructor:
Creates a new MobileMapPackage from the .mmpk file or exploded mobile map package at the given path.
If you really must have it as a .mmpk file, simply zip it using an Android API for making zip files and name it .mmpk instead of .zip.
Kinda late on the topic but i had several days working on this and found out something that may help some of you :
I created my mapData via this class : https://github.com/Esri/arcgis-runtime-samples-java/blob/master/src/main/java/com/esri/samples/map/generate_offline_map/GenerateOfflineMapSample.java
As you can see it creates a folder containing package.info + p13 (in which you find geodatabase file + mmap file)
WHen i tried offline to load this data, no errors appeared but the layer was empty and i could just see the carroying.
In fact after much more tries, i had to check that besides geodatabase and mmap file i could find a .tpk file (TilePackaged)
This one was never available (somehow due to network issues during the online download) and nothing alerted me.
Now that this tpk file is there, all items are clearly displayed like 'water network'
TL;DR; : check that tpk file is donwloaded during the online preparation.
I'm using this library to show a kml markers in a map in Android Maps V2.
I´m trying to get the lat and lon from a kml layer to do zoom directly to a placemark after add it to a map.
I tried to do this:
layer = new KmlLayer(mMap,R.raw.ruta, this );
layer.addLayerToMap();
for (KmlPlacemark act : layer.getPlacemarks()){
System.out.println("hi");//not iterate
}
but it doesn´t enter in the loop I read this kml-feature but don't work as is
You will have to read the 1st container of the KML-layer before attempting to read information about the placemarks:
KmlContainer = layer.getContainers().iterator().next();
if (kmlContainer == null) return;
for (KmlPlacemarks placemark : kmlContainer.getPlacemarks()) {
// Do the placemark magic here!
}
I'm developing an application with the MapView and the GPS.
My problem is that the user might disconnect while using my application, so he'll be walking in a fully grey map. I'd like to cache the map temporarily in order to see it later.
I know that cache the map isn't allowed by the google maps API, but I want to be sure if TEMPORARILY cache it (and delete it when the app is closed) is also forbidden.
I've heard about openstreetmaps / osmdroid, but I'd like to confirm that I HAVE to use it before deleting half of my code.
I know that cache the map isn't allowed by the google maps API, but I want to be sure if TEMPORARILY cache it (and delete it when the app is closed) is also forbidden.
There is no means of accomplishing this with the Google Maps Add-On for Android.
Because I need the satelliteView, I finally used a method that could be improved.
I start a thread that asks the controler to go to a location near the user. It's downloaded and the cache is managed by the MapView:
int latitudeSpan = mapView.getLatitudeSpan();
int longitudeSpan = mapView.getLongitudeSpan();
for(int i=-MEMORIZED_MAP_SIZE;i<=MEMORIZED_MAP_SIZE;i++)
{
for(int j=-MEMORIZED_MAP_SIZE;j<=MEMORIZED_MAP_SIZE;j++)
{
synchronized (this)
{
if(i==j&&j==0) j++;
Message msg = new Message();
msg.arg1=latitude+latitudeSpan*i;
msg.arg2=longitude+longitudeSpan*j;
msg.setTarget(MainActivity.handler);
msg.sendToTarget();
try
{
wait(5000);
}
catch(Exception e){}
}
}
}
Now, I just need to find another way than "wait(5000)" to know that the part of the map I'm looking at is downloaded. I'll modify this message if I find one.
Do you have the whole example of this?
int latitudeSpan = mapView.getLatitudeSpan();
int longitudeSpan = mapView.getLongitudeSpan();
for(int i=-MEMORIZED_MAP_SIZE;i<=MEMORIZED_MAP_SIZE;i++)
{
for(int j=-MEMORIZED_MAP_SIZE;j<=MEMORIZED_MAP_SIZE;j++)
{
synchronized (this)
{
if(i==j&&j==0) j++;
Message msg = new Message();
msg.arg1=latitude+latitudeSpan*i;
msg.arg2=longitude+longitudeSpan*j;
msg.setTarget(MainActivity.handler);
msg.sendToTarget();
try
{
wait(5000);
}
catch(Exception e){}
}
}
}
I managed to solve this partially using 2 MapView Fragments , one of them invisible.
Then a loop was moving the camera to create cache for the other MapView