2013-01-03 2 views
0

그래서 geolocation을 사용하여 googleMap을 시도했습니다. 나는 코드를 붙여 넣기 만하면되지만지도를 나타나게 만들지는 않았다. 브라우저는 심지어 내 위치를 얻으라고하지만지도가 나타나지 않습니다. (오, 심지어는 Google API 키를 사용하여 호출했습니다.)geolocation이 표시되지 않는 googleMap

<!DOCTYPE html> 
<html lang="en-gb" dir="ltr"> 
<head> 
<style type="text/css"> 
<meta charset="utf-8"> 
<title>Geolocation API getCurrentPosition example</title> 
<style> 
<script src="http://maps.google.com/maps/api/js?key=AIzaSyDBy_moo4fAc2Z2DmSLBayqjry_VkthwyU&sensor=false"> 
<script type="text/javascript" src="http://maps.gstatic.com/intl/pt_pt/mapfiles/api-3/10/19/main.js"> 
<script type="text/javascript" charset="UTF-8" src="http://maps.gstatic.com/cat_js/intl/pt_pt/mapfiles/api-3/10/19/%7Bcommon,util,stats%7D.js"> 
<script type="text/javascript" charset="UTF-8" src="http://maps.gstatic.com/cat_js/intl/pt_pt/mapfiles/api-3/10/19/%7Bmap,marker,infowindow%7D.js"> 
<script type="text/javascript" charset="UTF-8" src="http://maps.gstatic.com/cat_js/intl/pt_pt/mapfiles/api-3/10/19/%7Bonion%7D.js"> 
<script type="text/javascript" charset="UTF-8" src="http://maps.gstatic.com/cat_js/intl/pt_pt/mapfiles/api-3/10/19/%7Bcontrols%7D.js"> 
</style> 
</head> 

<body> 
<p>Click on the marker for position information.</p> 
<div id="map" style="position: relative; background-color: rgb(229, 227, 223); overflow: hidden;"> 
<div style="position: absolute; left: 0px; top: 0px; overflow: hidden; width: 100%; height: 100%; z-index: 0;"> 
<div style="position: absolute; left: 0px; top: 0px; overflow: hidden; width: 100%; height: 100%; z-index: 0;"> 
<div style="position: absolute; left: 0px; top: 0px; z-index: 1; cursor: url("http://maps.gstatic.com/mapfiles/openhand_8_8.cur"), default;"> 
<div style="position: absolute; left: 0px; top: 0px; z-index: 200;"> 
<div style="position: absolute; left: 0px; top: 0px; z-index: 201;"> 
<div style="position: absolute; left: 0px; top: 0px; z-index: 202;"> 
<div style="position: absolute; left: 0px; top: 0px; z-index: 100;"> 
<div style="position: absolute; left: 0px; top: 0px; z-index: 0;"> 
</div> 
</div> 
<div style="margin: 2px 5px 2px 2px; z-index: 1000000; position: absolute; left: 0px; bottom: 0px;"> 
<div class="gmnoprint" style="z-index: 1000001; position: absolute; right: 0px; bottom: 0px;"> 
<div style="background-color: white; padding: 15px 21px; border: 1px solid rgb(171, 171, 171); font-family: Arial,sans-serif; color: rgb(34, 34, 34); box-shadow: 0px 4px 16px rgba(0, 0, 0, 0.2); z-index: 10000002; display: none; width: 256px; height: 148px; position: absolute; left: 475px; top: 310px;"> 
<div class="gmnoscreen" style="position: absolute; right: 0px; bottom: 0px;"> 
<div class="gmnoprint" style="display: none; font-size: 10px; height: 17px; background-color: rgb(245, 245, 245); border: 1px solid rgb(220, 220, 220); line-height: 19px; position: absolute; right: 0px; bottom: 0px;"> 
<div class="gmnoprint" style="margin: 5px; -moz-user-select: none; position: absolute; left: 0px; top: 0px;" controlwidth="78" controlheight="356"> 
<div class="gmnoprint" style="margin: 5px; z-index: 0; position: absolute; cursor: pointer; right: 0px; top: 0px;"> 
</div> 
</div> 
<script> 
if (navigator.geolocation) { 
var timeoutVal = 10 * 1000 * 1000; 
navigator.geolocation.getCurrentPosition(
displayPosition, 
displayError, 
{ enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 } 
); 
} 
else { 
alert("Geolocation is not supported by this browser"); 
} 
function displayPosition(position) { 
var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
var options = { 
zoom: 10, 
center: pos, 
mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 
var map = new google.maps.Map(document.getElementById("map"), options); 
var marker = new google.maps.Marker({ 
position: pos, 
map: map, 
title: "User location" 
}); 
var contentString = "<b>Timestamp:</b> " + parseTimestamp(position.timestamp) + "<br/><b>User location:</b> lat " + position.coords.latitude + ", long " + position.coords.longitude + ", accuracy " + position.coords.accuracy; 
var infowindow = new google.maps.InfoWindow({ 
content: contentString 
}); 
google.maps.event.addListener(marker, 'click', function() { 
infowindow.open(map,marker); 
}); 
} 
function displayError(error) { 
var errors = { 
1: 'Permission denied', 
2: 'Position unavailable', 
3: 'Request timeout' 
}; 
alert("Error: " + errors[error.code]); 
} 
function parseTimestamp(timestamp) { 
var d = new Date(timestamp); 
var day = d.getDate(); 
var month = d.getMonth() + 1; 
var year = d.getFullYear(); 
var hour = d.getHours(); 
var mins = d.getMinutes(); 
var secs = d.getSeconds(); 
var msec = d.getMilliseconds(); 
return day + "." + month + "." + year + " " + hour + ":" + mins + ":" + secs + "," + msec; 
} 
</script> 
<div id="YontooInstallID" style="display: none;">E5458842-7EDA-B28E-B9F2-4CFB1EEAD44D</div> 
<div id="Y2PluginIds" style="display: none;">Y2:E5458842-7EDA-B28E-B9F2-4CFB1EEAD44D</div> 
</body> 
</html> 
</style> 
</head> 
<body> 
<div id="YontooInstallID" style="display: none;">E5458842-7EDA-B28E-B9F2-4CFB1EEAD44D</div> 
<div id="Y2PluginIds" style="display: none;">Y2:E5458842-7EDA-B28E-B9F2-4CFB1EEAD44D</div> 
</body> 
</html> 
+0

'style' 태그 안에'meta''title'과'script' 태그를 넣으면 안됩니다. 이 문제를 해결하면 작동 할 수 있습니다. –

+0

태그를 가져 왔으며 아직 아무것도 표시하지 않았습니다. 실제로 더 나 빠졌다. : P – user1847226

답변

0

그리고 모든 태그 (스크립트, div, 스타일)를 닫아야합니다. 예 :

<script> with </script> 
+0

문제는 (그리고 실제로 코드를 붙여넣고 브라우저에서 시도하면) 첫 번째 메타 제목과 스크립트를 감싸는 초기 스타일을 취하면 페이지가 전혀 작동하지 않습니다. 나는 또한 당신이 방금 말한 것을 시도했는데 그것은 나를 위해 일하지 못했습니다. (코드를 복사 한 페이지 : http://html5doctor.com/demos/geolocation/geolocation-getcurrentposition-example.html) – user1847226

0

Your example에서 완벽하게 복사됩니다.

코드에는 시작과 끝에 약 20 줄의 추가 코드가 있습니다.

에 따르면 Google Google Maps JavaScript API v3에는 API 키가 올바르게 작동하지 않아도됩니다. 그러나 API의 Console 키를 사용하여 Maps API를로드하면 애플리케이션의 Maps API 사용을 모니터링 할 수 있습니다.

<!DOCTYPE html> 
<html dir="ltr" lang="en-gb"> 
<head> 
    <meta charset="utf-8" /> 
    <title>Geolocation API getCurrentPosition example</title> 
    <style> 
     #map { width:100%; height:800px; } 
    </style> 
    <script src="http://maps.google.com/maps/api/js?sensor=false"></script> 
</head> 
<body> 
    <p>Click on the marker for position information.</p> 
    <div id="map"></div> 
    <script> 
     if (navigator.geolocation) { 
      var timeoutVal = 10 * 1000 * 1000; 
      navigator.geolocation.getCurrentPosition(
       displayPosition, 
       displayError, 
       { enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 } 
      ); 
     } 
     else { 
      alert("Geolocation is not supported by this browser"); 
     } 
     function displayPosition(position) { 
      var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
      var options = { 
       zoom: 10, 
       center: pos, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 
      var map = new google.maps.Map(document.getElementById("map"), options); 
      var marker = new google.maps.Marker({ 
       position: pos, 
       map: map, 
       title: "User location" 
      }); 
      var contentString = "<b>Timestamp:</b> " + parseTimestamp(position.timestamp) + "<br/><b>User location:</b> lat " + position.coords.latitude + ", long " + position.coords.longitude + ", accuracy " + position.coords.accuracy; 
      var infowindow = new google.maps.InfoWindow({ 
       content: contentString 
      }); 
      google.maps.event.addListener(marker, 'click', function() { 
       infowindow.open(map,marker); 
      }); 
     } 
     function displayError(error) { 
      var errors = { 
       1: 'Permission denied', 
       2: 'Position unavailable', 
       3: 'Request timeout' 
      }; 
      alert("Error: " + errors[error.code]); 
     } 
     function parseTimestamp(timestamp) { 
      var d = new Date(timestamp); 
      var day = d.getDate(); 
      var month = d.getMonth() + 1; 
      var year = d.getFullYear(); 
      var hour = d.getHours(); 
      var mins = d.getMinutes(); 
      var secs = d.getSeconds(); 
      var msec = d.getMilliseconds(); 
      return day + "." + month + "." + year + " " + hour + ":" + mins + ":" + secs + "," + msec; 
     } 
    </script> 
</body> 
</html> 
관련 문제