0

我已经完成了编码,当我按下按钮时,我会收到当前位置的邮件

这是代码

     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

所以请谁能告诉我如何获得这个当前位置

4

1 回答 1

0

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

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

location.getLagetLatitude()将写入您以整数形式解析的双精度值。

我建议您将 lat,lon 变量作为double,您的问题将得到解决。

于 2012-08-03T08:17:07.327 回答