2011-12-15 6 views
1

지도를 현재 위치의 중심에 배치 할 수 있습니까?Google지도의 현재 위치 중심

나는 이것을 꽤 오랫동안 해보려고 노력해 왔지만 작동하는 해결책을 찾지 못했습니다.

+0

현재 위치 *는 * 지도의 중심. 더 구체적으로 말하십시오. 핀 또는 'LatLng'을 중심으로 사용 하시겠습니까? – zzzzBov

+0

[google maps javascript api] (http://code.google.com/apis/maps/documentation/javascript/reference.html#Map)는 매우 간단합니다.'map.setCenter (latLng); – zzzzBov

답변

1

당신은 자바 스크립트와 HTML5 지오 API를 사용하여이 작업을 수행 할 수 있습니다 구글 지오 코딩 API (https://developers.google.com/maps/documentation/geocoding)를 사용하여

<!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

, 당신은 같은 것을 할 수 있습니다

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); 
}