javascript - 更改 Google map API 语言

标签 javascript c# google-maps

我正在使用Azure平台开发一个网站。 我正在使用 Google map API 来获取我的位置。 服务器端是用C#编写的 我希望谷歌以英语而不是其他语言返回答案。 我怎样才能做到这一点?

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false" lang="en-us"></script> 
<script type="text/javascript">
    var geocoder;

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
    }
    //Get the latitude and the longitude;
    function successFunction(position) {
        var lat = position.coords.latitude;
        var lng = position.coords.longitude;
        codeLatLng(lat, lng)
    }

    function errorFunction() {
        alert("Geocoder failed");
    }

    function initialize() {
        geocoder = new google.maps.Geocoder();



    }

    function codeLatLng(lat, lng) {

        var latlng = new google.maps.LatLng(lat, lng);
        geocoder.geocode({ 'latLng': latlng }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                console.log(results)
                if (results[1]) {
                    //formatted address
                    //alert(results[0].formatted_address)
                    document.getElementById('<%= citylbl.ClientID %>').value = results[0].formatted_address;
                    //find country name
                    for (var i = 0; i < results[0].address_components.length; i++) {
                        for (var b = 0; b < results[0].address_components[i].types.length; b++) {

                            //there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate
                            if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
                                //this is the object you are looking for
                                city = results[0].address_components[i];
                                break;
                            }
                        }
                    }
                    //city data
                    //alert(city.short_name+" " + city.long_name);


                } else {
                    alert("No results found");
                }
            } else {
                alert("Geocoder failed due to: " + status);
            }
        });
    }
</script> 
</head>
<body onload="initialize()">
<asp:HiddenField ID="citylbl" runat="server" />
  </body>

最佳答案

您需要在 API 请求中添加语言参数。

例如:https://maps.googleapis.com/maps/api/js?language=ja 将返回日语信息。

您可以访问this page关于 Google map 语言本地化。

此外,list of supported languages .

关于javascript - 更改 Google map API 语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28062542/

相关文章:

javascript - 在以下情况下,如何让 Waypoints(Jquery 插件)为无限滚动工作?

javascript - 无法在 html 中获取范围 slider 的正确值

javascript - Angular 的编译不起作用

javascript - 在javascript中使用onclick-event和document.write时,是否总是需要在新窗口中打开

javascript - HTML 控件同时同步(相同或不同的浏览器)

c# - 在 C# (Plex Media Server) 中访问/更改视频文件(MKV 等)的元数据

javascript - 我的 google map api 脚本出了问题

android - 根据已安装的库从 MapActivity 或 Activity 扩展一个类

c# - Roslyn:检索父级或祖先 SyntaxNode 中的符号

java - 通过过滤显示或隐藏 HashMap 标记