【问题标题】:Google Places API, additional marker (manually added) to resultsGoogle Places API,附加标记(手动添加)到结果
【发布时间】:2013-11-07 17:13:12
【问题描述】:

我的 javascript 不太热,我正在尝试将手动标记添加到从 Google Places API 收集的多个位置上。

我跟着 this post 得到了我的以下代码(有一些修改):

<script type="text/javascript">
      var map;
      var infowindow;
      var service ;
    base_Icon_festival = "https://maps.gstatic.com/mapfiles/ms2/micons/volcano.png";
    shadow_festival = "https://maps.gstatic.com/mapfiles/ms2/micons/volcano.shadow.png";

      function initialize(lat,lng) 
      {
        var origin = new google.maps.LatLng(lat,lng);

        map = new google.maps.Map(document.getElementById('map'), {
          mapTypeId: google.maps.MapTypeId.HYBRID,
          center: origin,
          zoom: 14,
      scrollwheel: false,

        });

        var request = {
          location: origin,
          radius: 2500,
          types: ['train_station','bus_station','subway_station','airport']
        };
        infowindow = new google.maps.InfoWindow();
        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++) {
            createMarker(results[i]);
          }
        }
      }

    base_Icon_train = "http://maps.google.com/mapfiles/ms/icons/red-dot.png";               
        base_Icon_bus = "http://maps.google.com/mapfiles/ms/icons/green-dot.png";  
        base_Icon_subway = "http://maps.google.com/mapfiles/ms/icons/blue-dot.png";   
        base_Icon_airport = "http://maps.google.com/mapfiles/ms/icons/yellow.png";  

      function createMarker(place) {

    var placeLoc = place.geometry.location;
        var marker;
        var icon_to_use;

    if (place.types.indexOf('train_station') != -1) {
           icon_to_use = base_Icon_train;
    } else if (place.types.indexOf('bus_station') != -1) {
           icon_to_use = base_Icon_bus;
    } else if (place.types.indexOf('subway_station') != -1) {
           icon_to_use = base_Icon_subway;
    } else if (place.types.indexOf('airport') != -1) {
           icon_to_use = base_Icon_airport;
    } 

marker = new google.maps.Marker({
      map: map,
      position: place.geometry.location,
    icon: icon_to_use  
    });

        var content='<strong style="font-size:1.2em">'+place.name+'</strong>'+
                    '<br/><strong>Latitude: </strong>'+placeLoc.lat()+
                    '<br/><strong>Longitude: </strong>'+placeLoc.lng()+
                    '<br/><strong>Type: </strong>'+place.types[0];

        //make a request for further details
        service.getDetails({reference:place.reference}, function (place, status) 
                                    {
                                      if (status == google.maps.places.PlacesServiceStatus.OK) 
                                      {
                                        more_content='<hr/><strong><a href="'+place.url+'" target="details">Details</a>';

                                        if(place.website)
                                        {
                                          more_content+='<br/><br/><strong><a href="'+place.website+'" target="details">'+place.website+'</a>';
                                        }
                                      }
                                    });


        google.maps.event.addListener(marker, 'click', function() {

          infowindow.setContent(content+more_content);
          infowindow.open(map, this);
        });

      }

      google.maps.event.addDomListener(window, 'load', function(){initialize(<?php echo $coordinated; ?>);});
    </script>
    <div id="map" style="height:400px;"></div>

现在我想手动添加另一个位于地图中心的标记(即当前从 PHP 变量中提取的变量 origin - var origin = new google.maps.LatLng(lat,lng);)。我还需要这个标记有一个与之关联的不同图标(分别为base_Icon_festivalshadow_festival)。

我不确定如何手动将另一个标记添加到位置 API 已收集的标记中?

最终目标:在地图中心放置一个节日图标标记,并在其周围放置一些公共交通标记,从而生成用于网站各个节日页面的代码。

提前感谢任何可以提供帮助的人。

【问题讨论】:

  • 你试过什么?添加single marker,文档中也很好地介绍了自定义标记。

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


【解决方案1】:

在 Google Maps API 中,通过创建 Marker 对象并设置地图属性来将标记添加到地图中。这是代码中添加现有标记的 sn-p。

marker = new google.maps.Marker({
  map: map,
  position: place.geometry.location,
  icon: icon_to_use  
});

要添加新标记,您只需将位置和图标属性替换为您的值。

new google.maps.Marker({
  map: map,
  position: origin,
  icon: shadow_festival 
});

此代码可能会添加到回调函数的末尾,如果您不需要实际的标记对象来做其他任何事情,您可以只创建标记对象并且永远不要分配它。

Google Maps API 文档here 中有一个示例。

【讨论】:

  • 你的意思是这样的:base_Icon_festival = "https://maps.gstatic.com/mapfiles/ms2/micons/volcano.png"; shadow_festival = "https://maps.gstatic.com/mapfiles/ms2/micons/volcano.shadow.png"; function callback(results, status) { if (status == google.maps.places.PlacesServiceStatus.OK) { for (var i = 0; i &lt; results.length; i++) { createMarker(results[i]); } new google.maps.Marker({ map: map, position: origin, icon: base_Icon_festival }); } } - 如果是这样,它似乎不起作用......?
  • origin 是从 PHP 变量中提取的坐标,例如 (53.068864,-2.526276)。请参阅有问题的最后一段代码。地图我不确定...? Javascript 对我来说很新……
  • 我发现了问题。原点在初始化函数中声明,在回调中不可访问。在初始化函数之外声明原点,你应该很好。顺便说一句,很难在卫星上看到阴影。
  • 感谢您的帮助 Andrew,但我仍然无法让这个工作!我试过在初始化函数之外声明起源,但没有骰子。您介意在 pastebin 上粘贴解决方案吗? - 再次感谢您的帮助,为影子/卫星的提示欢呼。
  • 我发布了gist。它适用于 Chrome 和 IE10。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多