0

重複の可能性:
Android でユーザーの現在地を取得する最も簡単で確実な方法は何ですか?

電話でGoogleマップを開き、目的地の経度+緯度と開始位置を渡す次のコードがあります。コードに開始位置を手動で入力する代わりに、何らかの方法でコードを取得して、ユーザーがどこにいるかを自動的に検出できるようにする方法があるかどうか疑問に思っていました。

add.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent intent = new Intent (android.content.Intent.ACTION_VIEW,
            Uri.parse("http://maps.google.com/maps?saddr=" + 51.5171 +
                      "," + 0.1062 + "&daddr=" + 52.6342 + "," + 1.1385));

        intent.setComponent(
            new ComponentName ("com.google.android.apps.maps",
                               "com.google.android.maps.MapsActivity"));

        startActivity(intent);
    }
});
4

3 に答える 3

2

この方法を使用できます。

public LatLng getLocation(Context ctx) 
{
    LocationManager lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = lm.getProviders(true);

    /*
     * Loop over the array backwards, and if you get an accurate location,
     * then break out the loop
     */
    Location l = null;

    for (int i = providers.size() - 1; i >= 0; i--) 
    {
        l = lm.getLastKnownLocation(providers.get(i));
        if (l != null)
            break;
    }
    return new LatLng(l.getLatitude(),l.getLongitude());
}
于 2013-01-08T20:57:00.660 に答える
0

この質問を参照してください: Android でユーザーの現在の場所を取得する最も簡単で堅牢な方法は何ですか?

基本的に、場所を取得したら、getLatitude()、getLongitude() を使用して、それらを URL にドロップできます。

getLastKnownLocation よりも堅牢なものをお勧めします。最後の既知の場所に依存しているため、数時間または数日で更新されていない可能性があります。たとえば休暇中の場合、これは地球の反対側にある可能性があります。

詳細については、 http://developer.android.com/guide/topics/location/strategies.htmlも参照してください。

于 2013-01-08T21:02:08.510 に答える
-1

あなたの質問のステップバイステップガイド:

http://www.androidcompetencycenter.com/?s=GPS

お役に立てれば

于 2013-01-08T21:20:10.890 に答える