2011-12-15 71 views
1

他们有可能将地图居中在当前位置吗?谷歌地图中心位于当前位置

我一直在试图做相当长的一段时间,但我还没有遇到过有效的解决方案。

+0

您当前的位置*是*的地图的中心。请更具体一些。你的意思是集中在一个别针或`LatLng`上? – zzzzBov 2011-12-15 16:54:03

回答

1

,您可以用JavaScript和HTML5地理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> 
0

使用谷歌地理编码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); 
} 
相关问题