Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So after doing some research it's clear that Google Maps only works if I use an element id. It does not work if I pass it a class.</p> <p>To work around this issue I had to dynamically add a div with a unique id when the button to show the map is clicked on.</p> <p>My HTML looks like this:</p> <pre><code> &lt;div class="body"&gt; &lt;div class="st-form-line"&gt; &lt;span class="st-labeltext"&gt;Location&lt;/span&gt; &lt;a href="#" class="GetPosition"&gt;Get Latitude &amp;amp; Longitude positions based on above address details.&lt;/a&gt; &lt;div class="mapContainer" style="display:none;"&gt; &lt;p&gt;If the map position is incorrect simply drag the pointer to the correct location.&lt;/p&gt; &lt;p&gt;@Html.LabelFor(model =&gt; model.Address.Longitude) @Html.EditorFor(model =&gt; model.Address.Longitude) @Html.LabelFor(model =&gt; model.Address.Latitude) @Html.EditorFor(model =&gt; model.Address.Latitude) &lt;br /&gt; @Html.ValidationMessageFor(model =&gt; model.Address.Longitude) @Html.ValidationMessageFor(model =&gt; model.Address.Latitude)&lt;/p&gt;&lt;br /&gt; &lt;div class="clear"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>The JavaScript looks like this:</p> <pre><code>var i = 1; $('a.GetPosition').live("click", function (evt) { evt.preventDefault(); // build up address string for map var address = //added my address textboxes here - removed for brevity; // find the map container and make it visible var mapContainer = $(this).next(); mapContainer.show(); // create a new map div with unique id var mapId = 'map' + i++; $("&lt;div&gt;&lt;/div&gt;").attr("id", mapId).attr("style", "width: 600px; height: 350px;").appendTo(mapContainer); // find the lat long textboxes and pass to the Google map function so we can populate with co-ordinates var latElement = $(this).closest('.body').find('[id$=__Address_Latitude]'); var longElement = $(this).closest('.body').find('[id$=__Address_Longitude]'); showAddress(mapId, address, latElement, longElement); // finally hide the button to avoid other map divs being generated $(this).hide(); }); var map; var marker; function showAddress(mapId, address, latElement, longElement) { var map = new google.maps.Map(document.getElementById(mapId), { mapTypeId: google.maps.MapTypeId.ROADMAP, zoom: 15 }); var geocoder = new google.maps.Geocoder(); geocoder.geocode({ 'address': address }, function (results, status) { if (status == google.maps.GeocoderStatus.OK) { marker = new google.maps.Marker({ draggable: true, position: results[0].geometry.location, map: map }); map.setCenter(results[0].geometry.location); latElement.val(results[0].geometry.location.lat().toFixed(5)); longElement.val(results[0].geometry.location.lng().toFixed(5)); google.maps.event.addListener(marker, 'dragend', function (event) { latElement.val(event.latLng.lat().toFixed(5)); longElement.val(event.latLng.lng().toFixed(5)); }); } }); } </code></pre> <p>Obviously there is still some fine tuning required with the above code as error checking needs to be added, etc but hopefully this will help some other people get started with getting Google Maps working with dynamic forms.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload