问题是android模拟器在Web View中显示地图

时间:2010-11-26 06:50:08

标签: android

我得到的是我的模拟器中的空白屏幕,并且地图不显示。以下是在浏览器中显示的内容:http://code.google.com/apis/maps/documentation/javascript/examples/layer-georss.html以下是清单文件的代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.apps.mylocations"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MyLocations"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >

    <WebView android:id="@+id/web_engine"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
            />
</LinearLayout>




 package com.apps.mylocations;

        import android.app.Activity;
        import android.os.Bundle;
        import android.webkit.WebView;

        public class MyLocations extends Activity {
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);


                WebView engine = (WebView) findViewById(R.id.web_engine);

                engine.getSettings().setJavaScriptEnabled(true);








                String data = "<html>" + 
        "<head>" + 
        "<meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\" />" + 
        "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"/>" + 
        "<title>Google Maps JavaScript API v3 Example: KmlLayer GeoRSS</title>" + 
        "<link href=\"http://code.google.com/apis/maps/documentation/javascript/examples/default.css\" rel=\"stylesheet\" type=\"text/css\" />" + 
        "<script type=\"text/javascript\" src=\"http://maps.google.com/maps/api/js?sensor=false\"></script>" + 
        "<script type=\"text/javascript\">" + 
        "function initialize() {" +
          "var myLatlng = new google.maps.LatLng(49.496675,-102.65625);" +
          "var myOptions = {" +
            "zoom: 4," +
            "center: myLatlng," +
            "mapTypeId: google.maps.MapTypeId.ROADMAP" +
          "}" +

          "var map = new google.maps.Map(document.getElementById(\"map_canvas\"), myOptions);" +

          "var georssLayer = new google.maps.KmlLayer('http://api.flickr.com/services/feeds/geo/?g=322338@N20&lang=en-us&format=feed-georss');" +
          "georssLayer.setMap(map);" +
        "}" +
        "</script>" + 
        "</head>" + 
        "<body onload=\"initialize()\">" + 
          "<div id=\"map_canvas\"></div>" + 
        "</body>" + 
        "</html>";


                engine.loadData(data, "text/html", "UTF-8");


            }
        }

3 个答案:

答案 0 :(得分:3)

您是否已按照现有问题的提示进行操作?

google map not showing after publishing android application?

Does anyone know why my maps only show grid

  • 清单中给出的互联网许可?
  • 地图密钥正确吗?

答案 1 :(得分:1)

您的代码:

engine.loadData(...)

不允许加载网络资源。因此,由于HTML代码段尝试从网络加载大量资源,因此根本不起作用。它可以显示的内容有限。查看文档:{​​{3}}

也许loadDataWithBaseURL会更好用吗?我不确定。我尝试加载你的代码,它使用了很多远程javascript,从静态/本地HTML字符串加载时可能会出现问题。

EDIT 12.05.2010:

如果您可以在某处(简单)访问服务器,则应该只发布HTML并在webview上使用loadUrl方法。准确的说明来自Google自己,使用Maps API文档,在这里:

http://developer.android.com/reference/android/webkit/WebView.html http://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/

标记

答案 2 :(得分:1)

您是否在需要代理才能访问互联网的网络中?如果是,则需要为模拟器设置代理。

http://developer.android.com/guide/developing/tools/emulator.html#proxy