3

ユーザーが指定した緯度、経度、高度を取得し、電話でこの GPS の場所を偽造し、Google マップでその場所にいることを示す APP を開発しています。マニフェスト ファイルに必要な権限があり、モックの場所が開発者設定で有効になっています。

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//lm.clearTestProviderEnabled(mocLocationProvider);
lm.addTestProvider(mocLocationProvider, false, false, false, false, false, false, false, 0, 10);
lm.setTestProviderEnabled(mocLocationProvider, true);
mockLocation = new Location(mocLocationProvider); // a string
mockLocation.setLatitude(Integer.parseInt(latitude.getText().toString()));  // double 
mockLocation.setLongitude(Integer.parseInt(longitude.getText().toString())); 
mockLocation.setAltitude(Integer.parseInt(altitude.getText().toString())); 
mockLocation.setTime(System.currentTimeMillis()); 
lm.setTestProviderLocation( mocLocationProvider, mockLocation); 

しかし、Google マップで GPS 位置がまったく変更されていないように見えますが、何が問題なのですか?

更新: 携帯電話に「偽の GPS 位置情報」というアプリをインストールしたところ、そのアプリは正常に動作しますが、コードの何が問題なのかはまだわかりませんが、私のコードはこれを達成するための正式な方法だと思います。

更新 #2: 同様のアプリケーションのいくつかは私の携帯電話で実行できますが、いくつかの例外が見つかりました ( http://www.cowlumbus.nl/forum/MockGpsProvider.zip )、このアプリは私の携帯電話では動作しません。誰かがこの問題で私を助けることができますか? 何百万もの感謝!毎回場所を設定するときにエラーメッセージが表示されません。

Update#3 : このアプリはかなり古いため、4.1 では動作しません。もしそうなら、新しいバージョンで同じことをする方法は? 私の電話はサムスンギャラクシーs3です。

更新#4:参考までに、私の更新#2のアプリのコードは次のとおりです。

package nl.cowlumbus.android.mockgps;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

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

public class MockGpsProviderActivity extends Activity implements LocationListener {
    public static final String LOG_TAG = "MockGpsProviderActivity"; 
    private static final String MOCK_GPS_PROVIDER_INDEX = "GpsMockProviderIndex";

    private MockGpsProvider mMockGpsProviderTask = null;
    private Integer mMockGpsProviderIndex = 0;

    /** Called when the activity is first created. */
    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /** Use saved instance state if necessary. */
        if(savedInstanceState instanceof Bundle) {
            /** Let's find out where we were. */
            mMockGpsProviderIndex = savedInstanceState.getInt(MOCK_GPS_PROVIDER_INDEX, 0);
        }

        /** Setup GPS. */
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
            // use real GPS provider if enabled on the device
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        }
        else if(!locationManager.isProviderEnabled(MockGpsProvider.GPS_MOCK_PROVIDER)) {
            // otherwise enable the mock GPS provider
            locationManager.addTestProvider(MockGpsProvider.GPS_MOCK_PROVIDER, false, false,
                    false, false, true, false, false, 0, 5);
            locationManager.setTestProviderEnabled(MockGpsProvider.GPS_MOCK_PROVIDER, true);
        }  

        if(locationManager.isProviderEnabled(MockGpsProvider.GPS_MOCK_PROVIDER)) {
            locationManager.requestLocationUpdates(MockGpsProvider.GPS_MOCK_PROVIDER, 0, 0, this);

            /** Load mock GPS data from file and create mock GPS provider. */
            try {
                // create a list of Strings that can dynamically grow
                List<String> data = new ArrayList<String>();

                /** read a CSV file containing WGS84 coordinates from the 'assets' folder
                 * (The website http://www.gpsies.com offers downloadable tracks. Select
                 * a track and download it as a CSV file. Then add it to your assets folder.)
                 */         
                InputStream is = getAssets().open("mock_gps_data.csv");
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));

                // add each line in the file to the list
                String line = null;
                while ((line = reader.readLine()) != null) {
                    data.add(line);
                }

                // convert to a simple array so we can pass it to the AsyncTask
                String[] coordinates = new String[data.size()];
                data.toArray(coordinates);

                // create new AsyncTask and pass the list of GPS coordinates
                mMockGpsProviderTask = new MockGpsProvider();
                mMockGpsProviderTask.execute(coordinates);
            } 
            catch (Exception e) {}
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        // stop the mock GPS provider by calling the 'cancel(true)' method
        try {
            mMockGpsProviderTask.cancel(true);
            mMockGpsProviderTask = null;
        }
        catch (Exception e) {}

        // remove it from the location manager
        try {
            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            locationManager.removeTestProvider(MockGpsProvider.GPS_MOCK_PROVIDER);
        }
        catch (Exception e) {}
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        // store where we are before closing the app, so we can skip to the location right away when restarting
        savedInstanceState.putInt(MOCK_GPS_PROVIDER_INDEX, mMockGpsProviderIndex);
        super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    public void onLocationChanged(Location location) {
        // show the received location in the view
        TextView view = (TextView) findViewById(R.id.text);
        view.setText( "index:" + mMockGpsProviderIndex
                + "\nlongitude:" + location.getLongitude() 
                + "\nlatitude:" + location.getLatitude() 
                + "\naltitude:" + location.getAltitude() );     
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub      
    }


    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub      
    }


    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub      
    }


    /** Define a mock GPS provider as an asynchronous task of this Activity. */
    private class MockGpsProvider extends AsyncTask<String, Integer, Void> {
        public static final String LOG_TAG = "GpsMockProvider";
        public static final String GPS_MOCK_PROVIDER = "GpsMockProvider";

        /** Keeps track of the currently processed coordinate. */
        public Integer index = 0;

        @Override
        protected Void doInBackground(String... data) {         
            // process data
            for (String str : data) {
                // skip data if needed (see the Activity's savedInstanceState functionality)
                if(index < mMockGpsProviderIndex) {
                    index++;
                    continue;
                }               

                // let UI Thread know which coordinate we are processing
                publishProgress(index);

                // retrieve data from the current line of text
                Double latitude = null;
                Double longitude = null;
                Double altitude= null;
                try {
                    String[] parts = str.split(",");
                    latitude = Double.valueOf(parts[0]);
                    longitude = Double.valueOf(parts[1]);
                    altitude = Double.valueOf(parts[2]);
                }
                catch(NullPointerException e) { break; }        // no data available
                catch(Exception e) { continue; }                // empty or invalid line

                // translate to actual GPS location
                Location location = new Location(GPS_MOCK_PROVIDER);
                location.setLatitude(latitude);
                location.setLongitude(longitude);
                location.setAltitude(altitude);
                location.setTime(System.currentTimeMillis());
                location.setLatitude(latitude);
                location.setLongitude(longitude);
                location.setAccuracy(16F);
                location.setAltitude(0D);
                location.setTime(System.currentTimeMillis());
                location.setBearing(0F);

                // show debug message in log
                Log.d(LOG_TAG, location.toString());

                // provide the new location
                LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                locationManager.setTestProviderLocation(GPS_MOCK_PROVIDER, location);

                // sleep for a while before providing next location
                try {
                    Thread.sleep(200);

                    // gracefully handle Thread interruption (important!)
                    if(Thread.currentThread().isInterrupted())
                        throw new InterruptedException("");
                } catch (InterruptedException e) {
                    break;
                }

                // keep track of processed locations
                index++;
            }

            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            Log.d(LOG_TAG, "onProgressUpdate():"+values[0]);
            mMockGpsProviderIndex = values[0];
        }
    }
}
4

1 に答える 1

2

問題の解決: 次のコードを追加して現在の場所を設定すると、Google マップ アプリケーションに正常に表示されます。

location.setLatitude(latitude);
location.setLongitude(longitude);
location.setBearing(bearing);
location.setSpeed(speed);
location.setAltitude(altitude);
location.setTime(new Date().getTime());
location.setProvider(LocationManager.GPS_PROVIDER);
location.setAccuracy(1);

結論: 新しいバージョンの android で模擬位置情報サービスを使用する場合は、すべての属性を自分で設定する必要があります。

于 2013-02-12T18:45:33.167 に答える