รับ lat lng ปัจจุบันของผู้ใช้หลังจากเปิดใช้งาน GPS - Android

ฉันได้ปฏิบัติตามบทช่วยสอนนี้แล้ว http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ สำหรับการแสดงป๊อปอัปหากไม่ได้เปิดใช้งาน GPS และรับตำแหน่งปัจจุบันของผู้ใช้

และนี่คือเครื่องรับออกอากาศที่รับฟังสภาวะปิดการใช้งาน GPS

private BroadcastReceiver mGpsSwitchStateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
            gps = new GPSTracker(getActivity());

            if(gps.canGetLocation()){
                Log.d("Your Location", "latitude:" + gps.getLatitude() + ", longitude: " + gps.getLongitude());

                Toast.makeText(getActivity(), "GPS Switch", Toast.LENGTH_SHORT).show();

                if(gps.getLatitude() != 0 && gps.getLongitude() != 0 ){
                    final CameraPosition cameraPosition = new CameraPosition.Builder()
                            .target(new LatLng(gps.getLatitude(),gps.getLongitude()))      // Sets the center of the map to selected place
                            .zoom(15)                   // Sets the zoom
                            .build();                   //
                    gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                    Toast.makeText(getActivity(), "GPS Switch Lat: "+gps.getLatitude()+" Long: "+gps.getLongitude(), Toast.LENGTH_SHORT).show();
                }
            } else {
                if(!gps.isSettingDialogShown())
                    gps.showSettingsAlert();
            }
        }
    }
};

เมื่อฉันเปิดใช้งาน GPS ผ่านทางกล่องโต้ตอบการตั้งค่าหรือจากเมนูแบบเลื่อนลง คลาส GPSTracker รับบริการระบุตำแหน่งและตรวจสอบตำแหน่งปัจจุบันของผู้ใช้ในส่วนของโค้ดนี้

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network Enabled");
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();

                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();

                        }
                    }
                }
            }
        }

    } catch (SecurityException e) {
        e.printStackTrace();
    }

    return location;
}

ปัญหา:

ประการแรก ฉันไม่รู้ว่าทำไมเครื่องรับการออกอากาศ ได้รับการดำเนินการสองครั้ง และประการที่สองเมื่ออยู่ในโหมดปกติ ค่า lat lng ในเครื่องรับยังคงเป็นศูนย์ แต่ในการดีบั๊กบางครั้งจะส่งกลับค่าของตำแหน่งปัจจุบันของผู้ใช้

คุณช่วยได้ไหมว่าฉันทำอะไรผิด?


person Malik Usman    schedule 31.03.2017    source แหล่งที่มา
comment
stackoverflow.com/a/43082164/115145   -  person CommonsWare    schedule 31.03.2017
comment
ใช้ Google Fused Location Api และการตั้งค่าตำแหน่ง ตรวจสอบสิ่งนี้   -  person Abid Khan    schedule 31.03.2017
comment
@CommonsWare ฉันไม่เข้าใจว่าฉันจะรับ lat lng ในเครื่องรับออกอากาศหลังจากเปิด GPS ได้อย่างไร และในคำตอบของคุณ คุณบอกว่าหลีกเลี่ยงการใช้ getLastKnownLocation() แล้วจะใช้อะไรแทนสิ่งนี้?   -  person Malik Usman    schedule 31.03.2017
comment
ใช้ requestLocationUpdates() โดยที่คุณจะได้รับตำแหน่งใน LocationListener ของคุณ ข้อมูลนี้ครอบคลุมอยู่ในคำตอบที่ฉันเชื่อมโยง ดูขั้นตอน #2 และ #3   -  person CommonsWare    schedule 31.03.2017


คำตอบ (1)


หากคุณไม่จำเป็นต้องทำงานกับ Gps และตำแหน่งเครือข่ายแยกกัน ฉันแนะนำให้ใช้ FusedLocationProvider BroadcastReceiver อาจทำงานสองครั้งเพราะใช้ได้กับทั้งบริการระบุตำแหน่ง GPS และเครือข่าย

person Hakan Saglam    schedule 31.03.2017
comment
ขอบคุณสำหรับความช่วยเหลือของคุณ มันช่วยแก้ปัญหาของฉันที่ฉันกำลังเผชิญอยู่ - person Malik Usman; 03.04.2017