2012-01-09 59 views
1
public class LocationtesterActivity extends Activity { 
     /** Called when the activity is first created. */ 
     LocationManager locManager; 
     // LocationListener locListener; 
     Button b; 
     String provider; 
     TextView lat, alt, longi; 


// Here i am first checking if both GPS and network options are enabled in Lovation and Security Settings or not. 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      b = (Button) findViewById(R.id.button1); 
      lat = (TextView) findViewById(R.id.lattitude); 
      alt = (TextView) findViewById(R.id.altitude); 
      longi = (TextView) findViewById(R.id.longitude); 

      b.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 

        showCurrentLocation(); 

       } 

      }); 

      locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

      Criteria criteria = new Criteria(); 
      criteria.setAccuracy(Criteria.ACCURACY_FINE); 
      criteria.setAltitudeRequired(false); 
      criteria.setBearingRequired(false); 
      criteria.setCostAllowed(true); 

      criteria.setPowerRequirement(Criteria.POWER_LOW); 

      provider = locManager.getBestProvider(criteria, true); 

      System.out.println("best provider is :" + provider); 

      if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER) 
        || !locManager 
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
       System.out.println("in provider enablement"); 
       createGpsDisabledAlert(); 

      } 

      else { 
       System.out.println("in location update request"); 
       locManager.requestLocationUpdates(provider, 0, 0, 
         new MyLocationListener()); 

      } 

     } 

     // for displaying the Dialogue Box 
     // if GPS or network not enabled 
     // and also taking to location and security screen 

     private void createGpsDisabledAlert() { 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage(
      "Your GPS or network provider is disabled! Would you like to enable it?") 
      .setCancelable(false) 
      .setPositiveButton("Enable provider", 
        new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        showGpsOptions(); 
       } 
      }); 
      builder.setNegativeButton("Do nothing", 
        new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
       } 
      }); 
      AlertDialog alert = builder.create(); 
      alert.show(); 
     } 

     private void showGpsOptions() { 
      Intent gpsOptionsIntent = new Intent(
        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      startActivityForResult(gpsOptionsIntent, 5); 
     } 

     // //////ends///// 

     // Code to check whether user enabled GPS and Network provider in settings 
     // if not then show the dialogue box again 

     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      if (requestCode == 5 && resultCode == 0) { 
       if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
        createGpsDisabledAlert(); 
        // Toast.makeText(this, 
        // "provider not enabled. Click the button for settings", 
        // 2000).show(); 
       } else { 

        Intent i=new Intent(this, LocationtesterActivity.class); 
        startActivity(i); 
        Toast.makeText(this, "User has enabled the provider", 1000) 
        .show(); 

       } 
      } 
     } 

     // Method to display the current location 
     protected void showCurrentLocation() { 

      Location location = locManager 
      .getLastKnownLocation(LocationManager.GPS_PROVIDER); 

      if (location != null) { 

       String message = String.format(

         "Current Location \n Longitude: %1$s \n Latitude: %2$s", 

         location.getLongitude(), location.getLatitude() 

       ); 

       lat.setText("" + location.getLatitude()); 
       longi.setText("" + location.getLongitude()); 

       Toast.makeText(LocationtesterActivity.this, message, 

         Toast.LENGTH_LONG).show(); 

      } 
      else 
      { 
       Toast.makeText(LocationtesterActivity.this, "no last known location", 1000).show(); 
      } 

     } 

     // Inner class for LocationListener 
     private class MyLocationListener implements LocationListener { 

      public void onLocationChanged(Location location) { 


       System.out.println("in location changed"); 

       String message = String.format(

         "New Location \n Longitude: %1$s \n Latitude: %2$s", 

         location.getLongitude(), location.getLatitude() 

       ); 
       lat.setText("" + location.getLatitude()); 
       longi.setText("" + location.getLongitude()); 
       // alt.setText("" + location.getAltitude()); 
       Toast.makeText(LocationtesterActivity.this, message, 
         Toast.LENGTH_LONG).show(); 

      } 

      public void onStatusChanged(String s, int i, Bundle b) { 

       Toast.makeText(LocationtesterActivity.this, 
         "Provider status changed", 

         Toast.LENGTH_LONG).show(); 

      } 

      public void onProviderDisabled(String s) { 

       Toast.makeText(LocationtesterActivity.this, 

         "Provider disabled by the user. GPS turned off", 

         Toast.LENGTH_LONG).show(); 

      } 

      public void onProviderEnabled(String s) { 

       Toast.makeText(LocationtesterActivity.this, 

         "Provider enabled by the user. GPS turned on", 

         Toast.LENGTH_LONG).show(); 

      } 

     } 

    } 

我正在使用上面的代碼來查找用戶位置。 1.根據我的標準,我始終將GPS作爲最佳供應商,但這對我來說很好。 2.Thing是爲什麼我無法獲得任何位置值? 即使位置位置= locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);我沒有得到任何東西。 請幫我獲取位置值。使用GPS無法找到用戶位置

+0

如果您正在檢查手機(設備)位置的位置= locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)試試這個初始加載和移動獲取更新的值與GPS! – Chet 2012-01-09 06:11:53

+0

隨着網絡提供商它的工作,但與GPS它不工作,即使我等待10薄荷糖。 – 2012-01-09 07:09:26

+0

將此更改爲locManager.requestLocationUpdates(provider,0,0, new MyLocationListener());到........ locManager.requestLocationUpdates(provider,8000,2, new MyLocationListener()); ... 8000(time [millisec] req for update ..)並且2指定計數器... – Chet 2012-01-09 15:57:15

回答

0

GPS需要一些時間,因爲建築物內部很慢並且不可用。您只需在建築物外面檢查一下。
And Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);只有當GPS一次獲得任何位置時纔有效。

+1

: - 感謝您的信息。但問題是,5分鐘或10分鐘後,我也調用了相同的方法,但我無法顯示任何值。 – 2012-01-09 07:06:15

+0

http://stackoverflow.com/questions/5124525/current-gps-location-in-android訪問此網站可能會找到您的解決方案 – 2012-01-09 07:11:29

-1

兩件事情,

  1. 具有u設置清單文件 「android.permission.ACCESS_FINE_LOCATION」 權限?

  2. 清除Google地圖應用程序的所有數據,然後嘗試加載地圖以獲取當前位置的地圖,如果Google地圖給出您的當前位置,則應用程序可以獲取當前位置。

它使用最佳標準獲取位置的好習慣。

有時設備無法讀取當前位置。我也面臨這個問題,但當時我檢查了Google Map/Direction應用程序並重置了我的GPS。

+0

我已添加權限: - – 2012-01-09 11:36:10

+0

我已添加權限: - 「android。 permission.ACCESS_COARSE_LOCATION 「 」android.permission.ACCESS_FINE_LOCATION「 」android.permission.ACCESS_MOCK_LOCATION「 」android.permission.INTERNET對「 」android.permission.CONTROL_LOCATION_UPDATES「 」android.permission.ACCESS_WIFI_STATE「 」 android.permission。 ACCESS_NETWORK_STATE「,我也加載了Google地圖。它顯示我的位置。我使用的是Android 2.2。 Aand我已經嘗試了使用Android 2.2 API和Google Inc API 2.2的項目。我需要別的東西嗎? – 2012-01-09 11:42:02

+0

你必須使用谷歌API。爲什麼你堅持要求GPS提供商,如果你可以在網絡提供商的幫助下獲取位置,那麼它有什麼問題? – rajpara 2012-01-09 12:48:42