Google 3D Earth How to find the center point from all the markers and set center view

StackOverflow https://stackoverflow.com/questions/17833347

  •  04-06-2022
  •  | 
  •  

سؤال

I am new to Google 3D Earth API

I would like to add multiple palcemarkers on Google Earth?

This is my sample code can any one suggest me where I am going to wrong to add multiple markers?

var ge;
var placemaker = new Array();
var point = new Array();

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

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

  function initCB(instance) {

     ge = instance;
     ge.getWindow().setVisibility(true);
     ge.getNavigationControl().setVisibility(ge.VISIBILITY_HIDE);

    for(var i = 0; i < data.length; i++ ) 
    {
        // Set the placemark's location.  
     point[i] = ge.createPoint( data[i].content );
         point[i].setLatitude( data[i].lat );
         point[i].setLongitude( data[i].log );
         placemark[i].setGeometry(point[i]);

         // Add the placemark to Earth.
        ge.getFeatures().appendChild(placemark[i]);
    }
  }

  function failureCB(errorCode) {}

  google.setOnLoadCallback(init);

Here data is an array of object(s) has lat, log and content. I will not seen any of the placemarker on the earth. If i push one single placemarker will working fine but not working if i go with loop.

In Google Map V there is options for bounds. is there any options available for 3D earth?

هل كانت مفيدة؟

المحلول

You need to create a Placemark object using createPlacemark(). You probably don't need to create an array of Placemarks since the google earth API keeps the list of placemarks from which you can iterate using ge.getFeatures().getChildNodes().

for(var i = 0; i < data.length; i++ ) 
{
     // Create the Placemark
     var placemark = ge.createPlacemark('');

     // Set the placemark's location.  
     point[i] = ge.createPoint('');
     point[i].setLatitude( data[i].lat );
     point[i].setLongitude( data[i].lon );       
     placemark.setGeometry(point[i]);

     // Add the placemark to Earth.
     ge.getFeatures().appendChild(placemark);
}

Also, probably won't need array of points since have the array of data. Can simplify this to the following:

for(var i = 0; i < data.length; i++ ) 
{    
     // Create the Placemark
     var placemark = ge.createPlacemark('');

     // Set the placemark's location.  
     var point = ge.createPoint('');
     point.setLatitude( data[i].lat );
     point.setLongitude( data[i].lon );
     placemark.setGeometry(point);

     // Add the placemark to Earth.
    ge.getFeatures().appendChild(placemark);
}

If you want to compute the center bounds of a bunch of generic KML features you can use an Google Earth API extension: https://code.google.com/p/earth-api-utility-library/wiki/GEarthExtensionsDomReference#computeBounds(object)

But if you just have an array of points then you can easily compute the center manually then set the LookAt to the computed center view point.

The final initCB() would look like this:

 function initCB(instance) {
    ge = instance;
    ge.getWindow().setVisibility(true);
    ge.getNavigationControl().setVisibility(ge.VISIBILITY_HIDE);

    var latSum = 0.0;
    var lonSum = 0.0;
    for(var i = 0; i < data.length; i++ ) 
    {
        // Set the placemark's location.  
        var point = ge.createPoint('');
        point.setLatitude( data[i].lat );
        point.setLongitude( data[i].lon );
        latSum += data[i].lat;
        lonSum += data[i].lon;

        var placemark = ge.createPlacemark(data[i].content);
        placemark.setGeometry(point);

        // Add the placemark to Earth.
        ge.getFeatures().appendChild(placemark);
    }

    // Create LookAt at the center view point
    var lookAt = ge.createLookAt('');
    lookAt.set(latSum / data.length, lonSum / data.length, 0,
            ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0, 20000);
    ge.getView().setAbstractView(lookAt);
  }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top