jmelosegui / GooglemapMvc

The control wraps Google maps API simplifying the use of Google maps in ASP.NET MVC applications.
MIT License
117 stars 61 forks source link

How to bind multiple layers from a model? #136

Closed Pugglewuggle closed 7 years ago

Pugglewuggle commented 7 years ago

Hi, is there any way to add multiple different layers of different types - polygons, polylines, points, etc. and style each layer differently when binding to a model? I have an IEnumerable of each different layer content and need to render these all on the map with different color and stroke, etc.

jmelosegui commented 7 years ago

What code have you tried? Could you show some example?

Anyway, this control should be able to fulfill your scenario.

Pugglewuggle commented 7 years ago

Hi, I haven't tried any code yet... I didn't see any example code to do this so I was curious if it could be done. Could you provide me an example of how to do this?

jmelosegui commented 7 years ago

I haven't tried that before, but I think if you repeat DataBinding code for each shape (Circle, Polygon, etc) it should work.

If you create a sample here in Github I could try to clone it and make it work.

Let me know if you need something else.

Pugglewuggle commented 7 years ago

Okay, I've got it working so far... the only thing I'm trying to figure out is how to add the points to a polygon and polyline when databinding... right now I'm getting the exception that the collection is read only. Here's the code I've got that errors out:

.BindTo<MyShape, Polygon>
(Model, mappings => mappings.For<MyShape>
(
    binding => binding.ItemDataBound
        ((polygon, obj) =>
        {
        polygon.Points
               .AddRange(obj.Points.Select(pnt => new Location(pnt.Latitude, pnt.Longitude)));
        polygon.StrokeColor = Color.Black;
        polygon.StrokeWeight = 2;
        })
    )
)

The polygon.Points.AddRange is what's giving me the problem.

jmelosegui commented 7 years ago

Could you try something like this (not tested)?

.BindTo<MyShape, Polygon>
(Model, mappings => mappings.For<MyShape>
(
    binding => binding.ItemDataBound
        ((polygon, obj) =>
        {
            foreach(var point in obj.Points.Select(pnt => new Location(pnt.Latitude, pnt.Longitude))
            {
                polygon.Points.AddPoint(point)
            }            
            polygon.StrokeColor = Color.Black;
            polygon.StrokeWeight = 2;
        })
    )
)

The Points property getter from the Polygon class returns a ReadOnlyCollection<Location>. So you will need to use the AddPoint(Location) method.

Pugglewuggle commented 7 years ago

That's exactly what I ended up doing. Thanks!

Pugglewuggle commented 7 years ago

Do you know if there's any way to programatically add more properties to the map outside of the initial map definition using the extension methods? For example, say I want to conditionally add certain things to the map in a switch statement in the view after the map has been defined.

jmelosegui commented 7 years ago

I want to conditionally add certain things

could you give me more details?

Pugglewuggle commented 7 years ago

Say I want to add more polygons or markers, or really anything that can be done through the various extension methods

jmelosegui commented 7 years ago

IMO, the logic behind adding map objects should happen inside the controller. Once you are in the view the only thing you should be doing is rendering those objects. The logic in the view should be kept at the minimum.

That being said, so log as you keep the reference to the map object you should be able to do it.

Something like this should work. (again not tested :wink: )

@{
    var map = Html.GoogleMap()
                 .Name("map");

    if(something)
    {
       map.Markers(marker => marker.Add()
                                .Title("Click to Open an InfoWindow")
                                .Latitude(23.140971656573335)
                                .Longitude(-82.39435493946075);
    }

    map.Render();
}

Please let me know if this works (I am really curious)

Pugglewuggle commented 7 years ago

Roger, thank you!