xenia27
asked on
User Current Location
I am trying to create a Java web application with google api. I will be able to locate a user's location and mark it on the google map. Then, I need to store the information of user's location. How exactly I can do this?
I try to use google.loader.ClientLocati on with JavaScript to get user's information. So, I tried to call through Java; therefore, I can store information into some table. However, I keep having some error message said "some variable is not defined" or "google is not defined".
What can I do to make this work?
I try to use google.loader.ClientLocati
What can I do to make this work?
I need to store the information of user's location?
could you try store user's information into cookies?
could you try store user's information into cookies?
ASKER
Error Message...
javax.script.ScriptExcepti on: sun.org.mozilla.javascript .internal. EcmaError: ReferenceError: "cl" is not defined. (<Unknown source>#1) in <Unknown source> at line number 1
at com.sun.script.javascript. RhinoScrip tEngine.in voke(Unkno wn Source)
at com.sun.script.javascript. RhinoScrip tEngine.in vokeFuncti on(Unknown Source)
at com.unet.googleproject.ser ver.Greeti ngServiceI mpl.greetS erver(Gree tingServic eImpl.java :44)
at sun.reflect.NativeMethodAc cessorImpl .invoke0(N ative Method)
at sun.reflect.NativeMethodAc cessorImpl .invoke(Un known Source)
at sun.reflect.DelegatingMeth odAccessor Impl.invok e(Unknown Source)
at java.lang.reflect.Method.i nvoke(Unkn own Source)
at com.google.gwt.user.server .rpc.RPC.i nvokeAndEn codeRespon se(RPC.jav a:527)
at com.google.gwt.user.server .rpc.Remot eServiceSe rvlet.proc essCall(Re moteServic eServlet.j ava:166)
at com.google.gwt.user.server .rpc.Remot eServiceSe rvlet.doPo st(RemoteS erviceServ let.java:8 6)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 713)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 806)
at org.mortbay.jetty.servlet. ServletHol der.handle (ServletHo lder.java: 487)
at org.mortbay.jetty.servlet. ServletHan dler$Cache dChain.doF ilter(Serv letHandler .java:1093 )
at com.google.apphosting.util s.servlet. Transactio nCleanupFi lter.doFil ter(Transa ctionClean upFilter.j ava:43)
at org.mortbay.jetty.servlet. ServletHan dler$Cache dChain.doF ilter(Serv letHandler .java:1084 )
at org.mortbay.jetty.servlet. ServletHan dler.handl e(ServletH andler.jav a:360)
at org.mortbay.jetty.security .SecurityH andler.han dle(Securi tyHandler. java:216)
at org.mortbay.jetty.servlet. SessionHan dler.handl e(SessionH andler.jav a:181)
at org.mortbay.jetty.handler. ContextHan dler.handl e(ContextH andler.jav a:712)
at org.mortbay.jetty.webapp.W ebAppConte xt.handle( WebAppCont ext.java:4 05)
at com.google.apphosting.util s.jetty.De vAppEngine WebAppCont ext.handle (DevAppEng ineWebAppC ontext.jav a:54)
at org.mortbay.jetty.handler. HandlerWra pper.handl e(HandlerW rapper.jav a:139)
at com.google.appengine.tools .developme nt.JettyCo ntainerSer vice$ApiPr oxyHandler .handle(Je ttyContain erService. java:306)
at org.mortbay.jetty.handler. HandlerWra pper.handl e(HandlerW rapper.jav a:139)
at org.mortbay.jetty.Server.h andle(Serv er.java:31 3)
at org.mortbay.jetty.HttpConn ection.han dleRequest (HttpConne ction.java :506)
at org.mortbay.jetty.HttpConn ection$Req uestHandle r.content( HttpConnec tion.java: 844)
at org.mortbay.jetty.HttpPars er.parseNe xt(HttpPar ser.java:6 44)
at org.mortbay.jetty.HttpPars er.parseAv ailable(Ht tpParser.j ava:205)
at org.mortbay.jetty.HttpConn ection.han dle(HttpCo nnection.j ava:381)
at org.mortbay.io.nio.SelectC hannelEndP oint.run(S electChann elEndPoint .java:396)
at org.mortbay.thread.Bounded ThreadPool $PoolThrea d.run(Boun dedThreadP ool.java:4 42)
javax.script.ScriptExcepti
at com.sun.script.javascript.
at com.sun.script.javascript.
at com.unet.googleproject.ser
at sun.reflect.NativeMethodAc
at sun.reflect.NativeMethodAc
at sun.reflect.DelegatingMeth
at java.lang.reflect.Method.i
at com.google.gwt.user.server
at com.google.gwt.user.server
at com.google.gwt.user.server
at javax.servlet.http.HttpSer
at javax.servlet.http.HttpSer
at org.mortbay.jetty.servlet.
at org.mortbay.jetty.servlet.
at com.google.apphosting.util
at org.mortbay.jetty.servlet.
at org.mortbay.jetty.servlet.
at org.mortbay.jetty.security
at org.mortbay.jetty.servlet.
at org.mortbay.jetty.handler.
at org.mortbay.jetty.webapp.W
at com.google.apphosting.util
at org.mortbay.jetty.handler.
at com.google.appengine.tools
at org.mortbay.jetty.handler.
at org.mortbay.jetty.Server.h
at org.mortbay.jetty.HttpConn
at org.mortbay.jetty.HttpConn
at org.mortbay.jetty.HttpPars
at org.mortbay.jetty.HttpPars
at org.mortbay.jetty.HttpConn
at org.mortbay.io.nio.SelectC
at org.mortbay.thread.Bounded
my html file...
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true">
var cl;
google.load("maps", "2.x");
if (google.loader.ClientLocation)
cl = google.loader.ClientLocation;
function GetClientLatitude()
{
return cl.latitude;
}
function GetClientLongitude()
{
return cl.longitude;
}
function sumTwoValue(var1, var2)
{
return var1 + var2;
}
</script>
my java file...
// getting engine
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
Invocable invocableEngine = (Invocable)jsEngine;
String value = new String();
try {
jsEngine.eval("function GetClientLatitude() {" +
" return cl.latitude;" +
"}");
// (Java-|ëScript Method&púÞ³<
System.out.println("return value from javascript is : " +
invocableEngine.invokeFunction("GetClientLatitude"));
value = invocableEngine.invokeFunction("GetClientLatitude").toString();
} catch (ScriptException ex) {
ex.printStackTrace();
} catch (NoSuchMethodException ex) {
ex.printStackTrace();
}
ASKER
What's wrong with my code?
Cookie? Which one is easiest one?
Cookie? Which one is easiest one?
xenia27,
Java and Javascript are two completely different languages and run at different times in a web page's "life." Java would be done by the server or maybe an applet; Javascript is run by the browser. This is why variables of one won't be available to the other.
What exactly do you mean a Java web app? Google's API uses Javascript and you would use Javascript to work with it in your browser. At least the part that is used for maps? What is the code you are trying to use to locate the location? What API exactly? The ClientLocation property I have seen in Google's API is related to their AJAX API. It is Javascript which is used to work with it. If you are having a problem with it then please provide more specifics on the code and when this problem occurs in relation to what you are doing.
Let me know if you have any questions or need more information.
b0lsc0tt
Java and Javascript are two completely different languages and run at different times in a web page's "life." Java would be done by the server or maybe an applet; Javascript is run by the browser. This is why variables of one won't be available to the other.
What exactly do you mean a Java web app? Google's API uses Javascript and you would use Javascript to work with it in your browser. At least the part that is used for maps? What is the code you are trying to use to locate the location? What API exactly? The ClientLocation property I have seen in Google's API is related to their AJAX API. It is Javascript which is used to work with it. If you are having a problem with it then please provide more specifics on the code and when this problem occurs in relation to what you are doing.
Let me know if you have any questions or need more information.
b0lsc0tt
>>if (google.loader.ClientLocat ion)
probably evaluated to false
probably evaluated to false
ASKER
How can I avoid this situation?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
as b0lsc0tt points out above (and you appear to have ignored) you're not loading the google js, or creating cl.
What you do in your html is completely independent to what you do in your java code.
ie. your html has nothing to do with your problem, you need to load the google js, and create cl in your *java* code if that where you want to use it.
Or make the call in your html (instead of java) and update the html dynamically.
What you do in your html is completely independent to what you do in your java code.
ie. your html has nothing to do with your problem, you need to load the google js, and create cl in your *java* code if that where you want to use it.
Or make the call in your html (instead of java) and update the html dynamically.
>>you're not loading the google js
That's not the case, it is being loaded, or an attempt is being made.
But don't you need a key as part of the query string?
That's not the case, it is being loaded, or an attempt is being made.
But don't you need a key as part of the query string?
There's absolutely no guarantee that ClientLocation will be valid, even when the js is loaded in the most ideal way, as described in the link i posted
Java isn't my expertise so may be it does have a way to run Javascript as a browser would and get and results or responses. I do know a key is a required part of the request to Google's API. Let me know if you need info on how to get it.
This actually brings us to a possible issue. Even if Java provides a way to do it the key is tied to a domain. Although the key can also work for "localhost" test it won't work from just anywhere. This may be an issue that will cause your efforts to not work but I don't know enough about how Java would use the script to know for sure.
Let me know if you have a question or how this helps.
bol
This actually brings us to a possible issue. Even if Java provides a way to do it the key is tied to a domain. Although the key can also work for "localhost" test it won't work from just anywhere. This may be an issue that will cause your efforts to not work but I don't know enough about how Java would use the script to know for sure.
Let me know if you have a question or how this helps.
bol
b0lsc0tt,
your initial analysis of the cause of the problem was correct :)
your initial analysis of the cause of the problem was correct :)
ASKER
OK...here is what I try to do..
This is what I try to do...
When a user login on the web page, I can locate which city (even the exac location) of this user. So the user can explorer the nearby product around this user.
I need to show the location and other products' location on a map; hence, I would love to have a google map on my web page. Also, there will be more functions in Java program, then, I want to figure how I can get the information of the user's location.
Not sure whether this information is enough...
Still trying to work on the tips above...
This is what I try to do...
When a user login on the web page, I can locate which city (even the exac location) of this user. So the user can explorer the nearby product around this user.
I need to show the location and other products' location on a map; hence, I would love to have a google map on my web page. Also, there will be more functions in Java program, then, I want to figure how I can get the information of the user's location.
Not sure whether this information is enough...
Still trying to work on the tips above...
Have you fixed up your java code to include all the javascript you need yet?
Why are you making the calls from java in the first place instead of directly from your html?
Why are you making the calls from java in the first place instead of directly from your html?
executing it in your java would not appear to be what you want.
ASKER
Nope, not yet...sorry for taking so long. I would love to use google api in my html and get the values from my html to my java program. Will this work?
Or, is there other ways to get values from my html to my java program?
Or, is there other ways to get values from my html to my java program?
> would love to use google api in my html and get the values from my html to my java program.
you would just post them, but why do you need them in your java program?
you would just post them, but why do you need them in your java program?
ASKER
because I need to store these information into a database and when the particular user login again, the program will show these information again. There are also some statistics calculation involved and email notifications, etc.
ASKER
I am confused that how I can start modify my programs now...
I'd suggest doing all your javascript calls in your html, and posting any details you need to store to your server. Running js with Java the way you are doing it will not work.
ASKER
how I can store the information so my server program can get them? Is there any web site I can study?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
a little bit of confused still..but working on it now...
ASKER
so I should use html to get the values I want and use POST/GET in my java program to have the values from html?
no you would POST/GET them from the javascript by making a call to your server
ASKER
I see...let me try~~
>>I am confused that how I can start modify my programs now...
The link i posted way back tells you all you need to know. You simply need to do this on the client in JavaScript and the link shows you how to use Ajax in this regard
The link i posted way back tells you all you need to know. You simply need to do this on the client in JavaScript and the link shows you how to use Ajax in this regard
ASKER
I tried to copy and paste the codes in the link aboved...and I kept having some error messages on the following line..
var mygetrequest=new ajaxRequest()
mygetrequest.onreadystatec hange=func tion(){
if (mygetrequest.readyState== 4){
document.getElementById("r esult").in nerHTML=my getrequest .responseT ext
}
} <-- error message here...said need an object
Any idea what's wrong?
var mygetrequest=new ajaxRequest()
mygetrequest.onreadystatec
if (mygetrequest.readyState==
document.getElementById("r
}
} <-- error message here...said need an object
Any idea what's wrong?
ASKER
Never mind...I got what's wrong....still working on the example~
Using a general API is not going to help you. Use the *specific* location API at the link i provided
In case there may be some confusion the AJAX code recently posted (see http:#a24783401) is not an API. It isn't Google's either. I am actually not really sure why it is needed. You need these results in your Java code, right? Have you tried just a basic test (i.e. a Google Search API) to see if your Java will get the response? I wouldn't spend lots of time on details of this until you verify the basics will work. Because of the way the Google API works I am afraid you won't be able to use it like you want.
This was mentioned earlier but you do know the ClientLocation results may not work and aren't very specific? I just mention this because if you have to always get a location or need it to be more accurate than it is designed then you might as well stop now. Google's Client Location does have some limitations.
Let me know if you have a question or need more info.
bol
This was mentioned earlier but you do know the ClientLocation results may not work and aren't very specific? I just mention this because if you have to always get a location or need it to be more accurate than it is designed then you might as well stop now. Google's Client Location does have some limitations.
Let me know if you have a question or need more info.
bol
the code used to call from java was never going to work as it was trying to access vars from the html
ASKER
OK...I quit my current program and start a new one using Javascript to get user's location as you guys suggested above. Just delete the old program and start a new one since I was messed up my program already.
Is there any way that I can know the exact location without keyin by users?
Is there any way that I can know the exact location without keyin by users?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oops ... instead of CurrentLocation it should probably be ClientLocation. I think the point is still clear but don't want to confuse this.
bol
bol
ASKER
to b0lsc0tt,
I copied and pasted your code aboved, and I got this error message...said "Line: 36 The object does not support this attribute or method" (I am not sure what exact this error message is in English) And line 36 is line 8 in the code aboved.
what should I do?
I copied and pasted your code aboved, and I got this error message...said "Line: 36 The object does not support this attribute or method" (I am not sure what exact this error message is in English) And line 36 is line 8 in the code aboved.
what should I do?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!--
Copyright 2008 Google Inc.
Licensed under the Apache License, Version 2.0:
http://www.apache.org/licenses/LICENSE-2.0
-->
<!-- The HTML 4.01 Transitional DOCTYPE declaration-->
<!-- above set at the top of the file will set -->
<!-- the browser's rendering engine into -->
<!-- "Quirks Mode". Replacing this declaration -->
<!-- with a "Standards Mode" doctype is supported, -->
<!-- but may lead to some differences in layout. -->
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- -->
<!-- Consider inlining CSS to reduce the number of requested files -->
<!-- -->
<link type="text/css" rel="stylesheet" href="XeniaNet.css">
<!-- -->
<!-- Any title is fine -->
<!-- -->
<title>Web Application Starter Project</title>
<!-- -->
<!-- This script loads your compiled module. -->
<!-- If you add any GWT meta tags, they must -->
<!-- be added before this line. -->
<!-- -->
<script type="text/javascript" language="javascript" src="xenianet/xenianet.nocache.js"></script>
<script src="http://maps.google.com/maps?file=api&v=2&sensor=true_or_false&key=ABQIAAAAfY2iSWaI4XX7aNUo-orecBQqqM2Hu6GCM5KAICnzfaDmWdVsMBT6njvj6tjPmPnRX0ayG5_cz81O0w" type="text/javascript"></script>
<script>
google.load("maps", "2", {callback: initialize});
function initialize() {
// Initialize default values
var zoom = 3;
var latlng = new google.maps.LatLng(37.4419, -100.1419);
var location = "Showing default location for map.";
// If ClientLocation was filled in by the loader, use that info instead
if (google.loader.ClientLocation) {
zoom = 13;
latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
location = "Showing IP-based location: <b>" + getFormattedLocation() + "</b>";
}
document.getElementById("location").innerHTML = location;
var map = new google.maps.Map2(document.getElementById('map'));
map.setCenter(latlng, zoom);
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
}
ASKER
now I removed the original html and copied your code and I got error message on line 4 said "must have '}'"
ASKER
ok..I figured out what's wrong...need this line <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> in my <head> section.
ASKER
so now I need to use POST/GET to communicate with my server program (in java)?
So does the page I provided work now just as an html page opened up in your browser? Make sure you are testing it from your domain or as a "localhost" site. If it does work then great. Let me know if you need anything else as far as an html page that will do this.
>> so now I need to use POST/GET to communicate with my server program (in java)? <<
Isn't that back to what you decide to drop because it wasn't going to work? Don't really understand what you mean and it seems like that is a line you said earlier about stuff you now know won't give the result you wanted. Let me know if you think I have misread this. Also if you are asking for help with that then please be more specific. The sentence seems like a statement except it ends with a question mark. I am not real sure what the question is.
bol
>> so now I need to use POST/GET to communicate with my server program (in java)? <<
Isn't that back to what you decide to drop because it wasn't going to work? Don't really understand what you mean and it seems like that is a line you said earlier about stuff you now know won't give the result you wanted. Let me know if you think I have misread this. Also if you are asking for help with that then please be more specific. The sentence seems like a statement except it ends with a question mark. I am not real sure what the question is.
bol
> Isn't that back to what you decide to drop because it wasn't going to work?
no, the original approach was trying to access the js from the java code.
you need to do it the other way round, ie your js call the server
no, the original approach was trying to access the js from the java code.
you need to do it the other way round, ie your js call the server
ASKER
to b0lsc0tt,
the html works...now i need to move on to the next step...Thanks!!!
to objects,
what do you mean by the other way? so calling my server from my js means using POST and GET? Just wanna make sure before I move on..
the html works...now i need to move on to the next step...Thanks!!!
to objects,
what do you mean by the other way? so calling my server from my js means using POST and GET? Just wanna make sure before I move on..
> so calling my server from my js means using POST and GET?
yes
yes
ASKER
ok...thanks everyone~~~~
xenia27, why did you mark http:#24782809 as the accepted answer? It's not relevant to your requirement and is an approach you correctly rejected ...
ASKER
mMmm...it helps me how to communicate with my server program (Java)...that's why I accepted that answer.
How? Please post code