我想制作可点击的链接以缩放并在地图上显示说明

时间:2016-01-04 05:53:44

标签: google-maps google-maps-api-3

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 15pt;
        }
    </style>


    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
        var markers = [
    {
        "title": 'Aksa Beach',
        "lat": '19.1759668',
        "lng": '72.79504659999998',
        "description": 'Aksa Beach is a popular beach and a vacation spot in Aksa village at Malad, Mumbai.'
    },
    {
        "title": 'Juhu Beach',
        "lat": '19.0883595',
        "lng": '72.82652380000002',
        "description": 'Juhu Beach is one of favourite tourist attractions situated in Mumbai.'
    },
    {
        "title": 'Girgaum Beach',
        "lat": '18.9542149',
        "lng": '72.81203529999993',
        "description": 'Girgaum Beach commonly known as just Chaupati is one of the most famous public beaches in Mumbai.'
    },
    {
        "title": 'Jijamata Udyan',
        "lat": '18.979006',
        "lng": '72.83388300000001',
        "description": 'Jijamata Udyan is situated near Byculla station is famous as Mumbai (Bombay) Zoo.'
    },
    {
        "title": 'Sanjay Gandhi National Park',
        "lat": '19.2147067',
        "lng": '72.91062020000004',
        "description": 'Sanjay Gandhi National Park is a large protected area in the northern part of Mumbai city.'
    }
    ];
        var myArray    = new Array();

        var desc    = new Array();

        window.onload = function () {
            LoadMap();
            myFunction(myArray,desc);

        }

        function LoadMap() {
            var mapOptions = {
                center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
                zoom: 8,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var infoWindow = new google.maps.InfoWindow();
            var latlngbounds = new google.maps.LatLngBounds();
            var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);

            for (var i = 0; i < markers.length; i++) {

                var data = markers[i]
                var myLatlng = new google.maps.LatLng(data.lat, data.lng);
                var marker = new google.maps.Marker({
                    position: myLatlng,
                    map: map,
                    title: data.title
                });

                myArray[i]=data.title;
                desc[i]=data.description;

                (function (marker, data) {
                    google.maps.event.addListener(marker, "click", function (e) 
                    {

                        infoWindow.setContent("<div style = 'width:60px;min-height:40px'>" + data.description + "</div>");
                        infoWindow.open(map, marker);
                    });

                    marker.addListener('click', function() {
                        map.setZoom(18);
                        map.setCenter(marker.getPosition());
                      });
                })(marker, data);
                latlngbounds.extend(marker.position);    

            }

             var bounds = new google.maps.LatLngBounds();
            map.setCenter(latlngbounds.getCenter());
            map.fitBounds(latlngbounds);

        }



    </script>
    <script>
    function myFunction(myArray,desc) {
        var out = "";
        var i;
        for(i = 0; i<myArray.length; i++) {

            out += '<a href="#" onclick="myClick();">' + 
            myArray[i] + '</a>'+desc[i]+'<br><br><br>';
        }
        document.getElementById("ids").innerHTML = out;
    }



    function myClick()
    {
        alert("hii");
    }
    </script>
    </head>
     <body> 
    <div id="dvMap" style="width: 700px; height: 800px">
    </div>
 <ul id="ids"></ul>

</body>
</html>

点击标记我可以获得描述并放大地图但我 想制作可点击的链接,以便在地图上缩放和显示说明。

1 个答案:

答案 0 :(得分:0)

以下是演示如何在地图上选择标记的修改示例:

var g_locations = [
 {
     "title": 'Aksa Beach',
     "lat": '19.1759668',
     "lng": '72.79504659999998',
     "description": 'Aksa Beach is a popular beach and a vacation spot in Aksa village at Malad, Mumbai.'
 },
 {
     "title": 'Juhu Beach',
     "lat": '19.0883595',
     "lng": '72.82652380000002',
     "description": 'Juhu Beach is one of favourite tourist attractions situated in Mumbai.'
 },
 {
     "title": 'Girgaum Beach',
     "lat": '18.9542149',
     "lng": '72.81203529999993',
     "description": 'Girgaum Beach commonly known as just Chaupati is one of the most famous public beaches in Mumbai.'
 },
 {
     "title": 'Jijamata Udyan',
     "lat": '18.979006',
     "lng": '72.83388300000001',
     "description": 'Jijamata Udyan is situated near Byculla station is famous as Mumbai (Bombay) Zoo.'
 },
 {
     "title": 'Sanjay Gandhi National Park',
     "lat": '19.2147067',
     "lng": '72.91062020000004',
     "description": 'Sanjay Gandhi National Park is a large protected area in the northern part of Mumbai city.'
 }
];

var g_infoWindow;
var g_markers = [];


function loadMap() {
    var mapOptions = {
        center: new google.maps.LatLng(g_locations[0].lat, g_locations[0].lng),
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    g_infoWindow = new google.maps.InfoWindow();
    var latlngbounds = new google.maps.LatLngBounds();
    var map = new google.maps.Map(document.getElementById("map"), mapOptions);

    for (var i = 0; i < g_locations.length; i++) {

        var data = g_locations[i];
        var myLatlng = new google.maps.LatLng(data.lat, data.lng);
        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: data.title,
            description: data.description
        });

     
        (function (marker, data) {
            google.maps.event.addListener(marker, "click", function (e) {

                g_infoWindow.setContent("<div style = 'width:60px;min-height:40px'>" + data.description + "</div>");
                g_infoWindow.open(map, marker);
                map.setZoom(18);
                map.setCenter(marker.getPosition());
            });

           
        })(marker, data);
        latlngbounds.extend(marker.position);

        g_markers.push(marker);
    }
    
    map.setCenter(latlngbounds.getCenter());
    map.fitBounds(latlngbounds);
}



function initPanel() {
   
    for (var i = 0; i < g_markers.length; i++) {
        var li = document.createElement('li');
        document.getElementById("panel").appendChild(li);
        li.innerHTML = '<a href="#" onclick="selectLocation(' + i + ');">' + g_markers[i].title + '</a>' + g_markers[i].description;
    }
    
}



function selectLocation(index) {
    var selectedMarker = g_markers[index];
    var map = selectedMarker.getMap();
    g_infoWindow.setContent("<div style = 'width:60px;min-height:40px'>" + selectedMarker.description + "</div>");
    g_infoWindow.open(map, selectedMarker);
    map.setZoom(18);
    map.setCenter(selectedMarker.getPosition());
}


google.maps.event.addDomListener(window, 'load', function() {
    loadMap();
    initPanel();
});
html, body {
    height: 100%;
    margin: 0;
    padding: 0;
}

#map {
    height: 800px;
    width: 700px;
}
<script src="https://maps.googleapis.com/maps/api/js"></script> 
<div id="map"></div>
<ul id="panel"></ul>