MVC 4 and Google Maps API v3

前端 未结 4 689
再見小時候
再見小時候 2021-02-06 08:52

I\'m incorporating Google Maps into my MVC 4 application. Fairly straightforward to do. However, I have a question concerning the best way to add multiple markers dynamically. M

4条回答
  •  情话喂你
    2021-02-06 09:43

    Here are my thoughts on the subject.

    Recently, I did a project which was a single page, AJAX-driven web application (I know you are not looking for an AJAX based solution but stay with me).

    I integrated Google maps in that application that was used to show location of different entities.

    When the user searched for something, instead of doing Geo-Coding in the browser (which was, at times, very slow), I sent the request to server. On server, the incoming text was Geo-coded (using Google API) and result was cached in local DB for later use. If, the same request came again, I fetch the Geo-cordinates from the DB and send it back to client.

    The return type was pretty simple. i.e.

    public class Marker
    {
       public double Lat{set;get;}
       public double Lon{set;get;}
       public string Title{set;get;}
    }
    

    On client, I would simply iterate over the List and plot the markers on the map (plotting markers is faster than requesting Geo-coding).

    Now, the same can be done in post back. All you have to do is, in your initialize function call

    var markers = @Html.Action("GetMarkers")
    

    this will fill var markers with list of all your markers that you can then iterate.

    Remember, the return type of GetMarkers is

     public JsonResult GetMarkers()
     {
       ...
       // returns List
     }
    

提交回复
热议问题