Google地图:获取搜索结果并显示标记

时间:2011-09-08 11:31:40

标签: javascript google-maps

我想在google maps api上搜索特定邮政编码中的位置,并将其显示在地图画布上。假设我想搜索特定邮政编码中的所有必胜客店。我该如何编码呢?

1 个答案:

答案 0 :(得分:1)

我不确定邮政编码,但你可以给出一个位置周围的半径(以米为单位)(在这种情况下是纽约市 - 布鲁克林的某个地方,如此(JSFiddle):

<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>ExtJS Google Maps Integration</title>

    <!-- Google Map API -->
    <style type="text/css">
        html { height: 100% }
        body { height: 100%; margin: 0; padding: 0 }
        #map_canvas { height: 100% }
    </style>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false&region=US"></script>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
</head>
<body>
<div id="map" style="width:100%; height:100%;"></div>

<script>
    var latlng = new google.maps.LatLng(40.693308, -73.990659);

    var options = 
    {
        zoom: 16,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById("map"), options);

    var request = 
    {
        location: latlng,
        radius: '1000',
        name: ['Pizza Hut']
    };

    var service = new google.maps.places.PlacesService(map);
    service.search( request, callback );

    function callback(results, status) 
    {
        if (status == google.maps.places.PlacesServiceStatus.OK) 
        {
            for ( var i = 0; i < results.length; i++ ) 
            {
                var place = results[i];
                var loc = place.geometry.location;
                var marker = new google.maps.Marker
                ({
                    position: new google.maps.LatLng(loc.Pa,loc.Qa)
                });
                marker.setMap(map);  
            }
        }
    }
</script>
</body>
</html>