
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) */@OverrIDepublic 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,this); // } // else if(!locationManager.isProvIDerEnabled(MockGpsProvIDer.GPS_MOCK_PROVIDER)) { // otherwise enable the mock GPS provIDer locationManager.addTestProvIDer(MockGpsProvIDer.GPS_MOCK_PROVIDER,false,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,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) { } }}@OverrIDepublic 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) { }}@OverrIDepublic 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);}@OverrIDepublic 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());}@OverrIDepublic voID onProvIDerDisabled(String provIDer) { // Todo auto-generated method stub}@OverrIDepublic voID onProvIDerEnabled(String provIDer) { // Todo auto-generated method stub}@OverrIDepublic 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]; }}}解决方法 尝试添加: location.setAccuracy(16F);location.setAltitude(0D);location.setbearing(0F);总结
以上是内存溢出为你收集整理的android – 模拟位置无法在Google地图上运行全部内容,希望文章能够帮你解决android – 模拟位置无法在Google地图上运行所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)