2015-03-25 91 views

回答

0

有三種方式找到自己的位置,refer this

TelephonyManager telephonyManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); GsmCellLocation cellLocation =(GsmCellLocation)telephonyManager.getCellLocation();

int cellid = cellLocation.getCid(); int celllac = cellLocation.getLac();

+0

感謝您的回答,您可以發表一些示例代碼嗎? – 2015-03-25 08:41:05

+0

參考這個http://developerlife.com/tutorials/?p=1375 – rKrishna 2015-03-25 08:51:13

+0

沒有工作,對不起:( – 2015-03-25 08:55:38

0

您可以在方法getLastKnownLocation中使用NETWORK_PROVIDER作爲服務參數。

+0

當位置服務在設備中被禁用時,我無法獲得最後的位置 – 2015-03-25 08:56:25

+0

如果定位服務被禁用,你將無法獲得GPS位置。 – Arun 2015-03-25 08:59:03

0

如果不想GPS得到的位置,那麼你應該使用NETWORK_PROVIDER獲得位置:

我已經採取了從AndroidHive website這個模塊你可以參考更多的在那裏。

您的查詢將用此方法getLocation()

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; 
      // First get location from Network Provider 
      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; 
} 
相關問題