1

GPS 座標を操作しようとしてきましたが、思ったよりもずっと大変でした。試行錯誤の数時間後、エミュレーターの緯度と経度 (モック) を出力することができました。以下は、私が行った2つの方法です。

最初の方法:

import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

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

        String text = "";

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if(location != null) {
            showMyAddress(location);
        }

        final LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                showMyAddress(location);
            }
            public void onProviderDisabled(String arg0) {
            }
            public void onProviderEnabled(String arg0) {
            }
            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            }
        };

        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 10, locationListener);

        double longitude = location.getLongitude();
        double latitude = location.getLatitude();

        Log.i(TAG, "longitude: " + longitude);
        Log.i(TAG, "latitude: " + latitude);

        text = "longitude: " + longitude + ", " + "latitude: " + latitude;

        TextView textView = (TextView) findViewById(R.id.txtvwMain);
        textView.setText(text);
    }

    private void showMyAddress(Location location) {
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());   
        List<Address> myList;
        try {
            myList = myLocation.getFromLocation(latitude, longitude, 1);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

2 番目の方法:

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity implements LocationListener
{
    private LocationManager lm;
    private TextView tv;

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

        tv = (TextView) findViewById(R.id.txtvwMain);
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, this);

        Location location = lm.getLastKnownLocation(lm.GPS_PROVIDER);
        double longitude = location.getLongitude();
        double latitude = location.getLatitude();
        tv.setText("latitude="+latitude+", longitude="+longitude);
    }

    @Override
    public void onLocationChanged(Location arg0) {
        String lat = String.valueOf(arg0.getLatitude());
        String lon = String.valueOf(arg0.getLongitude());
        Log.e("GPS", "location changed: lat="+lat+", lon="+lon);
        tv.setText("lat="+lat+", lon="+lon);
    }
    public void onProviderDisabled(String arg0) {
        Log.e("GPS", "provider disabled " + arg0);
    }
    public void onProviderEnabled(String arg0) {
        Log.e("GPS", "provider enabled " + arg0);
    }
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        Log.e("GPS", "status changed to " + arg0 + " [" + arg1 + "]");
    }
}

上記の両方の方法が機能し、モックの緯度と経度をエミュレーター (Android 4.2.2 を実行) の TextView に出力します。ただし、.apk ファイルをデバイス (Android 4.0.4 を実行しているタブレット) にアップロードすると、クラッシュするだけです。エラーメッセージが表示されないため、何が問題なのかわかりません。問題の原因と解決方法を教えてください。ありがとう。

4

0 に答える 0