Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Google Maps with a dynamic form
    primarykey
    data
    text
    <p>I use an MVC template to allow the user to add multiple addresses. They click a button and this calls my template using jquery and the form is genereated. If they want to add another location they click the button again and another form is setup and so on. This template works fine but I am having some trouble using Google Maps within these templates.</p> <p>The reason for using Google Maps in this scenario is to provide a nice interface for adding a longitude and latitude for the location you are adding. I'm using class here instead of id's as there could be any number of address areas on the screen.</p> <p>In my mvc template I have the following markup:</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;div class="map" style="width: 450px; height: 350px;"&gt;&lt;br/&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="clear"&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="st-form-line"&gt; &lt;span class="st-labeltext"&gt;@Html.LabelFor(model =&gt; model.Address.Longitude)&lt;/span&gt; @Html.EditorFor(model =&gt; model.Address.Longitude) @Html.ValidationMessageFor(model =&gt; model.Address.Longitude) &lt;div class="clear"&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="st-form-line"&gt; &lt;span class="st-labeltext"&gt;@Html.LabelFor(model =&gt; model.Address.Latitude)&lt;/span&gt; @Html.EditorFor(model =&gt; model.Address.Latitude) @Html.ValidationMessageFor(model =&gt; model.Address.Latitude) &lt;div class="clear"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>I have the following jquery (this is the bit that's giving me an issue):</p> <pre><code>$('a.GetPosition').live("click", function (evt) { evt.preventDefault(); $(this).next().show(); var mapElement = $(this).closest('.body').find('.map'); var latElement = $(this).closest('.body').find('[id$=__Address_Latitude]'); var longElement = $(this).closest('.body').find('[id$=__Address_Longitude]'); showAddress(mapElement, latElement, longElement); }); // Google Map var map; function showAddress(mapElement, latElement, longElement) { var myLatlng = new google.maps.LatLng(40.713956, -74.006653); var myOptions = { zoom: 8, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP } map = new google.maps.Map(mapElement, myOptions); var marker = new google.maps.Marker({ draggable: true, position: myLatlng, map: map, title: "choose location" }); google.maps.event.addListener(marker, 'click', function (event) { latElement.val(event.latLng.lat().toFixed(5)); longElement.val(event.latLng.lng().toFixed(5)); }); } </code></pre> <p>For now I am just hard coding in the default map position of '40.713956, -74.006653' but I will change this to use a real world address once I can get the map to show correctly.</p> <p>I know the issue is down to the 'mapElement' but I'm not sure what is wrong. If I comment our the showAddress function call and output some text into mapElement I can see the text so it does appear to have found the right dom loction. </p> <p>Any ideas I could try here? Thanks for your help.</p> <p>Thanks,</p> <p>Rich</p>
    singulars
    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.
    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