views:

3549

answers:

5

I have an MVC app which is pretty simple so far but I want to add a driving directions page to the clients location. I see plenty of examples using the traditional code behind model but none with MVC. The app uses master pages and content pages. I'm pretty new to MVC so bear with me. Any exaples of doing this would be much appreciated.

PS - I'm not totally against integrating a standard code behind type page in the app if needs be.

Thanks in advanve.

Mike

+2  A: 

I'd probably look for examples that interact through Javascript rather than via .NET interface. Check out the Google Maps API documentation at http://code.google.com/apis/maps/documentation/index.html, examples at http://code.google.com/apis/maps/documentation/examples/index.html.

tvanfosson
+1  A: 

I've used google maps api for exactly the same need as you (driving directions, as well as calculations for fuel economy, etc.). What I'll normally do is pull what I need from the database in the back-end and structure it into a list of some type which I can then fill in a hidden control or literal. Then on the front end I'll have the javascript pick it apart and send off the requests to the maps API.

Don't know how much different it could potentially be with the MVC framework, but I would imagine not too much.

TheTXI
+4  A: 

I think tvanfosson is correct; you should look at Javascript examples. The fact that you're using MVC won't really affect your Google Maps code. Write the Google maps code in one of your view content pages just as you would in a standard ASP.NET app, or even just a plain HTML + javascript app. The only difference is that you will want to dynamically add javascript variables or parameters so your Google maps code can use it.

I think you should not put any of your logic in a code-behind file. It will keep your project cleaner to keep logic in the controller, and using a code-behind won't really help you here. Instead have your controller perform the logic & hand the results to the view in the ViewData.

Bryan
Thanks - played with jquery to get it to work within my content page and it works.
MikeD
+1  A: 

There is a blog post about using Google Maps with ASP.NET MVC here: Using Google Maps with the MVC Framework

edit: The solution doesn't work for me as is, probably due to being built with an older version of the MVC framework. In order to fix it I had to replace the following line in the project's Web.Config:

<add namespace="Microsoft.Web.Mvc"/>

with the following line:

<add namespace="System.Web.Mvc.Html" />
maayank
A: 

I wrote a small HtmlHelper for Google maps with MVC that should be able to help you.

Check it out: http://www.deanhume.com/Home/BlogPost/mvc-google-maps-htmlhelper---dynamic-maps/20 All you need to do is call it like so,

<= Html.DrawMap("your_api_key_goes_here","/Home/GetMarkers","550", "400")>

Deano