【问题标题】:Get coordinates from 3G/WiFi从 3G/WiFi 获取坐标
【发布时间】:2014-10-08 09:39:45
【问题描述】:

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

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();
}

【问题讨论】:

    标签: java android gps


    【解决方案1】:

    你可以用这个

    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"/>
    

    【讨论】:

    • 谢谢!很有帮助。
    猜你喜欢
    • 2011-08-27
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多