2016-04-07 20 views
10

Tôi đang phát triển ứng dụng la bàn ở đó tôi muốn la bàn trỏ đến một vị trí kinh độ vĩ độ cụ thể thay vì phía bắc thông thường. Tôi tìm thấy một số questions liên quan đến vấn đề của tôi nhưng tôi không quản lý để làm cho họ làm việc cho tôi. Đây là mã của tôi:Điểm la bàn Android đến vị trí của tôi thay vì hướng bắc

public class MainActivity extends AppCompatActivity implements SensorEventListener { 
    private ImageView image; 
    private float currentDegree = 0f; 
    private SensorManager mSensorManager; 
    private TextView tvHeading; 
    private Location location = new Location("A"); 
    private Location target = new Location("B"); 
    private LocationManager locationManager; 

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

     image = (ImageView) findViewById(R.id.imageViewCompass); 
     tvHeading = (TextView) findViewById(R.id.tvHeading); 

     mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 

     location.setLatitude(54.903535); 
     location.setLongitude(23.979342); 

     target.setLatitude(54.904618); 
     target.setLongitude(23.978782); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), 
       SensorManager.SENSOR_DELAY_GAME); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     mSensorManager.unregisterListener(this); // to stop the listener and save battery 
    } 

    @Override 
    public void onSensorChanged(SensorEvent event) { 
     float degree = Math.round(event.values[0]); 
     tvHeading.setText("Heading: " + Float.toString(degree) + " degrees"); 
     RotateAnimation ra = new RotateAnimation(
       currentDegree, 
       -degree, 
       Animation.RELATIVE_TO_SELF, 0.5f, 
       Animation.RELATIVE_TO_SELF, 
       0.5f); 
     ra.setDuration(210); 
     ra.setFillAfter(true); 

     image.startAnimation(ra); 
     currentDegree = -degree; 
    } 

    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
     // not in use 
    } 
} 

hiện tại con trỏ của tôi chỉ hiển thị ở phía bắc thay vì vị trí mục tiêu bị bẻ khóa của tôi.

+0

Tôi đoán đây là không chính xác bạn đang tìm kiếm nhưng hãy thử giải pháp được chấp nhận, bạn có thể nhận được một số trợ giúp http://stackoverflow.com/questions/4308262/calculate-compass-bearing-heading-to-location-in-android. – Raghavendra

Trả lời

2

Tôi nghĩ rằng tôi đã tìm thấy cách để làm điều đó: Dưới đây là phương pháp tôi đã thay đổi để có được hành vi dự kiến ​​(Dựa trên this câu hỏi và CookieMonster câu trả lời)

@Override 
    public void onSensorChanged(SensorEvent event) { 
     // get the angle around the z-axis rotated 
     float degree = Math.round(event.values[0]); 
     degree += geoField.getDeclination(); 

     float bearing = location.bearingTo(target); 
     degree = (bearing - degree) * -1; 
     degree = normalizeDegree(degree); 

     tvHeading.setText("Heading: " + Float.toString(degree) + " degrees"); 

     // create a rotation animation (reverse turn degree degrees) 
     RotateAnimation ra = new RotateAnimation(
       currentDegree, 
       -degree, 
       Animation.RELATIVE_TO_SELF, 0.5f, 
       Animation.RELATIVE_TO_SELF, 
       0.5f); 

     // how long the animation will take place 
     ra.setDuration(210); 

     // set the animation after the end of the reservation status 
     ra.setFillAfter(true); 

     // Start the animation 
     image.startAnimation(ra); 
     currentDegree = -degree; 
    } 

private float normalizeDegree(float value) { 
     if (value >= 0.0f && value <= 180.0f) { 
      return value; 
     } else { 
      return 180 + (180 + value); 
     } 
    } 
Các vấn đề liên quan