<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0, maximum-scale=3.0" />
<div id="map" style="border:1px solid #0000FF; width:400px; height:300px;"></div>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
var map, infoWindow;
if (!navigator.geolocation) {
var map = document.getElementById("map");
map.innerHTML = "このブラウザは位置情報をサポートしていません。";
} else {
navigator.geolocation.getCurrentPosition(showMap2, showError2,
{
enableHighAccuracy:true,
maximumAge:0,
timeout:10000
});
}
function showMap2(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
// 取得して現在位置をGoogle Maps APIに渡す
var mapCenter = new google.maps.LatLng(latitude, longitude);
var mapDiv = document.getElementById("map");
var op = {
zoom:16, // 表示倍率を指定
center:mapCenter, // 中央位置を指定
mapTypeId:google.maps.MapTypeId.ROADMAP,
scaleControl:true // 倍率変更を指定
};
// 地図を表示
map = new google.maps.Map(mapDiv, op);
// マーカーを表示
var marker = new google.maps.Marker({
position:mapCenter,
map:map,
title:"現在地"});
map.setCenter(mapCenter);
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': mapCenter}, getGoogleMapAddress);
infoWindow = new google.maps.InfoWindow();
}
function showError2(error) {
var l = document.getElementById("map");
switch(error.code) {
case error.UNKNOWN_ERROR:
l.innerHTML = "UNKNOWN_ERROR:" + error.message;
break;
case error.PERMISSION_DENIED:
l.innerHTML = "PERMISSION_DENIED:";
break;
case error.POSITION_UNAVAILABLE:
l.innerHTML = "POSITION_UNAVAILABLE:";
break;
case error.TIMEOUT:
l.innerHTML = "TIMEOUT:";
break;
}
}
function getGoogleMapAddress(results, status){
var address;
if (status == google.maps.GeocoderStatus.OK) {
address = results[0].formatted_address;
infoWindow.setPosition(results[0].geometry.location);
infoWindow.setContent(address);
infoWindow.open(map);
} else {
m = document.getElementById("map");
m.innerHTML = "エラーが発生しました。" + status;
}
}
</script>
0 件のコメント:
コメントを投稿