获取GPS位置时,请加载其他URL

时间:2010-09-22 07:30:08

标签: java android gps android-webview

我有一点问题......我制作了一个扩展webview的Android应用程序。 webview Html页面上有这样的地图:Map example,它也在这里我得到了灵感。我的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。它保留了orignal网站“地图图像”,我想它会“卸载”页面,并且2.当第二个网址是加载,它需要相当长的时间才能完成,当我在我的HTC Desire上测试时,它有时不会显示第二个加载的页面(带有位置的地图),然后关闭并锁定屏幕,或者如果我去在应用程序中,这有时也有帮助...

希望你能提供帮助:)

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

以下是解决方案...... GPS听众显然不止一次播出;)因此,当GPS找到位置时,它会加载网址,然后停止广播。

之前的问题是,它将大量的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();
        }

以下是该应用程序的SourceCode:

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

        }

    }