Google 地图 API 从 GeoJSON 点几何绘制文本

Google Maps API draw a text from GeoJSON point geometry

我有这样的 geoJSON 数据:

{ "type": "FeatureCollection", 
  "features": [{"type": "Feature",
                "properties": {"text": "Street Foo", 
                               "Font": "25", 
                               "Angle": "0.99999"},
                "geometry":{"type":"Point",
                            "coordinates":[44.4878559081156,9.76673954155489]}}
              ]
}

我想用 Google 地图 API.

绘制字体大小为 25 和辐射角倾角为 0.999 的文本 "Street Foo"

我试过这个:

    var mapOptions = {
    zoom: 14,
    center: new google.maps.LatLng(44.4878559081156,9.76673954155489),
    mapTypeId: google.maps.MapTypeId.SATELLITE
    };

    var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
    testi = new google.maps.Data();
    testi.loadGeoJson('myjsonData.json');  

    // HERE I DON'T KNOW HOW TO GET TEXT, FONT AND ANGLE AND
    // DRAW THE TEXT...

    testi.setMap(map);

听起来你会想要使用 MarkerWithLabel

function initMap(lat, lon){

    var address = new google.maps.LatLng(lat, lon);
    var figureLabel = document.createElement('Figure');
    var pictureLabel = document.createElement('img');
    pictureLabel.src = "../icons/custom_map_marker.png";
    var caption = document.createElement('FIGCAPTION');
    label = " :) ";
    var text = document.createTextNode(label);

    figureLabel.appendChild(pictureLabel);
    figureLabel.appendChild(caption);

    var map = new google.maps.Map(document.getElementById('LocationMap'), {
        zoom : 17,
        center : address,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });


    marker = new MarkerWithLabel({
        position : address,
        map : map,
        labelContent : figureLabel,
        labelClass : 'Label',
        labelAnchor : new google.maps.Point(65, 83)
    });

}

是我过去用过的东西。

你会注意到标签被分类在对象实例化的第四行:labelClass 在你对应的 CSS 中,你应该使用 transform 来适当地调整它的角度。当然你会想在这里使用 JQuery,所以像 $('.Label').css('transform','rotate('+yourAngle+'deg)')

对于坐标,不使用参数,直接使用给定的两个坐标即可。所以

var address = new google.maps.LatLng(object.eatures[0].geometry.coordinates[0],object.features[0].geometry.coordinates[1])

一种选择是在使用 "addfeature" 事件加载 GeoJson 时对其进行处理,然后使用第三方 InfoBox library

在地图上显示文本
  1. 您的 GeoJson 坐标或您的地图中心坐标向后。
center: new google.maps.LatLng(44.4878559081156,9.76673954155489),

"coordinates":[44.4878559081156,9.76673954155489]

a google.maps.LatLng的坐标顺序为Latitude,Longitude,GeoJson的顺序相反。

  1. 到 "process" 加载 GeoJson 在 Data addFeature 事件上使用事件侦听器:
map.data.addListener('addfeature', function(evt) {
  if (evt.feature.getGeometry().getType() == "Point") {
    var coord = evt.feature.getGeometry().get();
    var labelText = evt.feature.getProperty("text");
    var labelFontSize = evt.feature.getProperty("Font") + "px";
    var labelAngle = evt.feature.getProperty("Angle");
  // ...
  1. 要旋转文本,相关问题的代码:
 var labelText = "4.32";
 var labelDiv = document.createElement("div");
 labelDiv.innerHTML = labelText;
 labelDiv.setAttribute("id", "label");
 labelDiv.setAttribute("style", "-ms-transform: rotate(45deg); -webkit-transform: rotate(45deg);     transform: rotate(45deg);");

proof of concept fiddle

代码片段:

var geocoder;
var map;

function initialize() {
  var mapOptions = {
    zoom: 14,
    center: new google.maps.LatLng(44.4878559081156, 9.76673954155489),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
  map.data.addListener('addfeature', function(evt) {
    if (evt.feature.getGeometry().getType() == "Point") {


      var coord = evt.feature.getGeometry().get();
      var labelText = evt.feature.getProperty("text");
      var labelFontSize = evt.feature.getProperty("Font") + "px";
      var labelAngle = evt.feature.getProperty("Angle");
      var labelDiv = document.createElement("div");
      labelDiv.innerHTML = labelText;
      labelDiv.setAttribute("id", "label");
      labelDiv.setAttribute("style", "-ms-transform: rotate(" + labelAngle + "deg); -webkit-transform: rotate(" + labelAngle + "deg);     transform: rotate(" + labelAngle + "deg);");

      var myOptions = {
        content: labelDiv,
        boxStyle: {
          border: "none",
          textAlign: "center",
          fontSize: labelFontSize,
          width: "50px"
        },
        disableAutoPan: true,
        pixelOffset: new google.maps.Size(-25, 0),
        position: coord,
        closeBoxURL: "",
        isHidden: false,
        pane: "mapPane",
        enableEventPropagation: true
      };

      var ibLabel = new InfoBox(myOptions);
      ibLabel.open(map);
    }
  });

  map.data.addGeoJson(geoJson);
  map.data.setMap(null);

}
google.maps.event.addDomListener(window, "load", initialize);

var geoJson = {
  "type": "FeatureCollection",
  "features": [{
    "type": "Feature",
    "properties": {
      "text": "Street Foo",
      "Font": "25",
      "Angle": "30.99999"
    },
    "geometry": {
      "type": "Point",
      "coordinates": [9.76673954155489, 44.4878559081156]
    }
  }]
};
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
<script src="https://cdn.rawgit.com/googlemaps/v3-utility-library/master/infobox/src/infobox.js"></script>
<div id="map_canvas"></div>