1

使用 Javascript 的 Google 地理定位不适用于 Firefox、Chrome 等主要浏览器的实时站点。地理位置结果显示在实时站点的 Opera 中,与所有浏览器(Chrome、firefox 和 Opera)的 localhost 一样。我的 Live 站点协议使用 https://。请参阅下面的代码并提出任何建议。地理位置和 Google Maps API

<script src="http://maps.google.com/maps/api/js?key=MY_KEY_HERE&sensor=true"></script>

<script src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script>

function writeAddressName(latLng) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
"location": latLng
},
function(results, status) {
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_2") {
//this is the object you are looking for
city= results[0].address_components[i];
break;
}
}
}
//alert(city.short_name + " " + city.long_name);
//alert(city.long_name);
var cityname = city.long_name;
if (status == google.maps.GeocoderStatus.OK) {
document.getElementById("address").innerHTML = results[0].formatted_address;
document.getElementById("citynm").innerHTML = cityname;  }
else {
document.getElementById("error").innerHTML += "Unable to retrieve your address" + "<br>"; }
});
}
function geolocationSuccess(position) {
var userLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
// Write the formatted address
writeAddressName(userLatLng);

var myOptions = {
zoom : 16,
center : userLatLng,
mapTypeId : google.maps.MapTypeId.ROADMAP
};
// Draw the map
var mapObject = new google.maps.Map(document.getElementById("map"), myOptions);
// Place the marker
new google.maps.Marker({
map: mapObject,
position: userLatLng
});
// Draw a circle around the user position to have an idea of the current localization accuracy
var circle = new google.maps.Circle({
center: userLatLng,
radius: position.coords.accuracy,
map: mapObject,
fillColor: '#0000FF',
fillOpacity: 0.5,
strokeColor: '#0000FF',
strokeOpacity: 1.0
});
mapObject.fitBounds(circle.getBounds());
}

function geolocationError(positionError) {
document.getElementById("error").innerHTML += "Error: " + positionError.message + "<br>";
}

function geolocateUser() {
// If the browser supports the Geolocation API
if (navigator.geolocation)
{
var positionOptions = {
enableHighAccuracy: true,
timeout: 10 * 1000 // 10 seconds
};
navigator.geolocation.getCurrentPosition(geolocationSuccess, geolocationError, positionOptions);
}
else
document.getElementById("error").innerHTML += "Your browser doesn't support the Geolocation API";
}
window.onload = geolocateUser;
</script>
</head>
<body>
<p id="citynm"></p>
<p id="address"></p>
</body>
</html>
4

3 回答 3

1

我已经测试了你的代码(Chrome v29,Windows 8),下面的代码可以在远程服务器(非本地主机)上运行,但你需要注意注意 2 区域

<script src="http://maps.google.com/maps/api/js?v=3.9&sensor=true"></script>

我添加了 v=3.9

function writeAddressName(latLng) {
    ...
    if (results[0].address_components[i].types[b] == "route") {

您可能需要处理无法设置城市的情况。

我可以看到地址。[本示例不包括地图展示]

<!DOCTYPE html>
<html>
<head>

<script src="http://maps.google.com/maps/api/js?v=3.9&sensor=true"></script>
<script>
function writeAddressName(latLng) {
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ "location": latLng }, function(results, status) {
        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] == "route") {
                //this is the object you are looking for
                    city = results[0].address_components[i];
                    break;
                }
            }
        }
//alert(city.short_name + " " + city.long_name);
//alert(city.long_name);
        var cityname = city.long_name;
        if (status == google.maps.GeocoderStatus.OK) {
            document.getElementById("address").innerHTML = results[0].formatted_address;
            document.getElementById("citynm").innerHTML = cityname;
        }
        else {
            document.getElementById("error").innerHTML += "Unable to retrieve your address" + "<br>";
        }
    });
}

function geolocationSuccess(position) {
    var userLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
// Write the formatted address
    writeAddressName(userLatLng);

    var myOptions = {
        zoom : 16,
        center : userLatLng,
        mapTypeId : google.maps.MapTypeId.ROADMAP
    };
    // Draw the map
    var mapObject = new google.maps.Map(document.getElementById("map"), myOptions);
    // Place the marker
    new google.maps.Marker({
        map: mapObject,
        position: userLatLng
    });
// Draw a circle around the user position to have an idea of the current localization accuracy
    var circle = new google.maps.Circle({
        center: userLatLng,
        radius: position.coords.accuracy,
        map: mapObject,
        fillColor: '#0000FF',
        fillOpacity: 0.5,
        strokeColor: '#0000FF',
        strokeOpacity: 1.0
    });
    mapObject.fitBounds(circle.getBounds());
}

function geolocationError(positionError) {
    document.getElementById("error").innerHTML += "Error: " + positionError.message + "<br>";
}

function geolocateUser() {
    // If the browser supports the Geolocation API
    if (navigator.geolocation) {
        var positionOptions = {
            enableHighAccuracy: true,
            timeout: 10 * 1000 // 10 seconds
        };
        navigator.geolocation.getCurrentPosition(geolocationSuccess, geolocationError, positionOptions);
    }
    else
    document.getElementById("error").innerHTML += "Your browser doesn't support the Geolocation API";
}
window.onload = geolocateUser;
</script>
</head>
<body>
<p id="error"></p>
<p id="citynm"></p>
<p id="address"></p>
</body>
</html>
于 2013-09-05T07:35:42.693 回答
0
https://developers.google.com/maps/faq?hl=en&csw=1#browsersupport



https://developers.google.com/maps/documentation/javascript/examples/map-geolocation

http://jsfiddle.net/BQzLq/3/

https://support.google.com/maps/answer/21849?hl=en

http://www.javascript-coder.com/window-popup/javascript-window-open.phtml

These all links is just for your knowledge in some browser or browser's older versions google map v3 doesn't works.
于 2013-09-05T07:02:33.603 回答
0

可能这个链接可以帮助你

https://developers.google.com/maps/articles/geocodestrat

google maps api 在本地主机上工作,但在网络服务器上不起作用

于 2013-09-05T06:52:32.843 回答