Skip to content

Instantly share code, notes, and snippets.

@banshee
Created July 5, 2012 23:47
Show Gist options
  • Save banshee/3057175 to your computer and use it in GitHub Desktop.
Save banshee/3057175 to your computer and use it in GitHub Desktop.
for stackoverflow question
/***
Copyright (c) 2008-2012 CommonsWare, LLC
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.
From _The Busy Coder's Guide to Advanced Android Development_
http://commonsware.com/AdvAndroid
*/
package com.commonsware.android.geoweb;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class GeoWebOne extends Activity {
private static String PROVIDER = LocationManager.GPS_PROVIDER;
private WebView browser;
private LocationManager myLocationManager = null;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
browser = (WebView) findViewById(R.id.webkit);
myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
browser.getSettings().setJavaScriptEnabled(true);
browser.addJavascriptInterface(new Locater(), "locater");
browser.loadUrl("file:///android_asset/geoweb1.html");
browser.setWebChromeClient(new WebChromeClient() {
public boolean onConsoleMessage(ConsoleMessage cm) {
Log.d("MyApplication",
cm.message() + " -- From line " + cm.lineNumber() + " of "
+ cm.sourceId());
return true;
}
});
browser.loadUrl("javascript:console.log(locater);");
}
@Override
public void onResume() {
super.onResume();
myLocationManager.requestLocationUpdates(PROVIDER,
10000,
100.0f,
onLocation);
browser.loadUrl("javascript:console.log(locater);");
}
@Override
public void onPause() {
super.onPause();
myLocationManager.removeUpdates(onLocation);
}
LocationListener onLocation = new LocationListener() {
public void onLocationChanged(Location location) {
// ignore...for now
}
public void onProviderDisabled(String provider) {
// required for interface, not used
}
public void onProviderEnabled(String provider) {
// required for interface, not used
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// required for interface, not used
}
};
public class Locater {
public String getLocation() throws JSONException {
Location loc = myLocationManager.getLastKnownLocation(PROVIDER);
if (loc == null) {
return (null);
}
JSONObject json = new JSONObject();
json.put("lat", loc.getLatitude());
json.put("lon", loc.getLongitude());
return (json.toString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment