首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从Android GPS向WebPage传递GPS LonLat (javascript)

从Android GPS向WebPage传递GPS LonLat (javascript)
EN

Stack Overflow用户
提问于 2010-03-01 05:10:38
回答 3查看 14.3K关注 0票数 4

我有以下代码,但lon/lat似乎返回null;

代码语言:javascript
运行
复制
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;  
    }

}

忽略URL位,因为它是一个上面有google地图的页面;但出于调试目的,我更喜欢它只显示一个带有值的警告。

你知道怎么回事吗?目标模拟器(Google API 1.5 API(3))

谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-03-01 06:35:04

使用0,0纬度和经度的原因是您没有纬度或经度。您在代码中没有做任何获取位置的操作--只有当您的代码中有其他东西请求位置更新时,getLastKnownLocation()才会起作用。

通过拉动(类似于努里克先生的addJavascriptInterface()推荐)或推送(使用javascript:表示法),Here is a sample project可以执行您想要的操作。

票数 8
EN

Stack Overflow用户

发布于 2010-03-01 05:57:07

您可能希望使用WebView#addJavascriptInterface而不是加载javascript: URL,因为这样会更干净。

基本上,你想要做的是:

  1. 创建一个具有返回位置数据的方法的类。
  2. 创建该类的实例并使用addJavascriptInterface将其传递给WebView (在此处,您将提供要在页面中的Javascript中使用的全局变量名,在addJavascriptInterface定义的全局变量上调用该方法,并根据需要使用结果。

有关更多信息,请参阅this question on Stack Overflow

票数 2
EN

Stack Overflow用户

发布于 2010-07-02 10:02:07

如果位置为空,则为

如果getLastKnownLocation或getLastKnownBestLocation返回null,那么您应该考虑将构建目标从‘Android1.5’更改为'Google API1.5‘。这为我解决了问题。

您可以随时在Eclipse中更改项目的构建目标:右键单击Package Explorer中的项目,选择Properties > Android,然后选中project Target的'Google API‘。

-

如果位置为0.0,则为

如果在按下DDMS位置控件中的“发送”按钮后getLastKnownLocation返回0.0,则您可能正在使用非英语区域性设置。这是一个模拟器错误,已在即将发布的SDK工具版本7中修复;为了快速修复,您可以将您的区域设置更改为'en‘。(详细信息请参见http://code.google.com/p/android/issues/detail?id=915。)

读了你对罗曼·努里克的评论,我认为非英语文化设置的SDK bug正在困扰着你。在Eclipse或您的操作系统中更改您的语言设置,您就没有问题了。作为替代方案,您可以使用telnet会话发送经度和纬度。

代码语言:javascript
运行
复制
telnet localhost:5554
geo fix 52.383144 5.217133
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2352679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档