The following is a basic example of using ASP.MVC to display a number of hotels on a Google map.
Domain Object - Hotel:
public class Hotel
{
public string Name { get; set; }
public double Longitude { get; set; }
public double Latitude { get; set; }
}
You will need a repository to get some of the hotel facilities. Use this in the Home Controller in the HotelsForMap () method:
public ActionResult HotelsForMap()
{
var hotels= new HotelRepository().GetHotels();
return Json(hotels);
}
google. GoogleMap. :
google api
<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA" type="text/javascript"></script>
jQuery, JSON
$().ready(() { if (GBrowserIsCompatible()) { $.getJSON( "/Home/HotelsForMap", ); }
});
jQuery
initialize (mapData) {
var map = new GMap2(document.getElementById("map_canvas"));
map.addControl(new google.maps.SmallMapControl());
map.addControl(new google.maps.MapTypeControl());
var zoom = mapData.Zoom;
map.setCenter(new GLatLng(mapData[0].Latitude, mapData[0].Longitude), 8);
$.each(mapData, function(i, Hotel) {
setupLocationMarker(map, Hotel);
});
}
jQuery
setupLocationMarker (, ) { var latlng = new GLatLng (Hotel.Latitude, Hotel.Longitude); var marker = GMarker (latlng); Map.addOverlay(); }
, , . div map_canvas, . :
<h2>Hotels</h2>
<br />
<div id="map_canvas" style="width: 500; height: 500px">
<% Html.RenderPartial("GoogleMap"); %>
</div>
, , ASP.MVC.