从3G / WiFi获取坐标

时间:2014-10-08 09:39:45

标签: java android gps

在我的应用中,我有一个对话框。单击“是”时,它会发送带有纬度和经度的HTTP请求作为参数。截至目前,该应用程序从GPS_PROVIDER检索这些信息,但我希望它也从3G和WiFi获取坐标,如果GPS不可用。 有人可以帮忙吗?

AlertDialog ad = new AlertDialog.Builder(getActivity()).setTitle("Dialog title")
                .setMessage("Do you want to send a request?")
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        if (locationManager
                                .isProviderEnabled(LocationManager.GPS_PROVIDER) && location!=null) {
//do HTTP request stuff
                                if (isOnline()) {

                                if (response.getStatusLine().getStatusCode()==200) {
                                    Toast toast = Toast.makeText(getActivity(), "Request has been sent. Your coordinates are: " + Double.toString(location.getLatitude()) + " (Latitude) " + Double.toString(location.getLongitude()) + " (Longitude).", Toast.LENGTH_LONG);
                                    toast.show();
                                }
                                else {
                                    Toast toast = Toast.makeText(getActivity(), "Request has NOT been sent.", Toast.LENGTH_LONG);
                                    toast.show();
                                }
                            }
                            else {
                                Toast toast = Toast.makeText(getActivity(), "Network is not available.", Toast.LENGTH_LONG);
                                toast.show();
                            }
                        }
                        else {
                            Toast toast = Toast.makeText(getActivity(), "GPS is not available.", Toast.LENGTH_LONG);
                            toast.show();
                        }
                    }
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        Toast toast = Toast.makeText(getActivity(), "Request has not been sent.", Toast.LENGTH_LONG);
                        toast.show();
                    }
                }).create();
        ad.show();
}
public boolean isOnline() {
    ConnectivityManager cm =
            (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);

    return cm.getActiveNetworkInfo() != null && 
            cm.getActiveNetworkInfo().isConnectedOrConnecting();
}

1 个答案:

答案 0 :(得分:0)

老兄你可以用这个

private Location getLocation() {
    LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        return manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    } else if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        return manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    }
    return null;
}

仅使用基于网络的位置使用此

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

对于基于GPS的位置,这一个

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>