当前位置:  开发笔记 > 编程语言 > 正文

将GPS LonLat从Android GPS传递到WebPage(javascript)

如何解决《将GPSLonLat从AndroidGPS传递到WebPage(javascript)》经验,为你挑选了1个好方法。

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

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 API 1.5 API(3))

谢谢



1> CommonsWare..:

0,0纬度和经度的原因是您没有纬度或经度.您在代码中没有做任何事情来获取位置 - getLastKnownLocation()只有在代码中有其他内容请求位置更新时才有效.

这是一个示例项目,可以通过拉动(根据Nurik先生的addJavascriptInterface()建议)或推动(使用javascript:符号)来完成您想要的任务.

推荐阅读
郑谊099_448
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有