Weitergabe der GPS-LonLat von Android-GPS-zur Webseite (javascript)
Ich habe den folgenden code, sondern die lon/lat scheint zu sein, die null zurückgeben;
package com.domain.www;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.Criteria;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebMapActivity extends Activity implements LocationListener {
private static final String MAP_URL = "http://www.yahoo.com/";
private WebView webView;
private Location mostRecentLocation;
@Override
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getLocation();
setupWebView();
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} catch (Exception e) {
e.printStackTrace();
}
}
/** Sets up the WebView object and loads the URL of the page **/
private void setupWebView() {
/*
final String centerURL = "javascript:centerAt("
+ mostRecentLocation.getLatitude() + ","
+ mostRecentLocation.getLongitude() + ")"; */
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url)
{
String jsCode = "javascript:alert('" + getMostRecentLocation("lon") + "')";
webView.loadUrl(jsCode.toString());
}
});
webView.loadUrl(MAP_URL);
}
/**
* The Location Manager manages location providers. This code searches for
* the best provider of data (GPS, WiFi/cell phone tower lookup, some other
* mechanism) and finds the last known location.
**/
private void getLocation() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locationManager.getBestProvider(criteria, true);
//In order to make sure the device is getting location, request
//updates. locationManager.requestLocationUpdates(provider, 1, 0,
//this);
//locationManager.requestLocationUpdates(provider, 1, 0, this);
setMostRecentLocation(locationManager.getLastKnownLocation(provider));
}
/** Sets the mostRecentLocation object to the current location of the device **/
@Override
public void onLocationChanged(Location location) {
setMostRecentLocation(location);
}
/**
* The following methods are only necessary because WebMapActivity
* implements LocationListener
**/
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
/**
* @param mostRecentLocation the mostRecentLocation to set
*/
public void setMostRecentLocation(Location mostRecentLocation) {
this.mostRecentLocation = mostRecentLocation;
}
/**
* @return the mostRecentLocation
*/
public double getMostRecentLocation(String lonLat) {
double gValue = 0.00;
try
{
Location xx = this.mostRecentLocation; //Used as a breakpoint
if(lonLat == "lat") gValue = this.mostRecentLocation.getLatitude();
if(lonLat == "lon") gValue = this.mostRecentLocation.getLongitude();
} catch (Exception e) {
e.printStackTrace();
}
return gValue;
}
}
Ignorieren Sie die URL-bit, das wäre eine Seite mit einer google map, auf der es; aber zu debugging-Zwecken würde ich es vorziehen, es einfach ausspucken eine Benachrichtigung mit den Werten.
Irgendwelche Ideen, was falsch? Ziel Emulatoren (Google API 1.5-API(3))
Dank
InformationsquelleAutor HappyGuy | 2010-02-28
Du musst angemeldet sein, um einen Kommentar abzugeben.
Den Grund für die 0,0 Längen-und Breitengrad, ist, dass Sie nicht über einen Längen-und Breitengrad. Sie haben nichts getan, Sie in Ihrem code, um eine Lage -
getLastKnownLocation()
funktioniert nur, wenn es etwas anderes in deinem code anfordern Standort.Hier ist ein Beispielprojekt das tut, was Sie wollen, entweder durch ziehen (a la Mr. Nurik ' s
addJavascriptInterface()
Empfehlung), oder durch drücken (mit derjavascript:
notation).InformationsquelleAutor CommonsWare
Die Sie wahrscheinlich verwenden wollen
WebView#addJavascriptInterface
statt ladenjavascript:
URLs, wie es werden viel sauberer.Im Grunde, was Sie tun müssen, ist:
WebView
mitaddJavascriptInterface
(hier bieten Sie eine Globale variable name auf den Einsatz in Javascript).addJavascriptInterface
, und verwenden Sie die Ergebnisse, wie Sie möchten.Weitere, siehe dieser Frage auf Stack Overflow.
InformationsquelleAutor Roman Nurik
Wenn die Lage ist null
Wenn getLastKnownLocation oder getLastKnownBestLocation gibt null zurück, dann sollten Sie in Erwägung ziehen, Ihren build-Ziel von "Android 1.5" zu "Google-API-1.5'. Das löste das Problem für mich.
Ändern können Sie das Build-Ziel für Ihr Projekt zu jeder Zeit in Eclipse: Rechts-klicken Sie das Projekt im Paket-Explorer, wählen Sie Eigenschaften - > Android und überprüfen Sie dann "Google API" für das Projekt-Ziel.
-
Wenn die Lage ist 0,0
Wenn getLastKnownLocation Renditen von 0,0 nach drücken der 'Senden' - button in DDMS' Ort-Kontrollen, die Sie wahrscheinlich sind das arbeiten mit nicht-englischen Kultur Einstellungen. Es ist ein emulator bug behoben werden mit dem kommenden release 7 der SDK-tools; für eine schnelle Lösung können Sie Ihre locale auf "en". (Siehe http://code.google.com/p/android/issues/detail?id=915 für details).
Lesen Ihren Kommentar zu Roman Nurik ich denke das SDK Wanze mit nicht-englischen Kultur Einstellungen ist beunruhigend Sie. Ändern Sie Ihre Sprache-Einstellungen in Eclipse oder Ihr Betriebssystem, und Sie werden in Ordnung sein. Als alternative, können Sie eine telnet-Sitzung zu senden Längen-und Breitengrad.
InformationsquelleAutor Patrick de Kleijn