简体   繁体   English

有什么办法可以在Google地图的图层内添加标签或标记?

[英]Is there any way to add label or marker inside layer of a google map?

I am new with google map API. 我是google map API的新手。 I need to place a label inside a google map datalayer. 我需要在Google Map Datalayer中放置标签。 I need to get understand is it possible to write any javascript code to solve this problem? 我需要了解是否可以编写任何JavaScript代码来解决此问题?

  new google.maps.Marker({
      position: e.latLng,
      map: map,
      icon: {
        path: resultPath,
        fillColor: resultColor,
        fillOpacity: .2,
        strokeColor: 'white',
        strokeWeight: .5,
        scale: 10
      }
    });

from the above code instead of 'map:map' can we use any layers? 从上面的代码而不是'map:map'可以使用任何图层吗?

No, you can't, that must be a google.maps.Map or google.maps.StreetViewPanorama object per the documentation : 不,您不能,按照文档说明 ,该对象必须是google.maps.Mapgoogle.maps.StreetViewPanorama对象:

map 地图
Type: Map | 类型: 地图 | StreetViewPanorama 街景全景
Map on which to display Marker. 要在其上显示标记的地图。

The google.maps.Data layer doesn't receive common overlay objects (marker, polyline, polygon, circle, rectangle). google.maps.Data图层不接收常见的叠加层对象(标记,折线,多边形,圆形,矩形)。 It displays google.maps.Data.Feature objects or GeoJSON valid literals. 它显示google.maps.Data.Feature对象或GeoJSON有效文字。

You could, for example, add a marker to the data layer by doing 例如,您可以通过执行以下操作将标记添加到数据层

var geoJsonFeature = {
    type:'Feature',
    geometry:{
      type:'Point',
      coordinates:[e.latLng.lng(), e.latLng.lat()]
    }
}

google.maps.data.addGeoJson(geoJsonFeature);

Regarding the styling of your marker, you can set the datalayer style as 关于标记的样式,可以将数据层样式设置为

google.maps.data.setStyle(function(Feature) {

   return {
      icon: {
        path: Feature.getProperty('resultPath'),
        fillColor: Feature.getProperty('resultColor'),
        fillOpacity: .2,
        strokeColor: 'white',
        strokeWeight: .5,
        scale: 10
     }
   };        

});

so, for each datalayer google.maps.data.Feature you add, if you want markers to have different styles, you should set their path and fillColor beforehand as 因此,对于您添加的每个数据层google.maps.data.Feature ,如果希望标记具有不同的样式,则应事先将其路径和fillColor设置为

var geoJsonFeature = {
    type:'Feature',
    properties: {
       path: resultPath,
       fillColor: resultColor
    },
    geometry:{
      type:'Point',
      coordinates:[e.latLng.lng(), e.latLng.lat()]
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM