Javascript Android Webview不规则加载问题

Javascript Android Webview不规则加载问题,javascript,android,function,webview,loading,Javascript,Android,Function,Webview,Loading,嗨,我想解释一下下面的代码。在这里,我的javascript函数有时可以工作,有时不能。结果,我真的不知道问题出在哪里。在这里,我的index.html文件自动加载地图位置。然后我用当前lat,lng作为参数调用javascript函数。因此,它首先加载默认贴图。然后,映射应该被javascript函数覆盖。问题是有时会发生,有时不会。所以,我想得到一个关于这个的答案 public class MapOptionsDemoModified extends Activity{ //Geoc

嗨,我想解释一下下面的代码。在这里,我的javascript函数有时可以工作,有时不能。结果,我真的不知道问题出在哪里。在这里,我的index.html文件自动加载地图位置。然后我用当前lat,lng作为参数调用javascript函数。因此,它首先加载默认贴图。然后,映射应该被javascript函数覆盖。问题是有时会发生,有时不会。所以,我想得到一个关于这个的答案

public class MapOptionsDemoModified extends Activity{
    //Geocoder geocoder;
    WebView mWebView;
    LocationManager mlocManager=null;
    LocationListener mlocListener;
    private MyLocationOverlay myLocationOverlay;
    protected MapView map; 
    private RadioButton mapButton;
    private RadioButton satelliteButton;
    private ToggleButton trafficToggle;
    private ToggleButton labelsToggle;
    private Configuration config;
    EditText LOC;
    Button routesbtn,settingsbtn;
    public String location="State Street"
    double lati,longi;
    GeoPoint currentLocation;
    double curlat,curlong;
    InputMethodManager imm;
    //String adrs;
    double latitude=40.07546;
    double longitude=-76.329999;


    String adrs="";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_options_modified);
        lati=34.1161;
        longi=-118.149399;
        adrs="";
        routesbtn=(Button)findViewById(R.id.mom_bt2);

        mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
        mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);


        mWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("file:///android_asset/index.html");
        //mWebView.loadUrl("http://www.google.com");
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSupportZoom(true);
        imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);




        /* taking data from caller activity page */  
        // ###################Receiving data starts
        Bundle extras = getIntent().getExtras();
        if (extras == null) {
         return;
        }
        lati=extras.getDouble("latitude");
        longi = extras.getDouble("longitude");
        adrs=extras.getString("adrs");

        // ##### Receiving data ends


        AlertDialog.Builder pdb=new AlertDialog.Builder(MapOptionsDemoModified.this);          
        pdb.setTitle("GPS");
        pdb.setMessage(adrs+" "+Double.toString(lati)+" "+Double.toString(longi));
        pdb.setPositiveButton("Ok", null);
        pdb.show();


        mWebView.loadUrl("javascript:getCurrentLocation("+lati+","+longi+")");


        routesbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if(mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                    //while(j<cnt)
                    //{

                        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
                        if(MyLocationListener.latitude>0)
                        {
                            latitude=MyLocationListener.latitude;
                            longitude=MyLocationListener.longitude;
                            //flag=1;

                            Geocoder gcd = new Geocoder(MapOptionsDemoModified.this, Locale.getDefault());
                            List<Address> addresses;
                            try {
                                addresses = gcd.getFromLocation(latitude,longitude, 5);
                                if (addresses.size() > 0)
                                {
                                    adrs=addresses.get(0).getAddressLine(0)+" "+addresses.get(0).getAddressLine(1)+" "+addresses.get(0).getAddressLine(2);
                                }
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                            //break;

                            finish();
                            Intent i = new Intent(MapOptionsDemoModified.this,RoutesPageOne.class);
                            i.putExtra("adrs", adrs);
                            i.putExtra("latitude", latitude);
                            i.putExtra("longitude", longitude);
                            startActivity(i);




                        }
                        else
                        {

                            AlertDialog.Builder pdb=new AlertDialog.Builder(MapOptionsDemoModified.this);          
                            pdb.setTitle("GPS");
                            pdb.setMessage("GPS activation in progress");
                            pdb.setPositiveButton("Ok", null);
                            pdb.show();

                        }

                    //}

                }   
                else {

                    AlertDialog.Builder pdb=new AlertDialog.Builder(MapOptionsDemoModified.this);          
                    pdb.setTitle("GPS");
                    pdb.setMessage("GPS is not turned on..., please start gps");
                    pdb.setPositiveButton("Ok", null);
                    pdb.show();
                }

            }
        });




        LOC=(EditText)findViewById(R.id.mom_editText1);     
        LOC.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub
               location = LOC.getText().toString();

                if (keyCode == KeyEvent.KEYCODE_ENTER && location.length() > 0) {

                    LOC.setText("");

                    imm.hideSoftInputFromWindow(LOC.getWindowToken(), 0);

                    mWebView.loadUrl("javascript:getLocation('" + location + "')");


                    /*AlertDialog.Builder edb=new AlertDialog.Builder(MapOptionsDemoModified.this);          
                    edb.setTitle("gps");
                    edb.setMessage(location+" lat= "+lati+" long="+longi);
                    edb.setPositiveButton("Ok", null);
                    edb.show();*/



                    //searchBarcode(barcode);
                    return true;
                }

                return false;
            }

            });



        //imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        mWebView.setWebViewClient(new HelloWebViewClient());






    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
公共类映射选项DemoModified扩展活动{
//地理编码器;
网络视图;
LocationManager mlocManager=null;
LocationListener mlocListener;
私人我的位置我的位置;
受保护的地图视图;
私人无线电按钮;
专用无线按钮卫星按钮;
专用切换按钮流量切换;
私人切换按钮标签切换;
私有配置;
编辑文本LOC;
按钮路由BTN,设置SBTN;
公共字符串location=“State Street”
双纬、长纬;
地点定位;
双卷发,卷发;
输入方法管理器imm;
//字符串ADR;
双纬度=40.07546;
双经度=-76.329999;
字符串adrs=“”;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.map\u选项\u修改);
纬度=34.1161;
longi=-118.149399;
adrs=“”;
routesbtn=(按钮)findViewById(R.id.mom_bt2);
mlocManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
mlocListener=新的MyLocationListener();
mlocManager.RequestLocationUpdate(LocationManager.GPS_提供程序,0,0,mlocListener);
mlocManager.isProviderEnabled(LocationManager.GPS\U提供程序);
mWebView=(WebView)findViewById(R.id.WebView);
WebSettings WebSettings=mWebView.getSettings();
setJavaScriptEnabled(true);
mWebView.loadUrl(“file:///android_asset/index.html");
//mWebView.loadUrl(“http://www.google.com");
webSettings.SetBuilTinZoomControl(真);
webSettings.setSupportZoom(真);
imm=(InputMethodManager)getSystemService(Context.INPUT\u方法\u服务);
/*从调用方活动页获取数据*/
//开始接收数据
Bundle extras=getIntent().getExtras();
如果(附加==null){
返回;
}
纬度=额外的双倍(“纬度”);
longi=额外的getDouble(“经度”);
adrs=extras.getString(“adrs”);
//#####接收数据结束
AlertDialog.Builder pdb=新建AlertDialog.Builder(映射选项DemoModified.this);
pdb.setTitle(“GPS”);
pdb.setMessage(adrs+“”+Double.toString(lati)+“”+Double.toString(longi));
pdb.setPositiveButton(“确定”,空);
pdb.show();
loadUrl(“javascript:getCurrentLocation(“+lati+”,“+longi+”)”);
routesbtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(mlocManager.isProviderEnabled(LocationManager.GPS_提供程序)){
//while(j0)
{
纬度=MyLocationListener.latitude;
经度=MyLocationListener.longitude;
//flag=1;
Geocoder gcd=新的Geocoder(MapOptionsDemoModified.this,Locale.getDefault());
列出地址;
试一试{
地址=gcd.getFromLocation(纬度,经度,5);
如果(地址.size()>0)
{
adrs=addresses.get(0).getAddressLine(0)+''+addresses.get(0).getAddressLine(1)+''+addresses.get(0).getAddressLine(2);
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//中断;
完成();
意向i=新意向(MapOptionsDemoModified.this,RoutesPageOne.class);
i、 putExtra(“ADR”,ADR);
i、 putExtra(“纬度”,纬度);
i、 putExtra(“经度”,经度);
星触觉(i);
}
其他的
{
AlertDialog.Builder pdb=新建AlertDialog.Builder(映射选项DemoModified.this);
pdb.setTitle(“GPS”);
pdb.setMessage(“GPS激活进行中”);
pdb.setPositiveButton(“确定”,空);
pdb.show();
}
//}
}   
否则{
AlertDialog.Builder pdb=新建AlertDialog.Builder(映射选项DemoModified.this);
pdb.setTitle(“GPS”);
pdb.setMessage(“GPS未打开…,请启动GPS”);
pdb.setPositiveButton(“确定”,空);
pdb.show();
}
}
});
LOC=(编辑文本)findViewById(R.id.mom\u编辑文本1);
LOC.setOnKeyListener(新的OnKeyListener(){
@凌驾
公共布尔onKey(视图v、int keyCode、KeyEvent事件){
//TODO自动生成的方法存根
location=LOC.getText().toString();
如果(keyCode==KeyEvent.keyCode_输入