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

Ashwin Parmar

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?

JasonM1

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);
  }

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Google maps dynamic center point PHP

分類Dev

How to center plotly 3d surface plot?

分類Dev

How to center text in navigation title view

分類Dev

How to align 3 boxes in the center

分類Dev

How to find view point coordinates?

分類Dev

Need intersect point for a line coming from center of circle?

分類Dev

ConstratintLayout: How to center a view according to another view, vertically

分類Dev

How to get middle point of Matlab movegui() center - west?

分類Dev

How to remove Media Center from Windows 8

分類Dev

How to scale a shape from its center as origin?

分類Dev

Swift charts - highlight center point

分類Dev

3D Earth globe on google maps API JS

分類Dev

Get all Google Map v3 markers loaded from GeoJSON

分類Dev

Flutter - How to proportionally center a view (centered with multiplier offset)

分類Dev

Place text on the radii extending from the center of a circle D3

分類Dev

How to align all td to center except a td with class

分類Dev

How to get all markers in google-maps-react

分類Dev

HTML Set Image Center On Div

分類Dev

How to center an iframe horizontally?

分類Dev

how to center MRI images

分類Dev

how to align this items at center?

分類Dev

How to center the horizontal axis

分類Dev

How to align <div> to center?

分類Dev

Google Maps API 3 - Show All Markers on Screen, but Keep Centerpoint

分類Dev

Determine the center point of multiple polygons in leaflet

分類Dev

SVG radial gradients - moving focal point (center)

分類Dev

How to center images in gif using convert from imagamagick?

分類Dev

How to Rotate a GameObject around its center, from script?

分類Dev

How to delete markers from my own google maps

Related 関連記事

ホットタグ

アーカイブ