2014-01-30 105 views
0

我有3個片段類的標籤佈局,其中一個我已經實現了一個按鈕來獲取當前位置(緯度&經度)。一切工作正常,除了緯度和經度顯示值爲0爲什麼我得到0經度和緯度?

package com.swipetab.example; 

     import java.util.Calendar; 

     import android.app.DatePickerDialog.OnDateSetListener; 
     import android.app.TimePickerDialog.OnTimeSetListener; 
     import android.content.Context; 
     import android.os.Bundle; 
     import android.support.v4.app.DialogFragment; 
     import android.support.v4.app.Fragment; 
     import android.view.LayoutInflater; 
     import android.view.View; 
     import android.view.ViewGroup; 
     import android.view.View.OnClickListener; 
     import android.widget.Button; 
     import android.widget.TextView; 
     import android.widget.TimePicker; 
     import android.widget.Toast; 

     public class FragmentC extends Fragment { 
      Button Date, Time, GpsBtn; 
      GPSTracker gps; 

      @Override 
      public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) { 
       View myFragmentView = inflater.inflate(R.layout.fragment_c, container, 
         false); 
       Date = (Button) myFragmentView.findViewById(R.id.button1); 
       Time = (Button) myFragmentView.findViewById(R.id.button2); 
       GpsBtn = (Button) myFragmentView.findViewById(R.id.button3); 
       Date.setOnClickListener(DateOnClickListener); 
       Time.setOnClickListener(TimeOnClickListener); 
       GpsBtn.setOnClickListener(GPSOnClickListener); 

       return myFragmentView; 
      } 

      OnClickListener DateOnClickListener = new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 

        showDatePicker(); 

       } 
      }; 

      OnClickListener TimeOnClickListener = new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 

        showTimePicker(); 

       } 
      }; 
      OnClickListener GPSOnClickListener = new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 

        showGPSLocation(); 

       } 
      }; 

      private void showDatePicker() { 
       DatePicker date = new DatePicker(); 
       /** 
       * Set Up Current Date Into dialog 
       */ 
       Calendar calender = Calendar.getInstance(); 
       Bundle args = new Bundle(); 
       args.putInt("year", calender.get(Calendar.YEAR)); 
       args.putInt("month", calender.get(Calendar.MONTH)); 
       args.putInt("day", calender.get(Calendar.DAY_OF_MONTH)); 
       date.setArguments(args); 
       /** 
       * Set Call back to capture selected date 
       */ 
       date.setCallBack(ondate); 

       date.show(getFragmentManager(), "Date Picker"); 
      } 

      private void showTimePicker() { 
       Time time = new Time(); 
       /** 
       * Set Up Current Time Into dialog 
       */ 
       Calendar calender = Calendar.getInstance(); 
       Bundle args = new Bundle(); 
       args.putInt("hour", calender.get(Calendar.HOUR_OF_DAY)); 
       args.putInt("min", calender.get(Calendar.MINUTE)); 
       time.setArguments(args); 
       /** 
       * Set Call back to capture selected date 
       */ 
       time.setCallBack(ontime); 

       time.show(getFragmentManager(), "Time Picker"); 
      } 

      private void showGPSLocation() { 
       gps = new GPSTracker(getActivity()); 

       // check if GPS enabled 
       if (gps.canGetLocation()) { 

        double latitude = gps.getLatitude(); 
        double longitude = gps.getLongitude(); 

        // \n is for new line 
        Toast.makeText(
          getActivity(), 
          "Your Location is - \nLat: " + latitude + "\nLong: " 
            + longitude, Toast.LENGTH_LONG).show(); 
       } else { 
        // can't get location 
        // GPS or Network is not enabled 
        // Ask user to enable GPS/network in settings 
        gps.showSettingsAlert(); 

       } 
      } 

      OnDateSetListener ondate = new OnDateSetListener() { 

       @Override 
       public void onDateSet(android.widget.DatePicker arg0, int year, 
         int month, int day) { 
        // TODO Auto-generated method stub 

        String TabOfFragmentB = ((MainActivity) getActivity()) 
          .getTabFragmentB(); 

        FragmentB fragmentB = (FragmentB) getActivity() 
          .getSupportFragmentManager().findFragmentByTag(
            TabOfFragmentB); 

        String y = Integer.toString(year); 
        String m = Integer.toString(month); 
        String d = Integer.toString(day); 

        String dat = y.concat("-") + m.concat("-") + d; 

        fragmentB.b_updateDate(dat); 

        Toast.makeText(
          getActivity(), 
          String.valueOf(year) + "-" + String.valueOf(month) + "-" 
            + String.valueOf(day), Toast.LENGTH_LONG).show(); 
       } 
      }; 
      OnTimeSetListener ontime = new OnTimeSetListener() { 

       @Override 
       public void onTimeSet(TimePicker arg0, int hourOfDay, int min) { 
        // TODO Auto-generated method stub 
        String TabOfFragmentB = ((MainActivity) getActivity()) 
          .getTabFragmentB(); 

        FragmentB fragmentB = (FragmentB) getActivity() 
          .getSupportFragmentManager().findFragmentByTag(
            TabOfFragmentB); 
        int hour; 
        String am_pm; 
        if (hourOfDay > 12) { 
         hour = hourOfDay - 12; 
         am_pm = "PM"; 
        } else { 
         hour = hourOfDay; 
         am_pm = "AM"; 
        } 
        String h = Integer.toString(hour); 
        String m = Integer.toString(min); 

        String t = h.concat(":") + m + " " + am_pm; 

        fragmentB.b_updateTime(t); 

        Toast.makeText(getActivity(), 
          String.valueOf(hour) + "-" + String.valueOf(min), 
          Toast.LENGTH_LONG).show(); 
       } 
      }; 
     } 

GPSTracker類

package com.swipetab.example; 

    import android.app.AlertDialog; 
    import android.app.Service; 
    import android.content.Context; 
    import android.content.DialogInterface; 
    import android.content.Intent; 
    import android.location.Location; 
    import android.location.LocationListener; 
    import android.location.LocationManager; 
    import android.os.Bundle; 
    import android.os.IBinder; 
    import android.provider.Settings; 
    import android.util.Log; 

    public class GPSTracker extends Service implements LocationListener { 

     private final Context mContext; 

     // flag for GPS status 
     boolean isGPSEnabled = false; 

     // flag for network status 
     boolean isNetworkEnabled = false; 

     // flag for GPS status 
     boolean canGetLocation = false; 

     Location location; // location 
     double latitude; // latitude 
     double longitude; // longitude 

     // The minimum distance to change Updates in meters 
     private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 

     // The minimum time between updates in milliseconds 
     private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 

     // Declaring a Location Manager 
     protected LocationManager locationManager; 

     public GPSTracker(Context context) { 
      this.mContext = context; 
      getLocation(); 
     } 

     private Location getLocation() { 
      // TODO Auto-generated method stub 
      try { 
       locationManager = (LocationManager) mContext 
         .getSystemService(LOCATION_SERVICE); 
       // getting GPS status 
       isGPSEnabled = locationManager 
         .isProviderEnabled(LocationManager.GPS_PROVIDER); 
       if (!isGPSEnabled && !isNetworkEnabled) { 
        // no network provider is enabled 
        System.out.println("Please Enable!!"); 
       } 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"); 

         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 Enabled", "GPS Enabled"); 
          if (locationManager != null) { 
           location = locationManager 
             .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
           if (location != null) { 
            latitude = location.getLatitude(); 
            longitude = location.getLongitude(); 
           } 
          } 
         } 
        } 
       } 

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

      return location; 
     } 

     /** 
     * Stop using GPS listener Calling this function will stop using GPS in your 
     * app 
     * */ 
     public void stopUsingGPS() { 
      if (locationManager != null) { 
       locationManager.removeUpdates(GPSTracker.this); 
      } 
     } 

     /** 
     * Function to get latitude 
     * */ 
     public double getLatitude() { 
      if (location != null) { 
       latitude = location.getLatitude(); 
      } 

      // return latitude 
      return latitude; 
     } 

     /** 
     * Function to get longitude 
     * */ 
     public double getLongitude() { 
      if (location != null) { 
       longitude = location.getLongitude(); 
      } 

      // return longitude 
      return longitude; 
     } 

     /** 
     * Function to check GPS/wifi enabled 
     * 
     * @return boolean 
     * */ 
     public boolean canGetLocation() { 
      return this.canGetLocation; 
     } 

     /** 
     * Function to show settings alert dialog On pressing Settings button will 
     * lauch Settings Options 
     * */ 
     public void showSettingsAlert() { 
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); 

      // Setting Dialog Title 
      alertDialog.setTitle("GPS settings"); 

      // Setting Dialog Message 
      alertDialog 
        .setMessage("GPS is not enabled. Do you want to go to settings menu?"); 

      // On pressing Settings button 
      alertDialog.setPositiveButton("Settings", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          Intent intent = new Intent(
            Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
          mContext.startActivity(intent); 
         } 
        }); 

      // on pressing cancel button 
      alertDialog.setNegativeButton("Cancel", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.cancel(); 
         } 
        }); 

      // Showing Alert Message 
      alertDialog.show(); 
     } 

     @Override 
     public void onLocationChanged(Location arg0) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProviderDisabled(String arg0) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProviderEnabled(String arg0) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public IBinder onBind(Intent arg0) { 
      // TODO Auto-generated method stub 
      return null; 
     } 

    } 

回答

0

你如果條件下,都是問題,我think.if GPS未啓用,那麼你獲取從網絡位置提供商和網絡提供商您的選擇 - >(使用無線網絡)應啓用位置服務設置和您的互聯網連接應該工作。基本工作,請找到鏈接。 enter link description here

0

您是否已將此項添加到您的AndroidManifest.xml文件中?

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

它的ACCESS_CO(A)RSE_LOCATION而不是ACCESS_COURSE_LOCATION –

0

您可以使用android studio中的顯示器控件更改座標。您必須轉至工具 - > Android-> Android設備監視器。它顯示一個窗口,在這個窗口中,你選擇模擬器控制,在底部你可以改變座標(經度,高度)

+0

在那裏,你可以發送模擬器的經度和高度 –