2012-02-29 3 views

答えて

0

ありがとう、私を助けてください。例を参照してください:

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
double longitude = location.getLongitude(); 
double latitude = location.getLatitude(); 

そしてManisfestに置き:

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

Heelo..Iを発現させる権限がウルのコードを使用していますが、それはlocation.getLongitudeに私にnullポインタ例外 – user1061793

+0

を与えています。 – user1061793

+0

これはおそらくそのプロバイダにその情報を持っていないからです。他のプロバイダを調べるための私の答えを見てください。ほとんどの場合結果を得ることができます。 – SERPRO

1

は、このチュートリアルを見てみましょう:基本的にA Deep Dive Into Location

、あなたは最後の既知のを使用しておおよその位置を取得したい場合場所は、このようなループを持つ可能なすべてのプロバイダを繰り返し処理する必要があります。

List<String> matchingProviders = locationManager.getAllProviders(); 
for (String provider: matchingProviders) { 
    Location location = locationManager.getLastKnownLocation(provider); 
    if (location != null) { 
    float accuracy = location.getAccuracy(); 
    long time = location.getTime(); 

    if ((time > minTime && accuracy < bestAccuracy)) { 
     bestResult = location; 
     bestAccuracy = accuracy; 
     bestTime = time; 
    } 
    else if (time < minTime && 
      bestAccuracy == Float.MAX_VALUE && time > bestTime){ 
     bestResult = location; 
     bestTime = time; 
    } 
    } 
} 
0

はあなたが

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

を追加しました)(ファイル

0
Try This 

public class LocationDemo extends Activity implements LocationListener { 
private static final String TAG = "LocationDemo"; 
private static final String[] S = { "Out of Service", 
     "Temporarily Unavailable", "Available" }; 

private TextView output; 
private LocationManager locationManager; 
private String bestProvider; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // Get the output UI 
    output = (TextView) findViewById(R.id.output); 

    // Get the location manager 
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 

    // List all providers: 
    List<String> providers = locationManager.getAllProviders(); 
    for (String provider : providers) { 
     printProvider(provider); 
    } 

    Criteria criteria = new Criteria(); 
    bestProvider = locationManager.getBestProvider(criteria, false); 
    output.append("\n\nBEST Provider:\n"); 
    printProvider(bestProvider); 

    output.append("\n\nLocations (starting with last known):"); 
    Location location = locationManager.getLastKnownLocation(bestProvider); 
    printLocation(location); 
} 

/** Register for the updates when Activity is in foreground */ 
@Override 
protected void onResume() { 
    super.onResume(); 
    locationManager.requestLocationUpdates(bestProvider, 20000, 1, this); 
} 

/** Stop the updates when Activity is paused */ 
@Override 
protected void onPause() { 
    super.onPause(); 
    locationManager.removeUpdates(this); 
} 

public void onLocationChanged(Location location) { 
    printLocation(location); 
} 

public void onProviderDisabled(String provider) { 
    // let okProvider be bestProvider 
    // re-register for updates 
    output.append("\n\nProvider Disabled: " + provider); 
} 

public void onProviderEnabled(String provider) { 
    // is provider better than bestProvider? 
    // is yes, bestProvider = provider 
    output.append("\n\nProvider Enabled: " + provider); 
} 

public void onStatusChanged(String provider, int status, Bundle extras) { 
    output.append("\n\nProvider Status Changed: " + provider + ", Status=" 
      + S[status] + ", Extras=" + extras); 
} 

private void printProvider(String provider) { 
    LocationProvider info = locationManager.getProvider(provider); 
    output.append(info.toString() + "\n\n"); 
} 

private void printLocation(Location location) { 
    if (location == null) 
     output.append("\nLocation[unknown]\n\n"); 
    else 
     output.append("\n\n" + location.toString()); 
} 

} 

// main.xml 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

    <ScrollView android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <TextView android:id="@+id/output" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 
    </ScrollView> 
</LinearLayout> 
関連する問題