LoginSignup
2
0

More than 5 years have passed since last update.

GoogleMapで表示されている地図の範囲を表示する

Posted at

画面に表示されている地図がどの範囲まで表示されているかを取得するサンプルです。

Google Maps JavaScript API v3を使っています。

画面サンプル

f:id:nakaji999:20141219043926p:plain

コードサンプル

google.maps.Map クラスの bounds_changed を使用します。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>表示されている地図の範囲を表示する</title>
    <script src="scripts/jquery-1.10.2.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script>
        $(function () {
            google.maps.event.addDomListener(window, 'load', initialize);
            function initialize() {
                var myLatlng = new google.maps.LatLng(33.839193, 132.765563);
                var mapOptions = {
                    zoom: 15,
                    center: myLatlng
                }
                var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

                google.maps.event.addListener(map, 'bounds_changed', function () {
                    $("#addressList").val(
                        "北東:" + map.getBounds().getNorthEast().lat() + "," + map.getBounds().getNorthEast().lng() + "\r\n" +
                        "南西:" + map.getBounds().getSouthWest().lat() + "," + map.getBounds().getSouthWest().lng()
                        );
                });
            }
        });
    </script>
</head>
<body>
    <h1>表示されている地図の範囲を表示する</h1>
    <textarea style="width: 100%; height: 50px;" id="addressList"></textarea>
    <div id="map-canvas" style="height: 250px;width: 500px;margin: 20px;padding: 20px;"></div>
</body>
</html>
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0