Question

I am trying to generate a Google map for my events in Sitefinity. I am using an embedded Google iframe code and I am using <%# Eval("Street") %> and <%# Eval("City") %> and <%# Eval("State") %> to pull the location of my events from my Sitefinity events custom fields to output html.

So my code looks like this:

<iframe width='500' height='240' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' src='https://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=<%# Eval("Street") %>+<%# Eval("City") %>+<%# Eval("State") %>&amp;aq=0&amp;oq=<%# Eval("Street") %>+<%# Eval("City") %>+<%# Eval("State") %>&amp;ie=UTF8&amp;hq=&amp;hnear=<%# Eval("Street") %>+<%# Eval("City") %>+<%# Eval("State") %>&amp;z=14&amp;output=embed&iwloc=near'></iframe>

Now the map is getting generate for the events and everything is working fine except the marker on the map is not getting centered. What should I do to make the marker centered?

Was it helpful?

Solution

Below is a sample HTML with three maps. Make sure that the arguments from Sitefinity are returning what you expect by inspecting the HTML in Developer Tools. Also make sure that there are spaces or commas between Address, City and state. Also if you are using Sitefinity 6 they have Google Maps built in now.

   <html>
    <head></head>
    <body>
    <iframe width='500' height='240' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' src='https://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=510 W Main St Fort Wayne IN&amp;aq=0&amp;oq=510 W Main St Fort Wayne IN&amp;ie=UTF8&amp;hq=&amp;hnear=510 W Main St Fort Wayne IN&amp;z=14&amp;output=embed&iwloc=near'></iframe>
    <iframe width='500' height='240' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' src='https://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=1770 Apple Glen Blvd Fort Wayne IN&amp;aq=0&amp;oq=1770 Apple Glen Blvd Fort Wayne IN&amp;ie=UTF8&amp;hq=&amp;hnear=1770 Apple Glen Blvd Fort Wayne IN&amp;z=14&amp;output=embed&iwloc=near'></iframe>
    <iframe width='500' height='240' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' src='https://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=1600 Amphitheatre Pkwy, Mountain View, CA&amp;aq=0&amp;oq=1600 Amphitheatre Pkwy, Mountain View, CA&amp;ie=UTF8&amp;hq=&amp;hnear=1600 Amphitheatre Pkwy, Mountain View, CA&amp;z=14&amp;output=embed&iwloc=near'></iframe>
    </body>
    </html>

Using the Google Maps V3 API. Paste the javascript & map div inside the widget in sitefiniy, use your <%#Eval("address")%> as the string being passed to the GetLatLong() function

<html html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?key=YOURKEYHERE&sensor=false"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
    var geocoder = new google.maps.Geocoder();
    var marker;
    var map;
    $(document).ready(function(){
        initialize();
    });

    function initialize() {
          var mapOptions = {
            zoom: 10,
            center: new google.maps.LatLng(39.8282, -98.5795),//US Center as Default
            mapTypeId: google.maps.MapTypeId.ROADMAP
          };
          map = new google.maps.Map(document.getElementById('map-canvas'),
              mapOptions);
        GetLatLong("201 W Main St Fort Wayne IN");//Use your <%#Eval("Address")%> here Like
        //GetLatLong('<%#Eval("Street")%> <%#Eval("City")%> <%#Eval("State")%> ');
    }
    function GetLatLong(address){
        if (geocoder)
        {
            geocoder.geocode({ 'address': address }, function (results,status) {
                if (status == google.maps.GeocoderStatus.OK)
                {
                    var l = results[0].geometry.location;
                    map.setCenter(l);
                    var marker = new google.maps.Marker({
                            position: results[0].geometry.location,
                            map: map,
                            title: address
                            });
                }
                else
                {

                }
            });
        }
    }
</script>
</head>
<body>
    <div id="map-canvas" style="height:500px;width:500px;"></div>
</body>
</html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top