How to Cluster Xamarin.Forms.GoogleMaps Pin in PCL Project - xamarin

Since I am doing a map based cross-platform project on Xamarin, And I'm stuck to the point where I need to cluster markers when zooming out.
things I had tried:
GoogleMapUtility Project.
And tried to use ClusterManager of GoogleMapUtility through dependency. so that I can add a pin to cluster manager From PCL project. But to initialize cluster manager I need a Native Map instance i.e. Android.Gms.Maps.GoogleMap.(don't know how to get)
I know I can achieve it with that with custom map renderer, but I cant do that coz. a lot of code related to map is already written, I don't want to rewrite a Code. So Is there any possibility that I can get a native instance of the map that I can use on dependency service.
I created CustomRenderer For Map without creating a customMap class at PCL (inherit Map class) the code is below. but it's not get triggered. What wrong I am doing here...
custom renderer code:
[assembly: ExportRenderer(typeof(Map), typeof(MarkerClusterRenderer))]
namespace SamplingApp.Droid.CustomRenderers
{
public class MarkerClusterRenderer : MapRenderer
{
ClusterManager _clusterManager;
protected override void OnMarkerCreated(Pin outerItem, Marker innerItem)
{
base.OnMarkerCreated(outerItem, innerItem);
AddToMarkerCluster();
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
}
protected override void OnElementChanged(ElementChangedEventArgs<Map> e)
{
base.OnElementChanged(e);
}
public void AddToMarkerCluster()
{
_clusterManager = new ClusterManager(Android.App.Application.Context, NativeMap);
//[do other things here]
}
}
}
since I am is new to xamarin. little guidance is appreciated.
am I in the right direction?
Thank you in advance. and sorry for my English.

I created CustomRenderer For Map without creating a customMap class at PCL (inherit Map class) the code is below. but it's not get triggered. What wrong I am doing here...
If you want to customize the map, you need to create a custom class for map control in PCL:
public class CustomMap:Map
{
}
And use it in xaml:
<local:CustomMap WidthRequest="320" HeightRequest="200"
x:Name="MyMap"
IsShowingUser="true"
MapType="Hybrid"/>
And renderer:
[assembly:ExportRenderer(typeof(SamplingApp.CustomMap),
typeof(MarkerClusterRenderer))]
namespace SamplingApp.Droid.CustomRenderers
{
public class MarkerClusterRenderer:MapRenderer
{
...
Then, your renderer's codes will be triggered correctly.

Related

How can I launch a Android Activity from Xamarin Forms Page?

I know that i can launch a Xamarin Forms page from an Native Android Activity, but how do i start a Native Android Activity from Xamarin Forms ContentPage. I tried with DependencyService but without success.
I figured it out.
First i needed to create a Interface like this in Xamarin Project
public interface IRecordVideoPage
{
void StartNativeIntentOrActivity();
}
then i launched the Native Android Activity from my Xamarin Forms Content Page using Dependency Service like this:
DependencyService.Register<IRecordVideoPage>();
DependencyService.Get<IRecordVideoPage>().StartNativeIntentOrActivity();
next I created a RecordActivity class in my Android Project that loads the Test Activity
[assembly: Xamarin.Forms.Dependency(typeof(RecordActivity))]
namespace StreamTest.Droid
{
[Activity(Label = "RecordActivity")]
public class RecordActivity : IRecordVideoPage
{
public void StartNativeIntentOrActivity()
{
var intent = new Intent(Forms.Context, typeof(Test));
Forms.Context.StartActivity(intent);
}
}
finally I set the content view in the Test class:
public class Test : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public Test() { }
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Record);
}
}
}
DependencyService is your best option. The concept behind PCL (Portable Class Library) is exactly to be platform independet.
Maybe we can help if you show the error you are getting with Dependency Service

Custom PageRenderer or NavigationRenderer not working (virtual missing?)

Currently I'm working on a Win Phone 8.1 project and need some Custom Renderer for ContentPage and NavigationPages.
But when I tried to implement it i saw there is no possibility for overwrite the OnElementChanged Method. After some minutes with DotPeek I saw that those methods not are virtual like on other plattforms. So does anybody have an idea or a workaround for implementing custom rederers on Windows RT?
Tried on Xamarin.Forms: 2.0.0.6490 & 2.1.0.6529
Sincerely
You can use the Event ElementChanged.
public class MyRender: NavigationPageRenderer
{
public MyRender()
{
ElementChanged += OnElementChanged;
}
private void OnElementChanged(object sender, VisualElementChangedEventArgs visualElementChangedEventArgs)
{
// do some stuff here
}
}

Robospice Impossible to start SpiceManager as no service of class

I am trying to use following libraries to develop my app.
Robospice
Gson
and Spring for android
To do so, In my gradle file I have following dependencies added
compile 'com.octo.android.robospice:robospice-spring-android:1.4.13'
compile 'com.google.code.gson:gson:2.2.4'
And in manifest file, I have following lines inserted inside application tag.
<service
android:name="com.octo.android.robospice.GsonSpringAndroidSpiceService"
android:exported="false" />
Now, I created a base class that is used as Base Activity. And, I have did this in the following way:
public class BaseSpiceActivity extends Activity {
private SpiceManager spiceManager = new SpiceManager(GsonSpringAndroidSpiceService.class);
#Override
protected void onStart() {
spiceManager.start(this);
super.onStart();
}
#Override
protected void onStop() {
spiceManager.shouldStop();
super.onStop();
}
protected SpiceManager getSpiceManager() {
return spiceManager;
}
}
And then I used that base class to extend my own class where I had to use spice service request.
SimpleTextRequest text = new SimpleTextRequest(placeUrl);
getSpiceManager().execute(text, "place", DurationInMillis.ONE_MINUTE, new RequestListener<String>() {
//Other functions and codes
}
)
But when the above code gets executed, I am getting following error
E/AndroidRuntime﹕ FATAL EXCEPTION: SpiceManagerThread 0
java.lang.RuntimeException: Impossible to start SpiceManager as no service of class : com.octo.android.robospice.SpiceService is registered in AndroidManifest.xml file !
at com.octo.android.robospice.SpiceManager.checkServiceIsProperlyDeclaredInAndroidManifest(SpiceManager.java:1287)
at com.octo.android.robospice.SpiceManager.tryToStartService(SpiceManager.java:1168)
at com.octo.android.robospice.SpiceManager.run(SpiceManager.java:247)
at java.lang.Thread.run(Thread.java:856)
I have been trying to solve the issue from many hours. But unfortunately couldn't. Please help me out.
I am 100% sure you got messed somewhere. Search for new SpiceManager(SpiceService.class); in your code, and you will find out that you do use this service instead of the desired GsonSpringAndroidSpiceService.
You need to declare your service in your manifest.xml
Like this
<service android:name=".SampleRetrofitService"
android:exported="false"/>
Try to find out where using SpiceManager.
In my case, I named a activity SpiceActivity, and others extend it.
But robospice library also offer a activity named SpiceActivity. And it uses new SpiceManager(SpiceService.class)
So activity extends the wrong SpiceActivity which use SpiceService, then cause the error.

How to inject dependencies into the global.asax.cs

How do I inject dependencies into the global.asax.cs, i.e. the MvcApplication class?
Having previously used the Service Locator (anti-)pattern for dependency injection, I am trying to follow best practice advice in my latest MVC application by using an IOC container (specifically Unity.Mvc3 because it comes with an implementation of the IDependencyResolver out of the box) and constructor injection.
Everything seems quite straight forward so far except for a couple of snags, one of which is in the global.asax.cs (the other is for custom attributes but there's aleady a question on SO covering that).
The HttpApplication event handlers in the MvcApplication class such as:
Application_Start()
Application_EndRequest(object sender, EventArgs e)
Application_AcquireRequestState(object sender, EventArgs e)
may require external dependencies, e.g. a dependency on an ILogService. So how do I inject them without resorting to the service locator (anti-)pattern of e.g.
private static ILogService LogService
{
get
{
return DependencyResolver.Current.GetService<ILogService>();
}
}
Any help/advice greatly appreciated!
The class in your global.asax.cs is your Composition Root, so you can't (and shouldn't) inject anything into it from the outside.
However, there's only one instance of the MvcApplication class, so if you need a service in one of its methods, you can just declare it as a member field - e.g:
public class MvcApplication : System.Web.HttpApplication
{
private readonly ILogService log;
public MvcApplication()
{
this.log = new MyLogService();
}
protected void Application_Start()
{
// ...
this.log.Log("Application started");
}
}
You must use AutofacConfig.Resolve<T>() instead using DependencyResolver.Current.GetService<T>() to get your services without errors.

Need help converting PRISM Unity Module Init to PRISM MEF Module Init

I need help converting the following class for use in a program that I am developing. The original was a demo program from IdeaBlade called "PRISM EXPLORER" based on Unity. I need help converting one part from UNITY to MEF. I handled everything else. Just stuck on this one. I already marked my classes with the MEF "[EXPORT(typeof(XXX))]" and I think I need to use the "ComposeExportedValue" somehow. The confusing part is finding the equivelant for this line:
var provider =
(IEntityManagerProvider) _container.Resolve<IPersistenceGateway>();
_container.RegisterInstance<IEntityManagerProvider>(provider);
THANKS!
The following is the entire class I need to convert. You can find the original here: Ideablade PRISM Page
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using PrismExplorer.Infrastructure;
namespace ModelExplorer.Explorer {
public class ExplorerModule : IModule {
private readonly IUnityContainer _container;
public ExplorerModule(IUnityContainer container) {
_container = container;
}
public void Initialize() {
InitializeContainer();
SetViews();
}
// ToDo: Consider getting from configuration
private void InitializeContainer() {
RegisterGatewayAndEntityManagerProvider();
_container.RegisterType<IQueryRepository, QueryRepository>(
new ContainerControlledLifetimeManager()); // singleton
}
private void RegisterGatewayAndEntityManagerProvider() {
_container.RegisterType<IPersistenceGateway, PrismExplorerPersistenceGateway>(
new ContainerControlledLifetimeManager()); // singleton
var provider =
(IEntityManagerProvider) _container.Resolve<IPersistenceGateway>();
_container.RegisterInstance<IEntityManagerProvider>(provider);
}
private void SetViews() {
var regionManager = _container.Resolve<IRegionManager>();
var view = _container.Resolve<ExplorerView>();
regionManager.AddToRegion(RegionNames.MainRegion, view);
regionManager.RegisterViewWithRegion(RegionNames.MainRegion, typeof(ExplorerView));
}
// Destructor strictly to demonstrate when module is GC'd
//~MevModule() {
// System.Console.WriteLine("Goodbye, MevModule");
//}
}
}
The two corresponding methods on a CompositionContainer are ComposeExportedValue<T>(...), which allows you to add a specific instance to the container, and GetExportedValue<T>(...) which gets an instance of T from the container.
If you can design your types in a way to reduce this use of service location and try and prefer constructor injection, it will make your code much easier to maintain and test. E.g., could your code be transformed into:
[Export(typeof(IModule))]
public class ExplorerModule : IModule
{
[ImportingConstructor]
public ExplorerModule(IPersistenceGateway gateway)
{
}
}

Resources