在多边形google earth api中获取地标

时间:2012-06-22 06:29:21

标签: api polygon google-earth marker

我是开发Google地球应用程序的新手。我想知道如何让地标入站我绘制的多边形。提前谢谢。

我的代码是

var ge;
var pm = null;
var isMouseDown = false;
var lineStringPlacemark = null;
var coords = null;
var pointCount = 0;
var doc = null;
var markers = [];
var polygons;

google.load("earth", "1");

function init() {
    google.earth.createInstance('map', initCB, failureCB);

}

function initCB(instance) {
   ge = instance;
   ge.getWindow().setVisibility(true);
   // add a navigation control
   ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);

   //create document
   doc = ge.createDocument('');
   ge.getFeatures().appendChild(doc);

   // add some event
   google.earth.addEventListener(ge.getGlobe(), 'mousemove', onmousemove); 
   google.earth.addEventListener(ge.getGlobe(), 'mousedown', function(event) { onmousedown(event); });

   loadData(ge);
}

function failureCB(errorCode) {
    alert(errorCode);
}

google.setOnLoadCallback(init);

function loadData(ge){
   EarthRequest = new htmldb_Get(null,$v('pFlowId'),'APPLICATION_PROCESS=LoadEarthData',$v('pFlowStepId'));
   EarthData = EarthRequest.get();

   if (EarthData) {
        var data = jQuery.parseJSON(EarthData);
        //printObject(data.row);
        $.each(data.row,function(item){
             latitude = parseFloat(data.row[item]['LATITUDE']);
             longitude = parseFloat(data.row[item]['LONGITUDE']);
             if(latitude==0 || longitude==0 || isNaN(latitude) || isNaN(longitude)){
              //do nothing
             }
             else{
                 marker = createMarker(ge,data.row[item]['CELL_ID']+' '+data.row[item]['SITE_NAME'],latitude ,longitude,data.row[item]['CELL_ID']);
                 if(marker){
            markers.push(marker);
         }
             }
        });

    var la = ge.createLookAt('');
    la.set(latitude, longitude,
      0,
      ge.ALTITUDE_RELATIVE_TO_GROUND,
      0,
      0, 
      5000 
      );
    ge.getView().setAbstractView(la);
   }
}

function createMarker(ge,placeName,latitude,longitude,placeId){
    placemark = ge.createPlacemark('');

    var icon = ge.createIcon('');
    icon.setHref('map_new.png');
    var style = ge.createStyle(''); //create a new style
    style.getIconStyle().setIcon(icon); 

    var lat = parseFloat(latitude);
    var lon = parseFloat(longitude);
    var point = ge.createPoint('');
    point.setLatitude(lat);
    point.setLongitude(lon);

    placemark.setStyleSelector(style); //apply the style to the placemark
    placemark.setGeometry(point);

    // add the placemark to the earth DOM
    ge.getFeatures().appendChild(placemark);

    placemark.setName(placeName);

    google.earth.addEventListener(placemark, 'click', function(event) {
         loadPlaceDetail(placeId);
    });

    return placemark;
}

function onmousemove(event) {
  if (isMouseDown) {
   coords.pushLatLngAlt(event.getLatitude(), event.getLongitude(), 0);
  }
}

//convert line to polygon
function convertLineStringToPolygon(placemark) {
  var polygon = ge.createPolygon('');
  var outer = ge.createLinearRing('');
  polygon.setOuterBoundary(outer);

  var lineString = placemark.getGeometry();
  for (var i = 0; i < lineString.getCoordinates().getLength(); i++) {
    var coord = lineString.getCoordinates().get(i);
    outer.getCoordinates().pushLatLngAlt(coord.getLatitude(), 
                                     coord.getLongitude(), 
                                     coord.getAltitude());
  }
  placemark.setGeometry(polygon);
  return polygon;
}

//mouse click event
function onmousedown(event) {
  if (isMouseDown) { 
        var lastdoc = doc.getFeatures().getChildNodes().getLength();

        isMouseDown = false;
        coords.pushLatLngAlt(event.getLatitude(), event.getLongitude(), 0);
        convertLineStringToPolygon(lineStringPlacemark);
    if(lastdoc>1){
        doc.getFeatures().removeChild(doc.getFeatures().getFirstChild());
                //I want to add some function to calculate polygon bound
        }

  } else {
    if(event.getAltKey()){

       isMouseDown = true;
       lineStringPlacemark = ge.createPlacemark('');
       var lineString = ge.createLineString('');
       lineStringPlacemark.setGeometry(lineString);
       lineString.setTessellate(true);
       lineString.setAltitudeMode(ge.ALTITUDE_CLAMP_TO_GROUND);

       lineStringPlacemark.setStyleSelector(ge.createStyle(''));
       var lineStyle = lineStringPlacemark.getStyleSelector().getLineStyle();
       lineStyle.setWidth(4);
       lineStyle.getColor().set('ddffffff');  // aabbggrr formatx
       lineStyle.setColorMode(ge.COLOR_RANDOM);
       var polyStyle = lineStringPlacemark.getStyleSelector().getPolyStyle();
       polyStyle.getColor().set('ddffffff');  // aabbggrr format
       polyStyle.setColorMode(ge.COLOR_RANDOM);

       coords = lineString.getCoordinates();
       coords.pushLatLngAlt(event.getLatitude(), event.getLongitude(), 0);

      //doc = ge.createDocument('');
       doc.getFeatures().appendChild(lineStringPlacemark);
    }
  }
}

所以在convertLineStringToPolygon运行之后,我需要获取多边形绑定并重新加载数据。如果绑定多边形的位置创建地标,如果没有跳过它。对于谷歌地图可以使用GPolygon.Contains()方法。但对于谷歌地球我还没有找到解决方案

这是来自ecoynm的示例谷歌地图:http://econym.org.uk/gmap/example_inside.htm

1 个答案:

答案 0 :(得分:0)

GEarthExtensions point in poly

使用Google地球扩展程序库http://code.google.com/p/earth-api-utility-library/

示例是使用鼠标移动事件

google.earth.addEventListener(ge.getGlobe(), 'mousemove', function(evt) {
  var poly = new geo.Polygon(pts);
  var contains = poly.containsPoint(
      new geo.Point(evt.getLatitude(), evt.getLongitude()));
  placemark.setStyleSelector(contains ? onStyle : offStyle);
});