2

私はしばらくの間、このコードに取り組んできましたが、すべて問題ないように見えますが、私のエディターであるEclipseは、myManagerをキャストするように私に言い続けています。コードは以下の通り、

package com.gps.project;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.content.Context;
import android.widget.Button;
/*LOcation based API*/
import android.location.LocationManager;


public class GpsActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /*Create ab ttuon we are to use in our GPS*/
        final Button gpsButton=(Button) findViewById(R.id.gpsButton);
        gpsButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                LoadCoords();

            }
        });

    }
    public void LoadCoords(){
        /*Create two textfields that will contain information as latitudes and longitudes*/
        TextView latText=(TextView) findViewById(R.id.latText);
        TextView lngText=(TextView) findViewById(R.id.lngText);

        /*Creation of a location manager from which we are to pull the location values*/
        LocationManager myManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
        /*To pull the coordinates from myManager, we use the getCurrentLocation( ) method.*/
        Double latPoint = myManager.getCurrentLocation("gps").getLatitude();
        Double lngPoint = myManager.getCurrentLocation("gps").getLongitude();
        /*Finally, take the new Double values and pass them to your TextViews:*/
        latText.setText(latPoint.toString());
        lngText.setText(lngPoint.toString());
    }
}

誰かが私のエラーを修正するのを手伝ってくれたらうれしいです。経度と緯度を取得するつもりです。

4

1 に答える 1

0

返信で言ったように、代わりに getLastKnownLocation を使用する必要があると思いますがドキュメントgetCurrentLocationでは見つかりませんでした (おそらく非推奨)。

指定されたプロバイダーから取得された最後の既知の位置修正からのデータを示す Location を返します。これは、プロバイダーを開始せずに実行できます。この場所は、デバイスの電源を切って別の場所に移動した場合など、古い可能性があることに注意してください。プロバイダーが現在無効になっている場合は、null が返されます。

追加する必要があるアクセス許可は次のとおりです。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
于 2012-04-29T21:45:50.363 に答える