2010-09-22 4 views
0

J'ai un petit problème ... J'ai fait une application Android qui étend le webview. La page Webview Html avec une carte comme ceci: Map example, c'était aussi ici que j'ai eu mon inspiration. Ma méthode onCreate ressemble à ceci:Lors de l'obtention de la localisation GPS, charger une autre URL

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); 

Alors, quand mon application obtient une position GPS, il appelle cette méthode:

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()); 

} 

Il charge fondamentalement juste une autre URL .... Mais, mon problème est, 1. il garde le site web original "image de la carte", j'ai imaginé qu'il "décharger" la page, et 2. Lorsque le second url est chargé, il faut beaucoup de temps avant qu'il soit fini, et quand je teste sur mon HTC Desire, il ne montre parfois pas la deuxième page chargée (la carte avec l'emplacement) avant qu'il éteigne l'écran et se verrouille, ou si je sors et dans l'application, cela aide parfois trop ...

Espoir vous pouvez aider :)

+0

Vous pouvez essayer browserView.clearView() avant de charger une nouvelle URL. – bhups

+0

Cela ne fonctionne pas ... :( – Roskvist

Répondre

1

Une suggestion - à OnLocationChanged, make locationFound(); la première déclaration au lieu de la dernière.

Il serait bon d'arrêter immédiatement l'écouteur, car l'instruction loadUrl pourrait prendre un certain temps à se terminer, alors que le fournisseur pourrait envoyer plus de mises à jour.

+0

Super idée :) merci – Roskvist

0

Voici la solution ... L'auditeur GPS apparemment diffusé plus d'une fois;) donc, lorsque le GPS trouve un emplacement, il charge l'URL et arrête ensuite la diffusion.

Donc le problème était avant, qu'il a envoyé des charges d'URL à la page html, et donc jamais simplement chargé 1 seule URL. Et j'ai juste simplifié un peu le 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(); 
     } 

Voici le code source de l'application:

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 

     } 

    } 
Questions connexes