Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7665635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:37:20+00:00 2026-05-31T14:37:20+00:00

I use an MVC template to allow the user to add multiple addresses. They

  • 0

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.

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.

In my mvc template I have the following markup:

<div class="body">
           <div class="st-form-line">   
                <span class="st-labeltext">Location</span>
                <a href="#" class="GetPosition">Get Latitude &amp; Longitude positions based on above address details.</a>
                <div class="mapContainer" style="display:none;">
                    <p>If the map position is incorrect simply drag the pointer to the correct location.</p>
                    <div class="map" style="width: 450px; height: 350px;"><br/></div>
                </div>
                <div class="clear"></div>
            </div>

            <div class="st-form-line">  
                <span class="st-labeltext">@Html.LabelFor(model => model.Address.Longitude)</span>
                @Html.EditorFor(model => model.Address.Longitude)
                @Html.ValidationMessageFor(model => model.Address.Longitude)
                <div class="clear"></div>
            </div>

            <div class="st-form-line">  
                <span class="st-labeltext">@Html.LabelFor(model => model.Address.Latitude)</span>
                @Html.EditorFor(model => model.Address.Latitude)
                @Html.ValidationMessageFor(model => model.Address.Latitude)
                <div class="clear"></div>
            </div>
</div>

I have the following jquery (this is the bit that’s giving me an issue):

$('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));
        });
    }

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.

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.

Any ideas I could try here? Thanks for your help.

Thanks,

Rich

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:37:21+00:00Added an answer on May 31, 2026 at 2:37 pm

    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.

    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.

    My HTML looks like this:

        <div class="body">
            <div class="st-form-line">  
                       <span class="st-labeltext">Location</span>
                       <a href="#" class="GetPosition">Get Latitude &amp; Longitude positions based on above address details.</a>
                       <div class="mapContainer" style="display:none;">
                       <p>If the map position is incorrect simply drag the pointer to the correct location.</p>
                       <p>@Html.LabelFor(model => model.Address.Longitude) @Html.EditorFor(model => model.Address.Longitude) @Html.LabelFor(model => model.Address.Latitude) @Html.EditorFor(model => model.Address.Latitude) <br /> 
                       @Html.ValidationMessageFor(model => model.Address.Longitude) @Html.ValidationMessageFor(model => model.Address.Latitude)</p><br />
                       <div class="clear"></div>
                   </div>
             </div>
         </div>  
    

    The JavaScript looks like this:

    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++;
            $("<div></div>").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));
                   });
               }
           });
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the T4 templates in my MVC project. This screencast suggests
I use templated helpers in an ASP.NET MVC 3 project. One display template had
I'm starting to use DataAnnotations in ASP.NET MVC and strongly typed template helpers. Now
I started the site using AST.NET Razor template, not ASP.NET MVC template. I recall
I'd like to use ASP.NET MVC's views as mail template engine. For that, I
I use telerik mvc grid and pop up edit template. But I've no idea
Im trying switch the Visual Studio asp.net mvc web app template to use MySql
I'm using ASP.NET MVC 3 with the Razor template engine for my website. I
I would like to use the Mvc UrlHelper in conjunction with JQuery Templates. Example
Using the default ASP.NET MVC template, I cannot figure out how to increase the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.