2013-07-19 72 views
0

我想獲取用戶使用網絡的位置。這是我的代碼網絡位置返回錯誤

package com.example.locationtest; 

import android.app.Activity; 
import android.content.Context; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.text.format.DateFormat; 
import android.util.Log; 

public class WhereAmI extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     LocationManager locationManager; 
     String context = Context.LOCATION_SERVICE; 
     locationManager = (LocationManager) getSystemService(context); 

     Criteria crta = new Criteria(); 
     crta.setAccuracy(Criteria.ACCURACY_FINE); 
     crta.setAltitudeRequired(false); 
     crta.setBearingRequired(false); 
     crta.setCostAllowed(true); 
     crta.setPowerRequirement(Criteria.POWER_LOW); 
     String provider = locationManager.getBestProvider(crta, true); 

     // String provider = LocationManager.GPS_PROVIDER; 
     Location location = locationManager.getLastKnownLocation(provider); 
     updateWithNewLocation(location); 

     locationManager.requestLocationUpdates(provider, 1000, 0, 
       locationListener); 
    } 

    private final LocationListener locationListener = new LocationListener() { 

     @Override 
     public void onLocationChanged(Location location) { 
      updateWithNewLocation(location); 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      updateWithNewLocation(null); 
     } 

     @Override 
     public void onProviderEnabled(String provider) { 
     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
     } 

    }; 

    private void updateWithNewLocation(Location location) { 
     String timeStamp = (String) DateFormat.format("hh:mm:ss", 
       new java.util.Date()); 
     String locationString = location.getLatitude() + ", " 
       + location.getLongitude(); 

     Log.i("Main", "Time = " + timeStamp + " Location = " + locationString); 
    } 
} 

該代碼可以使用我的Nexus 4,但如果我用我的銀河吉奧使用它的位置是關閉的30英里..

回答

0

網絡位置是基於最近的手機信號塔手機連接到哪裏。 Nexus4可能連接到一個Cell塔,而Galaxy Gio連接到另一個可能離設備所在位置很遠的單元塔。要獲得準確的修復,請使用GPS_PROVIDER。使用NETWORK_PROVIDER之前,我遇到過這個問題。擺脫它,當我開始使用GPS_PROVIDER,給了我準確的修復

相關問題