谷歌地图中心在当前位置

时间:2011-12-15 16:52:03

标签: asp.net google-maps

他们是否有可能将地图置于当前位置的中心位置?

我一直试图这样做很长一段时间,但我没有找到一个有效的解决方案。

2 个答案:

答案 0 :(得分:1)

您可以使用JavaScript和HTML5 Geo API执行此操作:

<!DOCTYPE HTML>
<html>
<head>
<script src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position){
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    var coords = new google.maps.LatLng(latitude, longitude);
    var mapOptions = {
        zoom: 15,
        center: coords,
        mapTypeControl: true,
        navigationControlOptions: {
            style: google.maps.NavigationControlStyle.SMALL
        },
        mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(
            document.getElementById("mapContainer"), mapOptions
            );
        var marker = new google.maps.Marker({
                position: coords,
                map: map,
                title: "Your current location!"
        });

    });
}else {
    alert("Geolocation API is not supported in your browser.");
}
</script>
<style type="text/css">
#mapContainer {
height: 500px;
width: 800px;
border:10px solid #eaeaea;
}
</style>
</head>
<body>
<div id="mapContainer"></div>
</body>
</html>

答案 1 :(得分:0)

使用Google GeoCode API(https://developers.google.com/maps/documentation/geocoding),您可以执行以下操作:

function initialize() {
   geocoder = new google.maps.Geocoder();
   var myOptions = {
                        zoom: 10,
                        center: new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude),
                        mapTypeId: 'terrain'
                   }
                   map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}