片段中没有得到纬度和经度

时间:2015-04-10 19:24:50

标签: android google-maps android-fragments

我的代码有问题,在片段中我有这段代码:

public class Logo extends Fragment implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {


LocationManager lm;
Location mLastLocation;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private Location mCurrentLocation;
private TextView Lat;
private TextView Long;
String provider;

public Logo() {

}
/**
 * Returns a new instance of this fragment for the given section
 * number.
 */
public static Logo newInstance() {
    Logo fragment = new Logo();

    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    lm = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
    View view = inflater.inflate(R.layout.fragment_main, container, false);
        Lat = (TextView) view.findViewById(R.id.Latitude);
    Long = (TextView) view.findViewById(R.id.Longitude);
    TextView Morad = (TextView) view.findViewById(R.id.Morada);
    Criteria c=new Criteria();
    provider=lm.getBestProvider(c, false);

    mLastLocation=lm.getLastKnownLocation(provider);
    Lat.setText("A obter");
    Long.setText(" dados");
    Morad.setText("Aguarde...");
    if(mLastLocation!=null)
    {
        Lat.setText(String.valueOf(mLastLocation.getLatitude()));
        Long.setText(String.valueOf(mLastLocation.getLongitude()));
    }
    else
    {
        Lat.setText("No connection");
        Long.setText(" wait");
    }
    return view;
}


@Override
public void onConnected(Bundle bundle) {
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
    if (mLastLocation != null) {
        Lat.setText(String.valueOf(mLastLocation.getLatitude()));
        Long.setText(String.valueOf(mLastLocation.getLongitude()));
    }
}

但永远不会得到拉特和经度值,我错过了什么。 我只想返回纬度和经度值并放入2个filds。

1 个答案:

答案 0 :(得分:0)

您需要决定是否要使用内置的LocationManager或Google Play服务位置APi,因为您尝试使用这两种服务并且无法使用。

如果您尝试使用内置版本,那么您永远不会获得位置,因为您没有最后一个位置,并且您从未请求位置更新。

如果您正在尝试使用Google Play服务位置API,那么您需要做更多的工作,因为您甚至没有真正实现它。我想真的在这两种情况下你仍然有更多的工作,因为你真的没有正确实现