Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a marker icon in Google Maps API

I am using Xamarin and am using the SimpleMapDemo and I have a question about setting a MarkerOptions object to have a different icon.

Here is my current code:

marker1.Icon(SetFeatureDrawable('Icon.png'));

This above code is not working.

May I please have some help to set the icon to be the icon that is in the Drawable folder that has the name of 'Icon.png'?

Thanks in advance

EDIT

Here is my code:

marker1.Icon(BitmapDescriptorFactory.FromResource(Resource.Drawable.monkey));

This is the error I am getting:

Non-invocable member 'Android.Gms.Maps.Model.MarkerOptions.Icon' cannot be used like a method

Here is my code to try and set the icon:

marker1.Icon = BitmapDescriptorFactory.FromResource(Resource.Drawable.monkey);

This is the error I am getting:

CS0200: Property or indexer 'Android.Gms.Maps.Model.MarkerOptions.Icon' cannot be assigned to -- it is read only (CS0200)
like image 235
user22707 Avatar asked Mar 21 '23 01:03

user22707


2 Answers

Try this code instead:

marker1.InvokeIcon(SetFeatureDrawable('Icon.png'));

In general, Xamarin tries to bind the Java setter/getters to properties. In this case, although MarkerOption.icon looks like a property to C# eyes, it is in fact a Java method. In this case the binding has "translated" the property-like Java method to MarkerOption.InvokeIcon in an attempt to conform with C# design guidelines.

like image 63
Tom Opgenorth Avatar answered Mar 29 '23 04:03

Tom Opgenorth


Try in this way:

double latitude = 0.0;
double longitude = 0.0;

MarkerOptions mapMarker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Your title will be here");

mapMarker .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)));

mapView.addMarker(mapMarker );
like image 44
Satyaki Mukherjee Avatar answered Mar 29 '23 04:03

Satyaki Mukherjee