纬度和经度总是得到零android但在logcat我得到当前位置

时间:2012-08-03 07:50:15

标签: android geolocation

我已完成编码,当我按下按钮

时,我会收到当前位置的邮件

这是代码

     LocationManager mlocManager =                                           (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
    //mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 5.0f, new MyLocationListener());
    LocationListener mlocListener = new MyLocationListener();
    //mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000,1000,mlocListener);
    /*if(mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000,1000,mlocListener);
    }
    else {

    }*/
    Criteria criteria = new Criteria();
    String provider = mlocManager.getBestProvider(criteria, true);
    if(provider!=null) {
        Location location = mlocManager.getLastKnownLocation(provider);
        if(location!=null) {
            lat = Integer.parseInt(location.getLatitude()+"");
            lang = Integer.parseInt(location.getLongitude()+"");
    }
    else
    {
        mlocManager.requestLocationUpdates(provider, 1, 0, mlocListener);
    }    

现在发送邮件

   class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        lat = Integer.parseInt(location.getLatitude()+"");
        lang = Integer.parseInt(location.getLongitude()+"");


                SendMail sender = new SendMail(mContext.getResources().getString(R.string.username), mContext.getResources().getString(R.string.password));


                try {
                    sender.sendMail(
                            "The Location is ",
                            " Latitude :- " + lang  + 
                         "\n Longitude :-"  + lat,
                            "xxxxx@gmail.com",
                            "xxxx@gmail.com");
                } catch (Exception e) {
                    e.printStackTrace();
                }

             }

现在我收到了邮件,但零纬度和零经度

现在我已经检查了logcat,我正在获取位置

    08-03 13:14:45.339: D/libloc(221): Event RPC_LOC_EVENT_PARSED_POSITION_REPORT (client 0)
    08-03 13:14:45.339: D/libloc(221): Session status: RPC_LOC_SESS_STATUS_IN_PROGESS   Valid mask: 0x6069
    08-03 13:14:45.339: D/libloc(221): Latitude:  22.2790718 (intermediate)
    08-03 13:14:45.339: D/libloc(221): Longitude: 70.7710290
    08-03 13:14:45.339: D/libloc(221): Accuracy: 0.0000000
    08-03 13:14:45.339: D/libloc(221): loc_eng_deferred_action_thread signalled
    08-03 13:14:45.339: D/libloc(221): loc_eng_deferred_action_thread event 1
    08-03 13:14:45.339: D/libloc(221): loc_eng_process_loc_event: 1
    08-03 13:14:45.339: D/libloc(221): loc_eng_deferred_action_thread. waiting for events
    08-03 13:14:45.469: D/libloc(221): Event RPC_LOC_EVENT_NMEA_1HZ_REPORT (client 0)
    08-03 13:14:45.469: D/libloc(221): loc_eng_deferred_action_thread signalled

所以请任何人告诉我如何获得当前位置

1 个答案:

答案 0 :(得分:0)

从你的代码中我发现你已经写了以下几行,

lat = Integer.parseInt(location.getLatitude()+"");
lang = Integer.parseInt(location.getLongitude()+""); 

location.getLagetLatitude()将写入double值,您将以整数形式解析。

我建议你把lat,lon变量作为double,你的问题就会解决。