Problem setting lat/long

32 views
Skip to first unread message

Walker

unread,
May 18, 2009, 9:32:42 AM5/18/09
to KML Developer Support - Google Earth Plug-in
Hello again,

You guys were brilliant earlier helping me find out how to calculate
camera altitude based on extent, but I've got another question.

I've got a small problem with my code posted below. In the setLat()
and setLng() functions I can't for the life of me get them to set when
you click
their respective buttons.

The quick fix is to comment out the two troublesome lines and simply
edit the source code for the desired latitude & longitude, but
ideally
I want the user to be able to input them just as they can input the
range. What puzzles me, is that using the exact same syntax works for
setting
the range, but doesn't work for setting the lat and lng.

My apologies if the code is a little messy, or if the fix for this is
something really simple. I'm not really sure if it's just
something wrong with my code or if it has to do with how I'm setting
my lookAt point within google earth.

Any help would greatly appreciated. Let me know if you need any
clarification.

Thanks!













<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /
>
<head>
<!--
Copyright 2008 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<title>Underlay Creator</title>
<!-- *** Replace the key below below with your own API key,
available at http://code.google.com/apis/maps/signup.html *** -->
<script src="http://www.google.com/jsapi?key=ABQIAAAAJjucYb-
UmLbNwb6Zr93UBBQcd1EN8YTzLvwr9D0CWHC9e2n4kRTOsbG1_9GnWONH739l8cAxZKvwqQ"></
script>
<script>
var ge;
var lat;
var lng;
var range;//range in nautical miles

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

function init() {
var content = document.getElementById('content');

var inputHTML = '<input id="latitude" value="Latitude"/>';
inputHTML += '<input type="button" onclick="setLat()" value="setLat
()"/>';

inputHTML += '<input id="longitude" value="Longitude"/>';
inputHTML += '<input type="button" onclick="setLng()" value="setLng
()"/>';

inputHTML += '<input id="rangeNM" value="RangeNM"/>';
inputHTML += '<input type="button" onclick="setRangeAndFly()"
value="setRangeAndFly()"/>';

content.innerHTML = inputHTML;

google.earth.createInstance('content', initCB, failureCB);
}

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

//un-comment line below to add a navigation control
//ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
//ge.setScaleLegendVisibility

//un-comment lines below to add desired layers
ge.getLayerRoot().enableLayerById(ge.LAYER_TERRAIN, false);
//ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
//ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);

document.getElementById('installed-plugin-version').innerHTML =
ge.getPluginVersion().toString();
}

function failureCB(errorCode) {}

function setLat() {
lat = document.getElementById('latitude').value;
//if you un-comment the line below, and comment the line above then
it //works as intended
//lat = 39.58031;
}

function setLng() {
lng = document.getElementById('longitude').value;
//if you un-comment the line below, and comment the line above then
it //works as intended
//lng = -108.10705;
}

function setRangeAndFly() {
range = document.getElementById('rangeNM').value;

latOffsetNauticalMiles = range;
latOffsetMiles = latOffsetNauticalMiles * 1.150779448;
latOffsetDegrees = latOffsetMiles / 69.172;

var r = 6378137; //var r = 6378700;
var F = 30.25;
var A = Math.max((lat + latOffsetDegrees), (lat - latOffsetDegrees))
- Math.min((lat + latOffsetDegrees), (lat -
latOffsetDegrees));
A = A * Math.PI / 180.0;
var x = r * Math.tan(A / 2);
var a = x / (Math.tan( F * Math.PI / 180.0));

var lookAt = ge.createLookAt('');
lookAt.set(lat, lng, a, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0, 0);
ge.getView().setAbstractView(lookAt);
}

google.setOnLoadCallback(init);
</script>
</head>
<body style="font-family: Arial;font-size:13px;border: 0 none;">
<div id="content" style="width: 1024px; height: 1024px;">
Loading...
</div>
</body>
</html>

shsavage

unread,
May 18, 2009, 12:45:08 PM5/18/09
to KML Developer Support - Google Earth Plug-in
Hi Walker,

If you just want to be able to fly to the user-specified location
and altitude, try this:

<script type="text/javascript">
var ge = null;
var lat = 39.58031;
var lng = -108.10705;
var range = 1000; // default range

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

//---------------------------------------------------------------
function init() {
google.earth.createInstance("map3d", initCB, failureCB);
}

//---------------------------------------------------------------
function initCB(instance) {
ge = instance;
ge.getWindow().setVisibility(true);

//un-comment line below to add a navigation control
ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
ge.setScaleLegendVisibility
//un-comment lines below to add desired layers
ge.getLayerRoot().enableLayerById(ge.LAYER_TERRAIN, false);
//ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
//ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);
}

//---------------------------------------------------------------
function failureCB(errorCode) {}

//---------------------------------------------------------------
function setLat(myLat) { lat = myLat; }

//---------------------------------------------------------------
function setLng(myLng) { lng = myLng; }

//---------------------------------------------------------------
function setRange(myRange) { range = myRange; }

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

</script>
</head>
<body onLoad="init()" style="font-family: Arial;font-size:
13px;border: 0 none;">
<div id="content" style="width: 1024px; height: 1024px;"><p>
Latitude: <input id="latitude" size = 10 value=39.58031
onChange="setLat(this.value)"/>
Longitude: <input id="longitude" size=10 value=-108.10705
onChange="setLng(this.value)"/>
Range: <input id="rangeNM" size=10 value=1000 onChange="setRange
(this.value)"/>
<input type="button" value = "Fly to this location"
onClick="flyTo()"/></p>
<div id='map3d_container' style='border: 1px solid maroon;
height: 600px; width: 800px'>
<div id='map3d' style='height: 100%; width: 100%; border:
medium double #5a5a28'></div>
</div>
</div>
</body>
</html>

-Steve

On May 18, 6:32 am, Walker wrote:
> Hello again,
>
> You guys were brilliant earlier helping me find out how to calculate
> camera altitude based on extent, but I've got another question.
>
> I've got a small problem with my code posted below.  In the setLat()
> and setLng() functions I can't for the life of me get them to set when
> you click
> their respective buttons.
>
> The quick fix is to comment out the two troublesome lines and simply
> edit the source code for the desired latitude & longitude, but
> ideally
> I want the user to be able to input them just as they can input the
> range. What puzzles me, is that using the exact same syntax works for
> setting
> the range, but doesn't work for setting the lat and lng.
>
> My apologies if the code is a little messy, or if the fix for this is
> something really simple. I'm not really sure if it's just
> something wrong with my code or if it has to do with how I'm setting
> my lookAt point within google earth.
>
> Any help would greatly appreciated.  Let me know if you need any
> clarification.
>
> Thanks!
>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>
>   <meta http-equiv="content-type" content="text/html; charset=utf-8" /
>
>   <head>
> <!--
> Copyright 2008 Google Inc.
>
> Licensed under the Apache License, Version 2.0 (the "License");
> you may not use this file except in compliance with the License.
> You may obtain a copy of the License at
>
>      http://www.apache.org/licenses/LICENSE-2.0
>
> Unless required by applicable law or agreed to in writing, software
> distributed under the License is distributed on an "AS IS" BASIS,
> WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> implied.
> See the License for the specific language governing permissions and
> limitations under the License.
> -->
>     <title>Underlay Creator</title>
>     <!-- *** Replace the key below below with your own API key,
> available athttp://code.google.com/apis/maps/signup.html*** -->

Walker

unread,
May 18, 2009, 2:21:07 PM5/18/09
to KML Developer Support - Google Earth Plug-in
Steve,

Thanks for the input, but your script doesn't seem to work for me
either. The user-input values aren't changing. The camera is only
zooming to the default that you have set at the top of your code like
this...
var ge = null;
var lat = 39.58031;
var lng = -108.10705;
var range = 1000; // default range

Changing the values in the boxes isn't changing where the camera flies
to at all.

This is the code I just tested it off of from you.

Thanks,

Walker








<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//
EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /
>
<head>
<!--
Copyright 2008 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<title>Underlay Creator</title>
<!-- *** Replace the key below below with your own API key,
available at http://code.google.com/apis/maps/signup.html *** -->
<script src="http://www.google.com/jsapi?key=ABQIAAAAJjucYb-
UmLbNwb6Zr93UBBQcd1EN8YTzLvwr9D0CWHC9e2n4kRTOsbG1_9GnWONH739l8cAxZKvwqQ"></
script>
<script>
var ge = null;
var lat = 39.58031;
var lng = -108.10705;
var range = 1000; // default range

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

shsavage

unread,
May 18, 2009, 2:54:31 PM5/18/09
to KML Developer Support - Google Earth Plug-in
Hi Walter,

Look at http://gaialab.asu.edu/DAAHL/test.htm - change any of the
values and then push the "Fly to this location" button. -s
> available athttp://code.google.com/apis/maps/signup.html*** -->
> ...
>
> read more »- Hide quoted text -
>
> - Show quoted text -

Walker

unread,
May 18, 2009, 3:37:13 PM5/18/09
to KML Developer Support - Google Earth Plug-in
Steve,

I opened your link, changed the values, hit "fly to this location" and
it was a no go.

The plug-in from your link only works if you leave the text box values
unchanged, and that's because of the default values because you
declared them at the top of your code to be the following...
var lat = 39.58031;
var lng = -108.10705;
var range = 1000; // default range

If you change the input boxes, and hit "fly to" still nothing happens.

Thanks,

http://gaialab.asu.edu/DAAHL/test.htm




On May 18, 1:54 pm, shsavage wrote:
> Hi Walter,
>
>    Look athttp://gaialab.asu.edu/DAAHL/test.htm- change any of the
> ...
>
> read more »

shsavage

unread,
May 19, 2009, 10:17:16 AM5/19/09
to KML Developer Support - Google Earth Plug-in
That's very odd. Click this link: http://gaialab.asu.edu/DAAHL/GETestMovie.wmv
to open a short video that shows everything working the way it's
supposed to. -S

On May 18, 12:37 pm, Walker wrote:
> Steve,
>
> I opened your link, changed the values, hit "fly to this location" and
> it was a no go.
>
> The plug-in from your link only works if you leave the text box values
> unchanged, and that's because of the default values because you
> declared them at the top of your code to be the following...
>  var lat = 39.58031;
>  var lng = -108.10705;
>  var range = 1000;    // default range
>
> If you change the input boxes, and hit "fly to" still nothing happens.
>
> Thanks,
>
> http://gaialab.asu.edu/DAAHL/test.htm
>
> On May 18, 1:54 pm, shsavage wrote:
>
>
>
> > Hi Walter,
>
> >    Look athttp://gaialab.asu.edu/DAAHL/test.htm-change any of the

Walker

unread,
May 19, 2009, 11:27:34 AM5/19/09
to KML Developer Support - Google Earth Plug-in
Thanks Steve!

Here's the thing, it doesn't work in firefox but if you open it in
internet explorer it runs like a dream. Tried re-tooling the code
maybe a dozen times to get it to work, then my co-worker suggested
opening it in IE and that was all it took.

In fact, I bet my original code will work fine in internet explorer as
well, but your setup is a bit better looking so I will probably end up
using it.

Walker

On May 19, 9:17 am, shsavage wrote:
> That's very odd.  Click this link:http://gaialab.asu.edu/DAAHL/GETestMovie.wmv
> to open a short video that shows everything working the way it's
> supposed to.  -S
>
> On May 18, 12:37 pm, Walker wrote:
>
> > Steve,
>
> > I opened your link, changed the values, hit "fly to this location" and
> > it was a no go.
>
> > The plug-in from your link only works if you leave the text box values
> > unchanged, and that's because of the default values because you
> > declared them at the top of your code to be the following...
> >  var lat = 39.58031;
> >  var lng = -108.10705;
> >  var range = 1000;    // default range
>
> > If you change the input boxes, and hit "fly to" still nothing happens.
>
> > Thanks,
>
> >http://gaialab.asu.edu/DAAHL/test.htm
>
> > On May 18, 1:54 pm, shsavage wrote:
>
> > > Hi Walter,
>
> > >    Look athttp://gaialab.asu.edu/DAAHL/test.htm-changeany of the
> ...
>
> read more »

DougH

unread,
May 19, 2009, 12:38:09 PM5/19/09
to KML Developer Support - Google Earth Plug-in
Walker -

Purists like to complain about Internet Explorer because it isn't
picky enough, but in this case that's why it works. Firefox doesn't
like using strings in this line:

lookAt.set(lat,lng,range,ge.ALTITUDE_RELATIVE_TO_GROUND,0,0,0);

If you make the following changes, your code will work in Firefox:

function setLat(myLat) { lat = myLat*1; }

function setLng(myLng) { lng = myLng*1; }

function setRange(myRange) { range = myRange*1; }

Doug

Walker

unread,
May 19, 2009, 2:39:32 PM5/19/09
to KML Developer Support - Google Earth Plug-in
Thanks Doug,

I made that change and it now runs (as I prefer) in Firefox as well.

fraser (Earth API Guru)

unread,
May 19, 2009, 6:47:53 PM5/19/09
to KML Developer Support - Google Earth Plug-in
Hi all,

Just to note the FF problem is due to the strict string/int variable
handling in the plug-in.
Also, I guess it is stylistic but I would suggest using Number,
parseInt or even parseFloat rather than *1 for readabilities sake.

i.e.
------------------------------------------------------
function setLng(myLng) { lng = Number(myLng); }

or

function setLng(myLng) { lng = pasreint(myLng, 10); }
------------------------------------------------------

F

shsavage

unread,
May 20, 2009, 9:41:33 AM5/20/09
to KML Developer Support - Google Earth Plug-in
Thanks to both Doug and Fraser for your insights on the FF issue.
Good to know!

-Steve
> > > Doug- Hide quoted text -

shsavage

unread,
May 20, 2009, 9:52:03 AM5/20/09
to KML Developer Support - Google Earth Plug-in
Hi Walter,

Glad this was finally resolved. FF, who knew (besides Fraser, of
course!)? If you want to get rid of the Fly To button, you could add
the call to flyTo() in the setLat, setLng and setRange functions.
Also, before "going into production" I'd reccommend building in a test
in each of these functions that will ensure the user's entry is within
appropriate limits. The example I ginned up was just a q&d to show
how to relocate.

-Steve
> > > >    Look athttp://gaialab.asu.edu/DAAHL/test.htm-changeanyof the
Reply all
Reply to author
Forward
0 new messages