none
Localizing the map RRS feed

  • Question

  • Hi,

    I am using Bing Map V7.0 API in ASP.NET (Framework 3.5) application. Localization of the map is one of the feature in my application.

    My question is that how can I set the culture of bing map as per the user's location.

     For example, the below given script set the culture for France.

    <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0&mkt=fr-fr"></script>

    But if the user belongs to Italy, how can I set mkt parameter at run time using Java script?

    Thanks


    Sharma M.


    • Edited by M_Sharma Wednesday, August 20, 2014 2:06 PM
    Wednesday, August 20, 2014 2:06 PM

Answers

  • First off you can find a list of the supported cultures for the Bing Maps V7 control here: http://msdn.microsoft.com/en-us/library/gg427600.aspx

    To change the culture you simply need to set the mkt parameter of the script reference for Bing Maps a valid culture code.

    Since you are using ASP.NET you can look at the page request header. In there you will find a property called "Accept-Language". Take a look at this blog post for more info on this: http://weblogs.asp.net/emilstoichev/tip-how-to-get-the-client-culture-from-an-asp-net-app Once you have the culture values you can either validate it against a list of supported culture codes, or try your luck with the culture code that you get. Bing Maps has a lot of undocumented cultures that aren't yet fully supported. In many of these cases the map labels will become localized while the navigation bar will be in English. If you pass in a culture code to the map that there is no data for the map automatically defaults to US English. Once you decide which culture code you want to use you can add the script reference to the page from code behind.

    Here is a code sample:

    Default.aspx.cs

    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    
    namespace LocalizedMaps
    {
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                string culture = "en-us";
                if (Request.UserLanguages.Length > 0)
                {
                    culture = Request.UserLanguages[0];
                } 
    
                var js = new HtmlGenericControl("script");
                js.Attributes["type"] = "text/javascript";
                js.Attributes["src"] = @"http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0&mkt=" + culture;
                Page.Header.Controls.Add(js);
            }
        }
    }

    Default.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="LocalizedMaps.Default" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    
        <script type="text/javascript">
    	var map, lastZoomLevel = 0;
    	
    	function GetMap()
    	{	
    		map = new Microsoft.Maps.Map(document.getElementById("myMap"), { 
    			credentials: "YOUR_BING_MAPS_KEY"
    		});
    	}
    	</script>
    </head>
    <body onload="GetMap();">
        <form id="form1" runat="server">
        <div>
            <div id='myMap' style=';width:800px;height:600px;'></div>
        </div>
        </form>
    </body>
    </html>


    http://rbrundritt.wordpress.com


    • Proposed as answer by Ricky_Brundritt Wednesday, August 20, 2014 7:02 PM
    • Edited by Ricky_Brundritt Thursday, August 21, 2014 9:59 AM
    • Marked as answer by M_Sharma Thursday, August 21, 2014 1:23 PM
    Wednesday, August 20, 2014 7:01 PM

All replies

  • First off you can find a list of the supported cultures for the Bing Maps V7 control here: http://msdn.microsoft.com/en-us/library/gg427600.aspx

    To change the culture you simply need to set the mkt parameter of the script reference for Bing Maps a valid culture code.

    Since you are using ASP.NET you can look at the page request header. In there you will find a property called "Accept-Language". Take a look at this blog post for more info on this: http://weblogs.asp.net/emilstoichev/tip-how-to-get-the-client-culture-from-an-asp-net-app Once you have the culture values you can either validate it against a list of supported culture codes, or try your luck with the culture code that you get. Bing Maps has a lot of undocumented cultures that aren't yet fully supported. In many of these cases the map labels will become localized while the navigation bar will be in English. If you pass in a culture code to the map that there is no data for the map automatically defaults to US English. Once you decide which culture code you want to use you can add the script reference to the page from code behind.

    Here is a code sample:

    Default.aspx.cs

    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    
    namespace LocalizedMaps
    {
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                string culture = "en-us";
                if (Request.UserLanguages.Length > 0)
                {
                    culture = Request.UserLanguages[0];
                } 
    
                var js = new HtmlGenericControl("script");
                js.Attributes["type"] = "text/javascript";
                js.Attributes["src"] = @"http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0&mkt=" + culture;
                Page.Header.Controls.Add(js);
            }
        }
    }

    Default.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="LocalizedMaps.Default" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    
        <script type="text/javascript">
    	var map, lastZoomLevel = 0;
    	
    	function GetMap()
    	{	
    		map = new Microsoft.Maps.Map(document.getElementById("myMap"), { 
    			credentials: "YOUR_BING_MAPS_KEY"
    		});
    	}
    	</script>
    </head>
    <body onload="GetMap();">
        <form id="form1" runat="server">
        <div>
            <div id='myMap' style=';width:800px;height:600px;'></div>
        </div>
        </form>
    </body>
    </html>


    http://rbrundritt.wordpress.com


    • Proposed as answer by Ricky_Brundritt Wednesday, August 20, 2014 7:02 PM
    • Edited by Ricky_Brundritt Thursday, August 21, 2014 9:59 AM
    • Marked as answer by M_Sharma Thursday, August 21, 2014 1:23 PM
    Wednesday, August 20, 2014 7:01 PM
  • Thanks Mr. Ricky :-)

    I will get back to you after testing my application with your suggested code. However, Your sample code works.


    Sharma M.



    • Edited by M_Sharma Thursday, August 21, 2014 4:59 AM
    Thursday, August 21, 2014 4:33 AM
  • Hi Mr ricky,

    Your solution worked for me.  However , there is one thing that I notice that when I Zoom In the state name shows in english regardless of what culture I set.

    I set the culture zh-TW (Chinese - Taiwan).   Is this the correct behaviour of map or am I missing anything?

    Thanks


    Sharma M.

    Thursday, August 21, 2014 1:34 PM
  • Some cultures only localize the data for the country in which they are used and not for the whole globe.

    http://rbrundritt.wordpress.com

    Thursday, August 21, 2014 1:51 PM