2012-04-19 39 views
4

我正在开发一个android应用程序。一旦他/她登录到应用程序,我就需要找到用户的位置。我不希望显示地图,应该在没有用户知识的情况下识别地点。使用Google地图API可以做到这一点吗?或者有没有其他办法可以做到这一点?查找用户位置

感谢

回答

3

试试这个,

protected LocationManager locationManager; 
     Context context; 
     public String gps_loc; 

     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 10, new MyLocationListener()); 
private class MyLocationListener implements LocationListener { 

     public void onLocationChanged(Location location) { 
      gps_loc = String.format("%1$s" +"-"+"%2$s",location.getLongitude(), location.getLatitude()); 
      Toast.makeText(Clockin.this, gps_loc, Toast.LENGTH_SHORT).show(); 
     } 
     public void onStatusChanged(String s, int i, Bundle b) { 
      Toast.makeText(class.this, "Provider status changed", Toast.LENGTH_SHORT).show(); 
     } 
     public void onProviderDisabled(String s) { 
      Toast.makeText(class.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_SHORT).show(); 
      final AlertDialog alertDialog = new AlertDialog.Builder(class.this).create(); 
      alertDialog.setTitle("Activate GPS..."); 
      alertDialog.setButton("ok", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);     
       } 
      }); 
      alertDialog.show(); 
      }  
     public void onProviderEnabled(String s) { 
      Toast.makeText(class.this,"Provider enabled by the user. GPS turned on", Toast.LENGTH_SHORT).show(); 
     } 
    } 
+0

取出面包消息得到O/P没有用户的知识。 – wolverine 2012-04-19 06:57:08

5

要做到这一点,最好的办法是使用PASSIVE位置提供像这样:

LocationManager lm = (LocationManager)yourActivityContext.getSystemService(Context.LOCATION_SERVICE); 
Location lastKnown = lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); 

这将返回由操作系统接收到的最后已知位置,所以这可能是过时的,但您可以通过查询位置对象来检查何时检索位置以及由哪个提供商提供。

总之,用户将不知道,你已经得到了一个位置除非您的应用程序将需要适当的位置的权限(S)。