I have some icons as drawable resources that I would like to put in specific locations in a MapBox map in android studio, but I don´t know how.
I have tried converting my resource files into bitmaps, and then convert those bitmaps into strings so as to fill the "withIconImage" method of SymbolOptions class.(I know it works with defined strings such as "airport", "fire-station").
Can someone help me?
Thank you!
This example from the Mapbox Android documentation shows how to add a local drawable resource from an Android application to your Mapbox map as a SymbolLayer. The initSpaceStationSymbolLayer helper method specifically takes care of this:
private void initSpaceStationSymbolLayer(#NonNull Style style) {
style.addImage(
"space-station-icon-id",
BitmapFactory.decodeResource(this.getResources(), R.drawable.iss)
);
style.addSource(new GeoJsonSource("source-id"));
style.addLayer(new SymbolLayer("layer-id", "source-id").withProperties(
iconImage("space-station-icon-id"),
iconIgnorePlacement(true),
iconAllowOverlap(true),
iconSize(.7f)
));
}
You mentioned SymbolOptions, however, so it is likely the case that you are using the Mapbox Annotation Plugin for Android rather than directly adding SymbolLayers. As indicated in the documentation for the SymbolOptions#withIconImage method, icon images are specified as Strings which reference the names of images in your style's sprite sheet. This example from the Mapbox Android Plugins demo app demonstrates how to add an image from the resources folder to your style, to then be used as the icon image in a SymbolManager. Namely, ID_ICON_AIRPORT is defined as "airport" here, then the helper method addAirplaneImageToStyle here adds the relevant image to the style, and finally a Symbol is created here using SymbolOptions#withIconImage and ID_ICON_AIRPORT passed as the argument. You use this same approach for adding your own drawable image.
Related
I have added vector drawable in my app, and its working fine in activity.
ivTeam.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.ic_home_team))
but when i execute the same code from recycler adapter which is in a fragment, it crashes and gives following error message.
android.content.res.Resources$NotFoundException: File
res/drawable/test.xml from drawable resource ID #0x7f122e
I am passing activity reference to adapter, and also tried following static block.
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
but nothing seems to be working here, i am on androidX, and added following in my my gradle file.
vectorDrawables.useSupportLibrary = true
Gradle Version:
classpath 'com.android.tools.build:gradle:3.4.1'
if you are indeed using Android X I suspect you might not be loading your vector drawables correctly
I have read your question shortly after an article which states:
Load with AndroidX
When loading drawables you need to use methods from AndroidX which provide the backported vector support. The entry point for this is to always load drawables with AppCompatResources.getDrawable. While there are a number of ways to load drawables (because reasons), you must use AppCompatResources if you want to use compat vectors. If you fail to do this, then you won’t hook into the AndroidX code path and your app might crash when trying to use any features not supported by the platform you’re running on.
From your code it appears that you are using ContextCompat.getDrawable instead of AppCompatResources.getDrawable
Using AppCompatResources.getDrawable should correct your issue
I have some data points in a csv file located on my website (www.mysite.com/csv_report/myfile.csv) as below:
subject,report_by,Lat,Long
I want to show each of data points as independent markers on Mapbox. I found a solution in Mapbox samples about add single marker but nothing found about multiple markers.
Please help me to find the best way to solve my issue.
Thanks a lot
Zare
Your best joice would probably be to use the by Mapbox provides sources/layers functionality.
The source(s) contain your datapoints, where as the layers you can think of it like an style sheet for HTML. It containes information about how the datapoints are gonna look like.
For creating the layer, you have different options depending on how you want the markers to look like. If you want to show your own icon you have to use a SymbolLayer, but there are many more (CircleLayer for displaying points/circles, ...)
For creating the source use something like the following
style.addLayer(
new SymbolLayer(YOUR_LAYER_ID_STRING, YOUR_SOURCE_ID_STRING)
.withProperties(
// here you can define things like the icon
// icon color
// icon size
// ... many more. Have a look at the Property class
)
);
You can also style your items based on values of your items. Its called data driven styling in Mapbox. See my other answer to maybe get a bit more understanding.
For creating the source use something like the following
ArrayList<Feature> features = new ArrayList<>();
Iterate over your CSV items {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("subject_key", item.subjectValue);
jsonObject.addProperty("report_key", item.reportByValue);
features.add(Feature.fromGeometry(Point.fromLngLat(item.getLat(), item.getLon()), jsonObject);
}
FeatureCollection featureCollection = FeatureCollection.fromFeatures(features);
style.addSource(new GeoJsonSource(YOUR_SOURCE_ID_STRING, featureCollection, new GeoJsonOptions()));
You can furthermore have click listeners to catch clicks on the markers (https://docs.mapbox.com/android/maps/examples/click-on-a-single-layer/).
I have created an Android library project that i am linking with my Android app. In the drawable folder of the library project, I have added a few images. Now I am trying to access these images from a non-activity class in the same project but they are not being accessed when I try to access them using the following snippet
Drawable drawable=context.getResources().getDrawable(R.drawable.image);
The import of R file is correct and the image id is being generated correctly in the R file. The context is correct. I tried refreshing and cleaning/building the project multiple times but nothing seems to work.
Help!
[Note: Answer copied from here.]
Yes you can if you know the package name of your library.
Resources.getIdentifier(...)
You can do:
getResources().getIdentifier("res_name", "res_type", "com.library.package");
ex:
R.id.settings would be:
getResources().getIdentifier("settings", "id", "com.library.package");
I'm not sure I understand the quest completely (the problem you are having is that the method does not return a drawable?
I'm not sure you about how you are getting the context, but you could try this:
Drawable drawable = ContextCompat(getContext(), R.drawable.image);
If you are storing the context in a variable, you can pass the variable instead of using getContext().
Additionally, the method you are using to get the drawable is deprected, so you shouldn't be using it anymore.
Let me know if it helped you and remember to upvote/select this as correct answer if it did, cheers.
how do I get the default icon for the current user location to display it on the map? In the maps applications, it's a blue array... I'd like to have something like that, but I don't find it it android.R.drawable... How do I get this default user location icon?
Thanks a lot!
Here you can find the icons from Android 2.2, the current location icon is named ic_maps_indicator_current_position
Java Usage example: myMenuItem.setIcon(android.R.drawable.ic_menu_save);
Resource Usage example: android:icon="#android:drawable/ic_menu_save"
Note from the link above:
Some of the images in the Android jar are not public and therefore cannot be directly used (you can copy them to you own application, but can't reference them via the "android" package namespace). This project does not distinguish between what is public and what is not. If you try to use an image that is not public, you will get an error indicating that fact.
I have recently started developing a game in android, never used it before and was wondering if there is a simple way of getting a set of images loaded into the application. As my current implementation involves basically
Creating an int[] array,
Storing the values of each drawable into this array, (now this has to be hand coded, so if I add any more images it has to be added programmitically)
Then itterating through each item in the array and calling BitmapFactory to get the resource.
(Unfortunately I don't have the code with me as it is at home and I am at work, but that is the jist)
So 2 questions, is there a way of getting the drawables without having to put in each item manually to the int[] - ie looking for perhaps a file name prefix and then only loading the resource with the prefix?
Which leads me to my second question because I more than just these images in my drawable resource directory, is there a way to add extra organisation (folders) to manage the files better. As currently I have loads of images within the drawable file and how would I reference these sub folders/images.
You cannot have sub folders within the resources structure. Android depends on the folder layout to determine which resource to use in what condition (localization, different screen resolutions, etc).
I'm not sure why exactly you are trying to load up a whole bunch of images, but there are a couple of (slower) methods that allow you to look up a resource by string name. If you used a naming convention for your images you could look them up that way via [Resources.getIdentifier()][1]. However, in a game performance likely matters, so you are probably better off with a more manual approach using the int IDs directly since it is much more efficient.
[1]: http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier(java.lang.String, java.lang.String, java.lang.String)
I am uploading a load of images as they will be shown to user for different items. Its not a system where responsiveness is critical so its okay in terms of what I want. Though...
public int getIdentifier (String name, String defType, String defPackage)
Since: API Level 1
Return a resource identifier for the given resource name. A fully qualified resource name is of the form "package:type/entry". The first two components (package and type) are optional if defType and defPackage, respectively, are specified here.
Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
They suggest using the resource id but if I want to add a file later on then I have to re-compile the app to include the extra file, this is where it bugs me, as the pic gets associated to an item that I have in a string array. So I can add items to the array but not the images without a change of the code.
Surely there is a function to fix this?