Google Maps in asp.net MVC application

I would like to show google maps in my asp.net MVC application. I tried to use J M Elosegui's solution, but when I implement it in my View I get an error for:

<div style="height: 500px; border: solid 1px #cccccc">
    @(Html.GoogleMap()
          .Name("map")
          .Width((int)ViewData["width"])
          .Height((int)ViewData["height"]))
</div>

with the error code:

Error 4 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'GoogleMap' and no extension method 'GoogleMap' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

Upvotes: 2

Views: 2702

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59318

This error occurs since GoogleMap extension method could not be resolved.

Add the reference to Jmelosegui.Mvc.Googlemap namespace on top of your view:

@using Jmelosegui.Mvc.Googlemap

Another option (in order to avoid adding this using clause to all your Razor views) to add it to the <namespaces> section of your ~/Views/web.config file:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="Jmelosegui.Mvc.Googlemap"/>  
      </namespaces>
    </pages>
</system.web.webPages.razor>

Upvotes: 2

Related Questions