iidioter / earth-api-samples

Automatically exported from code.google.com/p/earth-api-samples
0 stars 0 forks source link

GoogleEarth coordinates with "," instead of "."; FlyTo does not work in Firefox. #667

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
The installed Plugin Version is 6.0.3.2197; the OS is Windows 7.

A) SOME ESSENTIAL PRELIMINARY REMARKS.
I made two HTML tools to convert the coordinates of the waypoints, imported 
from a handheld GPS unit to my PC, and visualize them by means of Google Maps.
The coordinates can be either inserted in the formats [ddd°mm'ss.ssss"] and 
[ddd°mm.mmmm"] in separate fields, then converted clicking the proper button; 
or directly entered in the result fields (that works also as inputs) in the 
format [ddd.dddddd°].
Then, the waypoint can be visualized in Google Maps by clicking the "visualize" 
(Visualizza) button.
I made two versions of them: the first uses the API V3 version and allow both 
to switch to "Street View" and to display the coordinates of a different point 
by clicking on it (with the mouse) in the map.
The second is similar; it can't show the "Street View", but can switch to 
Google Earth by means of the Google Maps/Earth integration: the command 
"map.addMapType(G_SATELLITE_3D_MAP)".
You can try the working versions (in Italian, but they are obvious, anyway) on:

http://www.train-simulator.net/GPS_Viewer/+Converter_Large_Test_coord_6b.htm 
(larger window);

http://www.train-simulator.net/GPS_Viewer/+Converter_Medium_Test_coord_6b.htm 
(smaller window);

http://www.train-simulator.net/GPS_Viewer/+Maps-Earth_Convert_Large2b.html 
(larger window);

http://www.train-simulator.net/GPS_Viewer/+Maps-Earth_Convert_Medium2b.html 
(smaller window);

B) THE PROBLEM (better: the 2 problems...).
After having seen the documentation about the Google Earth API and some 
interesting samples, like "Globe/Window Event Handling": 

http://earth-api-samples.googlecode.com/svn/trunk/examples/groundaltitude.html

that shows the Ground Altitude at Mouse, I tried to adapt my previous tools to 
Google Earth. I followed the code of this page and, obviously, the scripts on 
the "Google Earth API Developer's Guide":

http://code.google.com/intl/it-IT/apis/earth/documentation/index.html 

The tool visualize in real time the coordinates and the height of the point the 
mouse hoovers on.
You find this first try at:

http://www.train-simulator.net/GPS_Viewer/M_E_C_M_L17h.html

and a screen dump:

www.train-simulator.net/GPS_Viewer/M_E_C_M_L17h.jpg

Hereinafter you can see the relevant code.
When the page is loaded, it opens a window with an instance of Google Earth, 
setted on a fixed point.

<<!-- Loading the Google Earth API; "key" valid for www.train-simulator.net 
site -->
<script type="text/javascript" 
src="http://www.google.com/jsapi?hl=en&amp;key=ABQIAAAAY70MZLbEZNORRYvqxWBFJxT42
v60HL-j2p8aV0da7mn-N9MQNhQ8si6FUgJhbof3wCIjzX59E9Kolw"></script>

<!-- Creating a container for the plugin -->
<table  border="2" cellpadding='7' bgcolor='#ddF9F9'>
<tr>
<td><div id="ui" style="position: relative;"> </div>
    <div id="map3d" style="border: 1px; width: 1000px; height: 400px;"></div></td>
    </tr>
</table>

<!-- Creating initialization functions -->
  <script type="text/javascript">
  var ge;
  google.load("earth", "1");
  function init() {
    google.earth.createInstance('map3d', earthCB, failureCB); 
// DIV element for the instance, the function to call in case of success, the 
function to call in case of failure
  }

// Executed on HTML loading, creates a first view of Google Earth for a preset 
point
  function earthCB(instance) {
    ge = instance;
    ge.getWindow().setVisibility(true);
    // add a navigation control
    ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
    // add some layers
    ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
    ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);
    ge.getLayerRoot().enableLayerById(ge.LAYER_TERRAIN, true);
    // Make buildings visible
     ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS, true);
// Set the first view on preset coordinates
    var la = ge.createLookAt('');
    la.set(43.740, 7.42, 0, ge.ALTITUDE_RELATIVE_TO_GROUND,
           25, 70, 5000);
// Create the preset view
    ge.getView().setAbstractView(la);
  }

  function failureCB(errorCode) {
  }

// Calling the intialization function when the page is loaded
  google.setOnLoadCallback(init);

As you see, the location of the viewer is 43.740 latitude and 7.42 longitude: 
the Principality of Monaco.

If the user wants to fly to a particular location, enters it in the latitude 
("Latitudine") and longitude ("Longitudine") fields; or uses the converter. The 
height (Altezza) is preset, but can be changed.
Here is the HTML code:

<!-- HTML code for the input of user's coordinates and altitude -->
<form name=form10 method=POST>
<table summary="" border="1" cellpadding='7' bgcolor='#ddF9F9'>
<tr>
<tr>
      <td>Latitudine: </td>
      <td><input type='text' tabindex="14" name='lat' style='width: 80px;'></td>
      <td>Longitudine: </td>
      <td><input type='text' tabindex="15" name='lon' style='width: 80px;'></td>
</tr>
<tr>
      <td>Altezza: </td>
      <td><input type='text' value='9000' tabindex="15" name='alt' style='width: 60px;'></td>
<!-- Button to get the user's coordinates and call the "FlyTo" function -->
 <td>
 <input type="button" tabindex="17" style="background-color: #EFEFEF;" value = "Visualizza"
    onClick ="lookAt(document.form10.lat.value,document.form10.lon.value,document.form10.alt.value,6000)">
<!-- parameters passed: latitude, longitude, height [user's], range [fixed] -->
  </td>
    </tr>
</nobr>
</tr>
</table>
</form> 

And this is the script code:

// Function for the "FlyTo" -->
function lookAt(lat, lng, alt, range) {
// parameters received: latitude, longitude, height, range
    var la = ge.createLookAt('');

// Optional Box to verify the coordinates passed from HTML input to the FlyTo 
function
//    alert('latit. = '+lat+', longit. = '+lng+ ', altezza = '+alt); 
// Set the view
    la.set(lat, lng, alt, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0,range);
// Create the view
    ge.getView().setAbstractView(la);
}
  </script>

C) Now here is the PROBLEM #1. It appears opening the page with Internet 
Explorer 8.
Having set the coordinates - whichever you want - like the ones on the picture, 
clicking the "visualize" (Visualizza) button, Google Earth flies to the 
location "0.00 0.00"!!! Clearly it is a point in the Atlantic Ocean at West of 
Africa, at the the crossing of Equator and the Greenwich Meridian!!!

Only by accident I finally discovered that the problem are the decimal 
points(!). If the coordinates are entered (see the picture) like "45,00125" 
"7,969389" (with commas!), Google Earth flies to the requested location!
This is absolutely ridiculous, nonsensical and inexplicable!!

In all the scripts, a floating point math is used, were the decimal part of the 
numbers is separated by the integer one by means of a point "."; and for the 
preset location, the coordinates are hard coded with points:
 "la.set(43.740, 7.42, 0, ge.ALTITUDE_RELATIVE_TO_GROUND, 25, 70, 5000);".
In the 2 previous tools, the coordinates are correctly interpreted with the 
point. Google Maps flies to the requested location only if this is entered with 
POINTS in the coordinates.
Here instead, the coordinates are only interpreted if the comma "," is used in 
place of the point "."...

D) And here is the PROBLEM #2. 
When the page is first loaded with Mozilla Firefox (I tried with either the 
3.6.20 and the 6.0 versions), it opens the window with an instance of Google 
Earth. Like in Internet Explorer. And the coordinates and the height are 
correctly displayed.
But after entering/converting the coordinates (with the comma, as for Internet 
Explorer), clicking the "visualize" button NOTHING HAPPENS!.
I tried with Firefox 3.6.20 version on a different PC, too; but the result was 
the same: no response from the visualize button.
I summarize: with Internet Explorer the button works and Google Earth flies to 
the new location; with Mozilla Firefox nothing happens!

Any idea for my 2 problems?

Original issue reported on code.google.com by nccbi...@alice.it on 21 Aug 2011 at 6:30

GoogleCodeExporter commented 9 years ago
By now I resolved the problem #1 with a trick: substitute a "," for the ".".
Here is the new code, compared to the previous. I added the code to have a 
placemark,too; every time a set of coordinates is converted, a new placemark is 
added:

// previous
<form  NAME="earth">
  <script type="text/javascript">
  var ge;
  google.load("earth", "1");
...............

function lookAt(lat, lng, alt, range) {
    var la = ge.createLookAt('');
    la.set(lat, lng, alt, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0,
range);
    ge.getView().setAbstractView(la);
}

// new
<form  NAME="earth">
  <script type="text/javascript">
  var ge;
  var counter = 1;  // create a counter for placemarks (see after)      
  google.load("earth", "1");
...............

function lookAt(lat, lng, alt, range) {
    var la = ge.createLookAt('');
    lat2=lat.replace(".",",");  lng2=lng.replace(".",",");
    // No error if "." is missing  :)
    la.set(lat2, lng2, alt, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0, range);
    ge.getView().setAbstractView(la);
    // Create a placemark.
    var placemark = ge.createPlacemark('');
    placemark.setName("marker "+ counter);
// Set the placemark's location.
    var point = ge.createPoint('');
    point.setLatitude(lat2); 
    point.setLongitude(lng2); 
    placemark.setGeometry(point);
// Add the placemark to Earth.
    ge.getFeatures().appendChild(placemark);
    ge.getView().setAbstractView(la);
// Increment the placemark counter
        counter++;
}

And lastly, a button to remove the placemarks:

HTML:
<!-- Button to remove the placemarks, from last to first -->
     <input type="button" tabindex="18" style="background-color: #EFEFEF;" value = "Rimuovi marker"
    onClick ="remove_placemark()">  

and the relative code:
  function remove_placemark () {
    last = ge.getFeatures().getLastChild(); // Last child in the list of objects.
        ge.getFeatures().removeChild(last); // remove it
             counter-- ;    // Decrement placemark counter
  }  

The new web page is at 
http://www.train-simulator.net/GPS_Viewer/M_E_C_M_L17m.html
A screen dump is at http://www.train-simulator.net/GPS_Viewer/M_E_C_M_L17m.jpg

Original comment by nccbi...@alice.it on 21 Aug 2011 at 11:29