0

我有一个小问题......我制作了一个扩展 webview 的 Android 应用程序。带有这样地图的 webview Html 页面:地图示例,也是在这里我得到了灵感。我的 onCreate 方法如下所示:

super.onCreate(savedInstanceState);

         //Removes the title bar in the application
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         setContentView(R.layout.main);
         
         //Creation of the Webview found in the XML Layout file
         browserView = (WebView)findViewById(R.id.webkit);

         //Removes both vertical and horizontal scroll bars 
         browserView.setVerticalScrollBarEnabled(false);
         browserView.setHorizontalScrollBarEnabled(false);
         
        
         myLocationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
         //Enable Javascripts
      
         url = "http://www.my-homepage.dk/map_example.html";
         
         browserView.getSettings().setJavaScriptEnabled(true);

         
         //The website which is wrapped to the webview
        
         browserView.loadUrl(url);

因此,当我的应用程序获取 GPS 位置时,它会调用此方法:

LocationListener onLocationChange=new LocationListener() {
 
 
  public void onLocationChanged(Location location) {
 
 
 StringBuilder buf=new StringBuilder(url);
 
 buf.append("?");
 buf.append("lon=");
 buf.append(String.valueOf(location.getLongitude()));
 buf.append("&");
 buf.append("lat=");
 buf.append(String.valueOf(location.getLatitude()));
     
    browserView.loadUrl(buf.toString());
 
 }

所以它基本上只是加载另一个 URL.... 但是,我的问题是,1. 它保留原始网站“地图图像”,我想它会“卸载”页面,以及 2. 当第二个 url 加载时,它它需要很长时间才能完成,当我在我的 HTC Desire 上进行测试时,它有时在关闭屏幕并锁定之前不显示第二个加载页面(带有位置的地图),或者如果我出去和进来该应用程序,有时也有帮助...

希望你能帮忙:)

4

2 回答 2

1

一个建议 - 在 onLocationChanged 中,make locationFound();第一个语句而不是最后一个语句。

最好立即停止侦听器,因为 loadUrl 语句可能需要一些时间才能完成,而提供者可能会发送更多更新。

于 2010-12-16T20:51:11.137 回答
0

这是解决方案...... GPS 监听器显然不止一次广播;)所以,当 GPS 找到一个位置时,它会加载 url,然后停止广播。

所以之前的问题是,它将大量的 URL 发送到 html 页面,因此从不只加载 1 个单个 url。我只是稍微简化了 onLocationChanged。

   public void locationFound(){

        myLocationManager.removeUpdates(this);

    }

        @Override
        public void onLocationChanged(Location location) {

            String lon = "lon="+String.valueOf(location.getLongitude());
            String lat = "lat="+String.valueOf(location.getLatitude());


            browserView.loadUrl(url+"?"+lon+"&"+lat);
            locationFound();
        }

这是应用程序的源代码:

package com.webview;



import android.app.Activity;
import android.content.Context;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;




public class WebViewTest extends Activity implements LocationListener{

    private WebView browserView;
     private static String PROVIDER="gps";
     private LocationManager myLocationManager=null;
     private String url;
     private boolean LocFound = false;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         //Removes the title bar in the application
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         setContentView(R.layout.main);

         //Creation of the Webview found in the XML Layout file
         browserView = (WebView)findViewById(R.id.webkit);

         //Removes both vertical and horizontal scroll bars 
         browserView.setVerticalScrollBarEnabled(false);
         browserView.setHorizontalScrollBarEnabled(false);


         myLocationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
         //Enable Javascripts

         url = "http://www.test.dk/test.html";

         browserView.getSettings().setJavaScriptEnabled(true);


         //The website which is wrapped to the webview

        browserView.loadUrl(url);


          }

    @Override
    public void onResume() {
    super.onResume();
    myLocationManager.requestLocationUpdates(PROVIDER, 0,
    0,
    this);
    }

    @Override
    public void onPause() {
    super.onPause();
    myLocationManager.removeUpdates(this);
    }

    public void locationFound(){

        myLocationManager.removeUpdates(this);

    }

        @Override
        public void onLocationChanged(Location location) {

            String lon = "lon="+String.valueOf(location.getLongitude());
            String lat = "lat="+String.valueOf(location.getLatitude());


            browserView.loadUrl(url+"?"+lon+"&"+lat);
            locationFound();
        }

        @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

        }

    }
于 2010-09-22T11:58:27.327 回答