u4819210 / social-networks-for-fun

Automatically exported from code.google.com/p/social-networks-for-fun
0 stars 0 forks source link

Javascript to get current device location for iPhone #56

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hi John,

When you get time, could you please help me to investigate if it's possible to 
get current user location through javascript interface. 

For Android, I can define a java class as below,

    public class SocialRaysJavascriptInterface {
        public Double getLatitude() {
            Location location = lm.getLastKnownLocation(application
                    .getBestLocationProvider());
            if (location != null)
                return location.getLatitude();
            else
                return null;
        }

        public Double getLongitude() {
            Location location = lm.getLastKnownLocation(application
                    .getBestLocationProvider());
            if (location != null)
                return location.getLongitude();
            else
                return null;
        }

        public int getWidth() {
            return web.getWidth();
        }

    }

And before I launch the web view, I can enable the javascript interface

    webView.addJavascriptInterface(new SocialRaysJavascriptInterface(), "SocialRays");

Now in my html web page, I can use javascript "SocialRays.getLatitude()" to get 
user current location.

Please help me check if it's doable on iPhone. It's not urgent. I have not got 
there yet. Even there is problem, I can still use the location which is 
reported every two minutes although it might not be the user current exact 
location.

Thanks,
Cheng

Original issue reported on code.google.com by zhangyon...@gmail.com on 22 Nov 2010 at 4:50

GoogleCodeExporter commented 8 years ago
Hi John, 

I found one article at 
http://mobileorchard.com/phonegap-use-the-iphone-sdks-features-from-html-and-jav
ascript/ .

It seems that it's doable to call native objecttive c code from javascript.

Thanks,
Cheng

Original comment by zhangyon...@gmail.com on 26 Nov 2010 at 4:08

GoogleCodeExporter commented 8 years ago
John, 

Please let me know your plan so that I could have some idea about our release 
plan. 

Thanks,
Cheng

Original comment by zhangyon...@gmail.com on 27 Nov 2010 at 8:47

GoogleCodeExporter commented 8 years ago
Your choices is either the standard location service (e.g. 
startUpdatingLocation, for which you can specify the desiredAccuracy), or the 
low-power significant change service (e.g., 
startMonitoringSignificantLocationChanges). But you generally don't specify 
location services based upon hardware, but rather on the basis of your app's 
functional needs. Use the standard service if you need an accurate location, 
and try using the significant change service if you don't need the same level 
of precision.

See the Location Awareness Guide for more information and Reference for mobile 
application development resource:
http://www.agileinfoways.com/technical-expertise/mobile-applications-development
/iphone/

But if you need a cross-platform; here you go! This small javascript library is 
cross-platform and supports all modern smartphones out of the box:

http://code.google.com/p/geo-location-javascript/

Here is how you use it:

//determine if the handset has client side geo location capabilities
if(geo_position_js.init()){
   geo_position_js.getCurrentPosition(success_callback,error_callback);
}else{
   alert("Functionality not available");
}

Original comment by naresh.r...@agileinfoways.com on 30 May 2014 at 11:57