2015-03-31 91 views
4

我已使用this中的代码。我已经改变了一下。以下是我的代码片段。问题是谷歌地图没有显示我嘲笑的适当位置。模拟位置在Google地图上不起作用

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, true, true, Criteria.POWER_LOW, 
      Criteria.ACCURACY_FINE); 
    locationManager.setTestProviderEnabled(MockGpsProvider.GPS_MOCK_PROVIDER, true); 
    locationManager.setTestProviderStatus(LocationManager.GPS_PROVIDER, LocationProvider.AVAILABLE, null, System.currentTimeMillis()); 

    // } 

    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 = LocationManager.GPS_PROVIDER; 

    /** 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.setAccuracy(1); 
      location.setTime(System.currentTimeMillis()); 
      location.setBearing(0F); 
      location.setSpeed(0.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]; 
    } 
} 
} 
+0

它显示的是什么位置?这是你的实际位置还是看似随机的东西?你是在物理设备上还是在仿真器上运行它? – 2015-03-31 12:23:04

+0

它在该路径上显示随机位置。我在设备上运行。 – Sanket 2015-04-01 05:21:31

回答

1

尝试增加:

location.setAccuracy(16F); 
location.setAltitude(0D); 
location.setBearing(0F); 
+0

已经尝试过,现在正在为我工​​作.. – Sanket 2015-04-02 09:06:40

+0

我在一个时间点的研究表明,嘲笑真实设备上的位置是零星.. – AniV 2015-04-02 16:48:52

0

我想你也可以调用模拟位置在您的权限(的Manifest.xml),为了工作。你有这个权限吗?

因为当我用它与this,它的工作。

(抱歉,我不能把它作为一个评论,因为我没有足够的声誉)

+0

我已经在manifest.xml文件中使用了该权限。 – Sanket 2015-04-07 09:49:50

0

你有没有在你的物理设备的设置打开Allow_Mock_Location?它在开发人员选项下。

+0

是的......它已经打开了。 – Sanket 2016-02-26 10:04:05

0

另外为您的模拟位置添加 location.setSpeed(5); 速度超过1,比如5或10