【问题标题】:android google maps on a webview [duplicate]webview上的android谷歌地图[重复]
【发布时间】:2012-03-23 20:17:54
【问题描述】:

我有一个 jqueryMobile 应用程序,它使用 google maps api 并且在 iOS 上正常工作。但是,我无法让它在 android 上运行。我在清单文件上设置了以下权限。文件已正确加载,但我无法查看地图!假设 jqmobile 代码工作正常,因为它适用于 iOS,如何启用它或需要哪些步骤?谢谢。

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />

我的 webview 类看起来像,

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);

        browse4 = (WebView) findViewById(R.id.webview1);
        browse4.getSettings().setJavaScriptEnabled(true);
        browse4.getSettings().setUseWideViewPort(true);
        browse4.getSettings().setLoadWithOverviewMode(true);
        browse4.getSettings().setBuiltInZoomControls(true);
        browse4.getSettings().setSupportZoom(true);

        browse4.loadUrl("file:///data/data/" + PACKAGE_NAME + "/files/" + "myMap.html");

    }

【问题讨论】:

  • 如何将 html 文件放入文件目录?也许它在那个过程中被破坏了?尝试放入一个仅包含一些文本的纯 html 页面,然后查看 WebView 是否以这种方式显示您的 html。
  • html 没有错误。再次检查。还使用 android 和 iOS 从远程服务器进行了测试,并且适用于 iOS。

标签: java android google-maps android-maps


【解决方案1】:

您不使用 Google MapView 有什么特别的原因吗?

我知道它使用的是 jQuery,但这个 可能更容易。

【讨论】:

  • 是的,由于某些原因,我需要使用 webView。应该是一件容易的事......确实是在 iOS 上,但这个问题让我在 android 上发疯......
  • 先运行setJavaScriptEnabled(true)就好了,不过可能和jqmobile有关。如果它是 iOS 特定的,它是什么?这可能是 javascript 中的错误,导致页面的其余部分无法正确初始化。
  • 问题的解决方案是什么?我需要移植我的以 mapView 为中心的 android 应用程序以使用加载了谷歌地图的 webView,以便我可以尝试将我的 APK 重新打包为黑莓 BAR。
  • @twig 因为我们不必包含整个 androidx 库和谷歌地图库,它们会减慢构建速度并使生成的 apk 膨胀
【解决方案2】:

可能是一个愚蠢的答案,但你的清单中有这个吗?

<uses-library android:name="com.google.android.maps" />

【讨论】:

  • 值得一试,我知道我以前犯过这个错误
【解决方案3】:

好的,试试这个

使用 Google API 创建一个新项目并尝试一下。

package com.ab1209.webview;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
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://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/simple-android-map.html";
    private WebView webView;

    Location mostRecentLocation;

    @Override
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getLocation();
        setupWebView();
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    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 the location, request
        // updates.
        locationManager.requestLocationUpdates(provider, 1, 0, this);
        mostRecentLocation = locationManager.getLastKnownLocation(provider);
    }

    @Override
    public void onLocationChanged(Location arg0)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
        // TODO Auto-generated method stub

    }

    /** 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);
        // Wait for the page to load then send the location information
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl(MAP_URL);
        /** Allows JavaScript calls to access application resources **/
        webView.addJavascriptInterface(new JavaScriptInterface(), "android");
    }

    /**
     * Sets up the interface for getting access to Latitude and Longitude data
     * from device
     **/
    private class JavaScriptInterface
    {
        public double getLatitude()
        {
            return mostRecentLocation.getLatitude();
        }

        public double getLongitude()
        {
            return mostRecentLocation.getLongitude();
        }
    }
}

也请检查this。我遇到了同样的问题,现在已经解决了。

【讨论】:

    【解决方案4】:

    您不能这样访问该文件。您需要将它放在您的assets 文件夹中,然后使用file:///android_asset/file_name.html 访问它。如果您有 root 访问权限,则只能直接访问 /data/data 文件夹。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多