private Location map(LatLng latLng1, float bearing) {
   Location location = new Location(PROVIDER);
   location.setLatitude(latLng1.lat);
   location.setLongitude(latLng1.lng);
   location.setAccuracy(3.0f);
   location.setTime(System.currentTimeMillis());
   location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
   location.setBearing(bearing);
   location.setSpeed(13.4f);
   location.setAltitude(0d);
   return location;
 }
    private void retrieveLocation() {
      Location location = null;
      final Iterator<String> providers =
          mLocationManager.getProviders(new Criteria(), true).iterator();
      while (providers.hasNext()) {
        final Location lastKnownLocation = mLocationManager.getLastKnownLocation(providers.next());
        // pick the most recent location
        if (location == null
            || (lastKnownLocation != null
                && location.getElapsedRealtimeNanos()
                    < lastKnownLocation.getElapsedRealtimeNanos())) {
          location = lastKnownLocation;
        }
      }

      // In the case there is no location available (e.g. GPS fix or network location
      // is not available yet), the longitude of the location is estimated using the timezone,
      // latitude and accuracy are set to get a good average.
      if (location == null) {
        Time currentTime = new Time();
        currentTime.set(System.currentTimeMillis());
        double lngOffset =
            FACTOR_GMT_OFFSET_LONGITUDE * (currentTime.gmtoff - (currentTime.isDst > 0 ? 3600 : 0));
        location = new Location("fake");
        location.setLongitude(lngOffset);
        location.setLatitude(0);
        location.setAccuracy(417000.0f);
        location.setTime(System.currentTimeMillis());
        location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());

        if (DEBUG) {
          Slog.d(TAG, "Estimated location from timezone: " + location);
        }
      }

      setLocation(location);
    }
    @Override
    public void handleMessage(Message msg) {
      super.handleMessage(msg);
      if (1 == msg.what) {

        // 模拟一个 Locaiton 数据,数据要设全,因为 {@link LocationManagerServicer} 内部会做校验
        Location mockLocation = new Location(mTargetProvider);
        mockLocation.setTime(System.currentTimeMillis());
        mockLocation.setLongitude(mFakeLon);
        mockLocation.setLatitude(mFakeLat);
        mockLocation.setAccuracy(100);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
          mockLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE /*
						&& mLocationManager.isProviderEnabled(mTestProvider)*/
            && mLocationManager.isProviderEnabled(mTargetProvider)) {
          mLocationManager.setTestProviderLocation(mTestProvider, mockLocation);

          // 看看伪造是否成功
          Location l = mLocationManager.getLastKnownLocation(mTargetProvider);
          String info;
          if (null == l) {
            info = "mock location false.";
          } else {
            info = "lat&long:" + l.getLatitude() + l.getLongitude();
          }
          ToastUtil.show(mContext, info);
        }
        // 需要每隔几秒重新设置一下伪造的数据,保障伪造数据一直在
        MyHandler.this.sendMessageDelayed(MyHandler.this.obtainMessage(1), 1000);
      } else if (2 == msg.what) {
        mHandler.removeMessages(1);
      }
    }
Beispiel #4
0
  public void mockGpsPoint(int diretion) {

    // 1.将基点坐标转化为UTM坐标;
    String utmLatlon = ConverUtil.latLon2UTM(mLocation.getLatitude(), mLocation.getLongitude());
    int[] xy = UTM2Xy(utmLatlon);

    // 2.将UTM坐标向指定方向偏移;
    x = xy[0];
    y = xy[1];
    if (diretion == TO_LEFT) {
      bearing -= 15;
    } else if (diretion == TO_RIGHT) {
      bearing += 15;
    } else if (diretion == TO_TOP) {
      // 处理前进算法
      double sin = Math.sin(Math.PI * bearing / 180.0);
      double cos = Math.cos(Math.PI * bearing / 180.0);
      int x_dis = (int) (dis_array[mManager.current_Gear] * sin);
      int y_dis = (int) (dis_array[mManager.current_Gear] * cos);
      x += x_dis;
      y += y_dis;
      Log.i(
          MockService.TAG,
          "Math bearing="
              + bearing
              + ",sin="
              + sin
              + ",cos="
              + cos
              + ", x_dis="
              + x_dis
              + ",y_dis="
              + y_dis);
    }

    // 3.将偏移后的UTM坐标转为经纬度坐标;
    String str = "50 R " + x + " " + y;
    double[] latlon = ConverUtil.utm2LatLon(str);
    Log.i(
        MockService.TAG,
        "utmLatlon="
            + utmLatlon
            + ",getLatitude="
            + mLocation.getLatitude()
            + ",getLongitude="
            + mLocation.getLongitude());

    /*同一个经纬度,经纬度转UTM,UTM转经纬度,经纬度再转UTM后,y总是会减1,该算法有误差
    Log.i(MockService.TAG, "utmLatlon1="+str+",latlon1="+latlon[0]+",latlon2="+latlon[1]);
    String utmLatlon0 = ConverUtil.latLon2UTM(latlon[0], latlon[1]);
    int[] xy0 = UTM2Xy(utmLatlon0);
    String str0 = "50 R "+ xy0[0] + " " + xy0[1];
    double[] latlon0 = ConverUtil.utm2LatLon(str0);
    Log.i(MockService.TAG, "utmLatlon2="+utmLatlon0+",latlon2="+latlon0[0]+",latlon2="+latlon0[1]);*/

    mLocation.setLatitude(latlon[0]);
    mLocation.setLongitude(latlon[1]);
    mLocation.setBearing(bearing);
    mLocation.setAccuracy(70);
    mLocation.setSpeed(dis_array[mManager.current_Gear] / 1); // 每秒移动dis_array[i]的距离
    mLocation.setTime(System.currentTimeMillis());

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
      // 没有设该时间,高德地图不会移动
      mLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
    }
    try {
      lm.setTestProviderLocation(providerName, mLocation);
    } catch (SecurityException e) {
      Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG);
      e.printStackTrace();
    }
  }