Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Google-Maps MVC3 Razor
Was hoping someone could point me in the right direction for a problem i cant seem to find the answer for - I have a googlemaps jquery inside an mvc view that plots a postcode on a map. I want to pass postcodes from my model and show multiple markers on the google map. All the examples i find are for cshtml and have a for each item in model loop to add the markers. This methodology doesnt seem to work in vbhtml ( Razor ). Attached is my view and the model just passes and Id key field and a postcode ( pcode) :
 
how do I pass my model.pcode in a loop to the 'var postcode' inside the javascript. I read that you can point the maps to a controller/action but when i tried this the view returns the data to the view as a long xml style string and doesnt plot the points???
 
Thanks in advance
 
Jase
 
@ModelType IEnumerable(Of Maps_Application.Tbl_Pcodes)
 
@Code
    ViewData("Title") = "Index"
End Code
<pre lang="text"><pre lang="text"><pre lang="HTML">
<!DOCTYPE html>
<html>
<head>
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
</head>
<body>
<div id="map_canvas" style="width: 600px; height: 800px;"></div>
</body>
</html>
 

 
<script type="text/javascript">
 
var postcode = "ln5 7nq";
 
</script>
 

 
<script type="text/javascript">
var geocoder;
var map;
 
$(document).ready(function () {
initializeGoogleMap();
codeAddress();
});
 
function initializeGoogleMap() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map($("#map_canvas")[0], myOptions);
}
 
function codeAddress() {
var address = postcode + ", UK";
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
 
</script>
Posted 31-May-13 0:16am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 31 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100