From 004567c59b41c42c25a29ce1669a3ff54e9938cb Mon Sep 17 00:00:00 2001 From: vng Date: Mon, 21 May 2012 17:41:20 +0300 Subject: [PATCH] [android] Fix compass direction. Pass angle parameters in radians only. --- .../jni/com/mapswithme/maps/MWMActivity.cpp | 2 +- .../maps/DownloadResourcesActivity.java | 152 +++++++++--------- .../src/com/mapswithme/maps/MWMActivity.java | 124 +++++++------- .../maps/location/LocationService.java | 58 ++++--- iphone/Maps/Classes/SearchVC.mm | 5 +- iphone/Maps/Platform/LocationManager.mm | 11 +- map/location_state.cpp | 7 +- platform/location.hpp | 9 +- 8 files changed, 189 insertions(+), 179 deletions(-) diff --git a/android/jni/com/mapswithme/maps/MWMActivity.cpp b/android/jni/com/mapswithme/maps/MWMActivity.cpp index 143291ad18..15029a2230 100644 --- a/android/jni/com/mapswithme/maps/MWMActivity.cpp +++ b/android/jni/com/mapswithme/maps/MWMActivity.cpp @@ -27,7 +27,7 @@ extern "C" JNIEXPORT void JNICALL Java_com_mapswithme_maps_MWMActivity_nativeCompassUpdated(JNIEnv * env, jobject thiz, - jlong time, jdouble magneticNorth, jdouble trueNorth, jfloat accuracy) + jlong time, jdouble magneticNorth, jdouble trueNorth, jdouble accuracy) { g_framework->OnCompassUpdated(time, magneticNorth, trueNorth, accuracy); } diff --git a/android/src/com/mapswithme/maps/DownloadResourcesActivity.java b/android/src/com/mapswithme/maps/DownloadResourcesActivity.java index faff745cb3..ab8fc7c582 100644 --- a/android/src/com/mapswithme/maps/DownloadResourcesActivity.java +++ b/android/src/com/mapswithme/maps/DownloadResourcesActivity.java @@ -2,13 +2,8 @@ package com.mapswithme.maps; import java.io.File; -import com.mapswithme.maps.location.LocationService; - import android.app.Activity; -import android.app.AlertDialog; -import android.app.Dialog; import android.app.ProgressDialog; -import android.content.DialogInterface; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; @@ -21,14 +16,16 @@ import android.widget.CheckBox; import android.widget.ProgressBar; import android.widget.TextView; +import com.mapswithme.maps.location.LocationService; + public class DownloadResourcesActivity extends Activity implements LocationService.Listener, MapStorage.Listener { private static final String TAG = "DownloadResourcesActivity"; - + private ProgressDialog mDialog = null; // Error codes, should match the same codes in JNI - + private static final int ERR_DOWNLOAD_SUCCESS = 0; private static final int ERR_NOT_ENOUGH_MEMORY = -1; private static final int ERR_NOT_ENOUGH_FREE_SPACE = -2; @@ -52,20 +49,18 @@ public class DownloadResourcesActivity extends Activity implements LocationServi private String mCountryName = null; private boolean mHasLocation = false; private WakeLock mWakeLock = null; - + private int getBytesToDownload() { - return nativeGetBytesToDownload( - mApplication.getApkPath(), - mApplication.getDataStoragePath()); + return nativeGetBytesToDownload(mApplication.getApkPath(), + mApplication.getDataStoragePath()); } - + private void disableAutomaticStandby() { if (mWakeLock == null) { - PowerManager pm = (PowerManager) mApplication.getSystemService( - android.content.Context.POWER_SERVICE); + PowerManager pm = (PowerManager) mApplication.getSystemService(android.content.Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG); mWakeLock.acquire(); } @@ -79,33 +74,33 @@ public class DownloadResourcesActivity extends Activity implements LocationServi mWakeLock = null; } } - + private void setDownloadMessage(int bytesToDownload) { Log.d(TAG, "prepareFilesDownload, bytesToDownload:" + bytesToDownload); if (bytesToDownload < 1024 * 1024) - mMsgView.setText(String.format(getString(R.string.download_resources), - bytesToDownload * 1.0f / 1024, - getString(R.string.kb))); + mMsgView.setText(String.format(getString(R.string.download_resources), + bytesToDownload * 1.0f / 1024, + getString(R.string.kb))); else mMsgView.setText(String.format(getString(R.string.download_resources, - bytesToDownload * 1.0f / 1024 / 1024, - getString(R.string.mb)))); - + bytesToDownload * 1.0f / 1024 / 1024, + getString(R.string.mb)))); + /// set normal text color mMsgView.setTextColor(Color.WHITE); } - + protected void prepareFilesDownload() { if (mBytesToDownload > 0) { setDownloadMessage(mBytesToDownload); - + findViewById(R.id.download_resources_location_progress).setVisibility(View.VISIBLE); findViewById(R.id.download_resources_location_message).setVisibility(View.VISIBLE); - + mLocationService = mApplication.getLocationService(); mLocationService.startUpdate(this); } @@ -122,10 +117,10 @@ public class DownloadResourcesActivity extends Activity implements LocationServi mProgress.setVisibility(View.VISIBLE); mProgress.setMax(mBytesToDownload); - + mDownloadButton.setVisibility(View.GONE); mCancelButton.setVisibility(View.VISIBLE); - + nativeDownloadNextFile(this); } @@ -134,39 +129,39 @@ public class DownloadResourcesActivity extends Activity implements LocationServi mDownloadButton.setVisibility(View.VISIBLE); mCancelButton.setVisibility(View.GONE); } - + public void onTryAgainClicked(View v) { mTryAgainButton.setVisibility(View.GONE); mCancelButton.setVisibility(View.VISIBLE); - + mBytesToDownload = getBytesToDownload(); - + setDownloadMessage(mBytesToDownload); - + nativeDownloadNextFile(this); } - + public void onProceedToMapClicked(View v) { - showMapView(); + showMapView(); } - - + + public String getErrorMessage(int res) { int id; switch (res) { - case ERR_NOT_ENOUGH_FREE_SPACE: id = R.string.not_enough_free_space_on_sdcard; break; - case ERR_STORAGE_DISCONNECTED: id = R.string.disconnect_usb_cable; break; - case ERR_DOWNLOAD_ERROR: id = R.string.download_has_failed; break; - default: id = R.string.not_enough_memory; + case ERR_NOT_ENOUGH_FREE_SPACE: id = R.string.not_enough_free_space_on_sdcard; break; + case ERR_STORAGE_DISCONNECTED: id = R.string.disconnect_usb_cable; break; + case ERR_DOWNLOAD_ERROR: id = R.string.download_has_failed; break; + default: id = R.string.not_enough_memory; } - + return getString(id); } - + public void showMapView() { // Continue with Main UI initialization (MWMActivity) @@ -175,27 +170,27 @@ public class DownloadResourcesActivity extends Activity implements LocationServi mwmActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(mwmActivityIntent); } - + public void finishFilesDownload(int result) { enableAutomaticStandby(); if (result == ERR_NO_MORE_FILES) { Log.i(TAG, "finished files download"); - + if (mHasLocation && mDownloadCountryCheckBox.isChecked()) { mDownloadCountryCheckBox.setVisibility(View.GONE); mMsgView.setText(String.format(getString(R.string.downloading_country_can_proceed), mCountryName)); - + MapStorage.Index idx = mMapStorage.findIndexByName(mCountryName); - + if (idx.isValid()) { mProgress.setMax((int)mMapStorage.countryRemoteSizeInBytes(idx)); mProgress.setProgress(0); - + mMapStorage.downloadCountry(idx); mProceedButton.setVisibility(View.VISIBLE); mCancelButton.setVisibility(View.GONE); @@ -210,26 +205,28 @@ public class DownloadResourcesActivity extends Activity implements LocationServi { mMsgView.setText(getErrorMessage(result)); mMsgView.setTextColor(Color.RED); - + mCancelButton.setVisibility(View.GONE); mDownloadButton.setVisibility(View.GONE); mTryAgainButton.setVisibility(View.VISIBLE); } } - + + @Override public void onCountryStatusChanged(MapStorage.Index idx) { final int status = mMapStorage.countryStatus(idx); - + if (status == MapStorage.ON_DISK) showMapView(); } - + + @Override public void onCountryProgress(MapStorage.Index idx, long current, long total) { mProgress.setProgress((int)current); } - + @Override protected void onCreate(Bundle savedInstanceState) { @@ -239,20 +236,20 @@ public class DownloadResourcesActivity extends Activity implements LocationServi mApplication = (MWMApplication)getApplication(); mMapStorage = MapStorage.getInstance(); - + mSlotId = mMapStorage.subscribe(this); // Create sdcard folder if it doesn't exist new File(mApplication.getDataStoragePath()).mkdirs(); // Used to migrate from v2.0.0 to 2.0.1 - nativeMoveMaps(mApplication.getExtAppDirectoryPath("files"), + nativeMoveMaps(mApplication.getExtAppDirectoryPath("files"), mApplication.getDataStoragePath()); mBytesToDownload = getBytesToDownload(); - + if (mBytesToDownload == 0) showMapView(); - else + else { mMsgView = (TextView)findViewById(R.id.download_resources_message); mProgress = (ProgressBar)findViewById(R.id.download_resources_progress); @@ -264,31 +261,31 @@ public class DownloadResourcesActivity extends Activity implements LocationServi prepareFilesDownload(); } } - + @Override protected void onDestroy() { if (mLocationService != null) { mLocationService.stopUpdate(this); - mLocationService = null; + mLocationService = null; } - + mMapStorage.unsubscribe(mSlotId); - + super.onDestroy(); } - + public void onDownloadProgress(int currentTotal, int currentProgress, int globalTotal, int globalProgress) { - Log.d(TAG, "curTotal:" + currentTotal + ", curProgress:" + currentProgress - + ", glbTotal:" + globalTotal + ", glbProgress:" + globalProgress); - + Log.d(TAG, "curTotal:" + currentTotal + ", curProgress:" + currentProgress + + ", glbTotal:" + globalTotal + ", glbProgress:" + globalProgress); + if (mProgress != null) mProgress.setProgress(globalProgress); } - - public void onDownloadFinished(int errorCode) + + public void onDownloadFinished(int errorCode) { if (errorCode == ERR_DOWNLOAD_SUCCESS) { @@ -299,18 +296,19 @@ public class DownloadResourcesActivity extends Activity implements LocationServi else finishFilesDownload(errorCode); } - + private native void nativeMoveMaps(String fromFolder, String toFolder); private native int nativeGetBytesToDownload(String m_apkPath, String m_sdcardPath); private native void nativeAddCountryToDownload(String countryName, Object observer); private native int nativeDownloadNextFile(Object observer); private native String nativeGetCountryName(double lat, double lon); - + private boolean mReceivedFirstEvent = false; private int mLocationsCount = 0; private final int mLocationsTryCount = 0; - + + @Override public void onLocationUpdated(long time, double lat, double lon, float accuracy) { if (mReceivedFirstEvent) @@ -319,32 +317,34 @@ public class DownloadResourcesActivity extends Activity implements LocationServi { findViewById(R.id.download_resources_location_progress).setVisibility(View.GONE); findViewById(R.id.download_resources_location_message).setVisibility(View.GONE); - + CheckBox checkBox = (CheckBox)findViewById(R.id.download_country_checkbox); - + Log.i(TAG, "Searching for country name at location lat=" + lat + ", lon=" + lon); - + checkBox.setVisibility(View.VISIBLE); mCountryName = nativeGetCountryName(lat, lon); mHasLocation = true; checkBox.setText(String.format(getString(R.string.download_country_ask), mCountryName)); - + mLocationService.stopUpdate(this); mLocationService = null; } - + Log.d(TAG, "tryCount:" + mLocationsCount); ++mLocationsCount; } } - public void onCompassUpdated(long time, double magneticNorth, - double trueNorth, float accuracy) - {} + @Override + public void onCompassUpdated(long time, double magneticNorth, double trueNorth, double accuracy) + { + } + @Override public void onLocationStatusChanged(int status) { if (status == LocationService.FIRST_EVENT) - mReceivedFirstEvent = true; + mReceivedFirstEvent = true; } } diff --git a/android/src/com/mapswithme/maps/MWMActivity.java b/android/src/com/mapswithme/maps/MWMActivity.java index a92de891de..d1f071b96d 100644 --- a/android/src/com/mapswithme/maps/MWMActivity.java +++ b/android/src/com/mapswithme/maps/MWMActivity.java @@ -1,11 +1,5 @@ package com.mapswithme.maps; -import java.io.File; - -import com.mapswithme.maps.R; -import com.mapswithme.maps.location.LocationService; -import com.nvidia.devtech.NvEventQueueActivity; - import android.app.Activity; import android.app.AlertDialog; import android.content.BroadcastReceiver; @@ -14,9 +8,10 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; -import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.os.Environment; +import android.util.DisplayMetrics; +import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -24,11 +19,12 @@ import android.view.MenuItem; import android.view.Surface; import android.view.View; import android.webkit.WebView; -import android.util.DisplayMetrics; -import android.util.Log; + +import com.mapswithme.maps.location.LocationService; +import com.nvidia.devtech.NvEventQueueActivity; public class MWMActivity extends NvEventQueueActivity implements - LocationService.Listener +LocationService.Listener { //VideoTimer m_timer; @@ -49,11 +45,13 @@ public class MWMActivity extends NvEventQueueActivity implements m_shouldStartLocationService = false; } } - + + @Override public void OnRenderingInitialized() { runOnUiThread(new Runnable() { + @Override public void run() { checkShouldStartLocationService(); @@ -62,6 +60,7 @@ public class MWMActivity extends NvEventQueueActivity implements runOnUiThread(new Runnable() { + @Override public void run() { checkMeasurementSystem(); @@ -69,10 +68,12 @@ public class MWMActivity extends NvEventQueueActivity implements }); } + @Override public void ReportUnsupported() { runOnUiThread(new Runnable() { + @Override public void run() { AlertDialog alert = new AlertDialog.Builder(getCurrentContext()).create(); @@ -82,15 +83,16 @@ public class MWMActivity extends NvEventQueueActivity implements alert.setCancelable(false); alert.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.close), - new DialogInterface.OnClickListener() - { - public void onClick(DialogInterface dlg, int which) - { - Activity a = (Activity)getCurrentContext(); - a.moveTaskToBack(true); - dlg.dismiss(); - } - }); + new DialogInterface.OnClickListener() + { + @Override + public void onClick(DialogInterface dlg, int which) + { + Activity a = (Activity)getCurrentContext(); + a.moveTaskToBack(true); + dlg.dismiss(); + } + }); alert.show(); } @@ -119,26 +121,28 @@ public class MWMActivity extends NvEventQueueActivity implements alert.setMessage(getString(R.string.which_measurement_system)); alert.setButton(AlertDialog.BUTTON_NEGATIVE, getString(R.string.miles), - new DialogInterface.OnClickListener() - { - public void onClick(DialogInterface dialog, int which) - { - setMeasurementSystem(UNITS_FOOT); - setupMeasurementSystem(); - dialog.dismiss(); - } - }); + new DialogInterface.OnClickListener() + { + @Override + public void onClick(DialogInterface dialog, int which) + { + setMeasurementSystem(UNITS_FOOT); + setupMeasurementSystem(); + dialog.dismiss(); + } + }); alert.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.kilometres), - new DialogInterface.OnClickListener() - { - public void onClick(DialogInterface dlg, int which) - { - setMeasurementSystem(UNITS_METRIC); - setupMeasurementSystem(); - dlg.dismiss(); - } - }); + new DialogInterface.OnClickListener() + { + @Override + public void onClick(DialogInterface dlg, int which) + { + setMeasurementSystem(UNITS_METRIC); + setupMeasurementSystem(); + dlg.dismiss(); + } + }); alert.show(); } @@ -181,7 +185,7 @@ public class MWMActivity extends NvEventQueueActivity implements { startActivity(new Intent(this, DownloadUI.class)); } - + private MWMApplication mApplication; @Override @@ -197,7 +201,7 @@ public class MWMActivity extends NvEventQueueActivity implements super.onCreate(savedInstanceState); m_context = this; - + mApplication = (MWMApplication)getApplication(); // Get screen density @@ -208,6 +212,7 @@ public class MWMActivity extends NvEventQueueActivity implements } // From Location interface + @Override public void onLocationStatusChanged(int newStatus) { Log.d("LOCATION", "status: " + newStatus); @@ -217,29 +222,30 @@ public class MWMActivity extends NvEventQueueActivity implements } // From Location interface + @Override public void onLocationUpdated(long time, double lat, double lon, float accuracy) { nativeLocationUpdated(time, lat, lon, accuracy); } - public double normalizeAngle(double a) + private double normalizeAngle(double a) { - // normalize magneticNorth into [0, 2PI] and convert to degrees - if (a < 0.0) + // normalize magneticNorth into [0, 2PI] + if (a < 0.0) a += (2.0*Math.PI); a = a % (2.0*Math.PI); - a = a * 180.0 / Math.PI; return a; } - + // From Location interface - public void onCompassUpdated(long time, double magneticNorth, double trueNorth, float accuracy) + @Override + public void onCompassUpdated(long time, double magneticNorth, double trueNorth, double accuracy) { - // correct direction + // correct direction final int screenRotation = getWindowManager().getDefaultDisplay().getOrientation(); - + double correction = 0; - + // correct due to orientation switch (screenRotation) { @@ -253,14 +259,13 @@ public class MWMActivity extends NvEventQueueActivity implements correction = (3.0 * Math.PI / 2.0); break; } - + magneticNorth += correction; trueNorth += correction; magneticNorth = normalizeAngle(magneticNorth); - trueNorth = normalizeAngle(trueNorth); - - + trueNorth = normalizeAngle(trueNorth); + nativeCompassUpdated(time, magneticNorth, trueNorth, accuracy); } @@ -277,8 +282,8 @@ public class MWMActivity extends NvEventQueueActivity implements @Override protected void onPause() { - //< stop update only if it's started in OnRenderingInitialized - if (!m_shouldStartLocationService) + //< stop update only if it's started in OnRenderingInitialized + if (!m_shouldStartLocationService) if (findViewById(R.id.map_button_myposition).isSelected()) mApplication.getLocationService().stopUpdate(this); @@ -337,9 +342,10 @@ public class MWMActivity extends NvEventQueueActivity implements builder.setTitle(R.string.about); builder.setPositiveButton(R.string.close, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - dialog.cancel(); - } + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } }).show(); } @@ -443,5 +449,5 @@ public class MWMActivity extends NvEventQueueActivity implements private native void nativeDestroy(); private native void nativeLocationStatusChanged(int newStatus); private native void nativeLocationUpdated(long time, double lat, double lon, float accuracy); - private native void nativeCompassUpdated(long time, double magneticNorth, double trueNorth, float accuracy); + private native void nativeCompassUpdated(long time, double magneticNorth, double trueNorth, double accuracy); } diff --git a/android/src/com/mapswithme/maps/location/LocationService.java b/android/src/com/mapswithme/maps/location/LocationService.java index ba4eebb86e..3ccb30223d 100644 --- a/android/src/com/mapswithme/maps/location/LocationService.java +++ b/android/src/com/mapswithme/maps/location/LocationService.java @@ -4,8 +4,6 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; -import com.mapswithme.maps.MWMApplication; - import android.content.Context; import android.hardware.GeomagneticField; import android.hardware.Sensor; @@ -20,6 +18,8 @@ import android.os.PowerManager; import android.os.PowerManager.WakeLock; import android.util.Log; +import com.mapswithme.maps.MWMApplication; + public class LocationService implements LocationListener, SensorEventListener, WifiLocation.Listener { private static final String TAG = "LocationService"; @@ -34,7 +34,7 @@ public class LocationService implements LocationListener, SensorEventListener, W public interface Listener { public void onLocationUpdated(long time, double lat, double lon, float accuracy); - public void onCompassUpdated(long time, double magneticNorth, double trueNorth, float accuracy); + public void onCompassUpdated(long time, double magneticNorth, double trueNorth, double accuracy); public void onLocationStatusChanged(int status); }; @@ -46,18 +46,18 @@ public class LocationService implements LocationListener, SensorEventListener, W private WifiLocation m_wifiScanner = null; private LocationManager m_locationManager; - + private SensorManager m_sensorManager; private Sensor m_accelerometer = null; private Sensor m_magnetometer = null; /// To calculate true north for compass private GeomagneticField m_magneticField = null; - + private boolean m_isActive = false; // @TODO Refactor to deliver separate first update notification to each provider, // or do not use it at all in the location service logic private boolean m_reportFirstUpdate = true; - + private WakeLock m_wakeLock = null; private MWMApplication mApplication = null; @@ -89,14 +89,14 @@ public class LocationService implements LocationListener, SensorEventListener, W it.next().onLocationUpdated(time, lat, lon, accuracy); } - private void notifyCompassUpdated(long time, double magneticNorth, double trueNorth, float accuracy) + private void notifyCompassUpdated(long time, double magneticNorth, double trueNorth, double accuracy) { Iterator it = m_observers.iterator(); while (it.hasNext()) it.next().onCompassUpdated(time, magneticNorth, trueNorth, accuracy); } - + private void disableAutomaticStandby() { if (m_wakeLock == null) @@ -190,7 +190,7 @@ public class LocationService implements LocationListener, SensorEventListener, W m_locationManager.removeUpdates(this); if (m_sensorManager != null) m_sensorManager.unregisterListener(this); - + m_isActive = false; m_reportFirstUpdate = true; m_magneticField = null; @@ -232,7 +232,7 @@ public class LocationService implements LocationListener, SensorEventListener, W // Check if the old and new location are from the same provider final boolean isFromSameProvider = isSameProvider(newLocation.getProvider(), - currentBestLocation.getProvider()); + currentBestLocation.getProvider()); // Situation when last known location is equal to the new one if (timeDelta == 0 && isFromSameProvider) @@ -269,6 +269,7 @@ public class LocationService implements LocationListener, SensorEventListener, W private final static float HUNDRED_METRES = 100.0f; //@Override + @Override public void onLocationChanged(Location l) { if (isBetterLocation(l, m_lastLocation)) @@ -292,14 +293,14 @@ public class LocationService implements LocationListener, SensorEventListener, W } private native float[] nativeUpdateCompassSensor(int ind, float[] arr); - private float[] UpdateCompassSensor(int ind, float[] arr) + private float[] updateCompassSensor(int ind, float[] arr) { /* Log.d(TAG, "Sensor before, Java: " + String.valueOf(arr[0]) + ", " + String.valueOf(arr[1]) + ", " + String.valueOf(arr[2])); - */ + */ float[] ret = nativeUpdateCompassSensor(ind, arr); @@ -308,27 +309,28 @@ public class LocationService implements LocationListener, SensorEventListener, W String.valueOf(ret[0]) + ", " + String.valueOf(ret[1]) + ", " + String.valueOf(ret[2])); - */ + */ return ret; } private float[] m_gravity = null; private float[] m_geomagnetic = null; - + //@Override + @Override public void onSensorChanged(SensorEvent event) { // Get the magnetic north (orientation contains azimut, pitch and roll). float[] orientation = null; - + if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { - m_gravity = UpdateCompassSensor(0, event.values); + m_gravity = updateCompassSensor(0, event.values); } if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { - m_geomagnetic = UpdateCompassSensor(1, event.values); + m_geomagnetic = updateCompassSensor(1, event.values); } if (m_gravity != null && m_geomagnetic != null) @@ -341,52 +343,60 @@ public class LocationService implements LocationListener, SensorEventListener, W SensorManager.getOrientation(R, orientation); } } - + if (orientation != null) { double north = orientation[0]; if (m_magneticField == null) - notifyCompassUpdated(event.timestamp, north, -1.0, 10.0f); + { + // -1.0 - as default parameters + notifyCompassUpdated(event.timestamp, north, -1.0, -1.0); + } else { // positive 'offset' means the magnetic field is rotated east that much from true north - final float offset = m_magneticField.getDeclination(); + final double offset = m_magneticField.getDeclination() * Math.PI / 180.0; double trueNorth = north - offset; - - if (trueNorth < 0.0) - trueNorth += 360.0; - + + if (trueNorth < 0.0) + trueNorth += (2.0 * Math.PI); + notifyCompassUpdated(event.timestamp, north, trueNorth, offset); } } } //@Override + @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { //Log.d(TAG, "Compass accuracy changed to " + String.valueOf(accuracy)); } //@Override + @Override public void onProviderDisabled(String provider) { Log.d(TAG, "Disabled location provider: " + provider); } //@Override + @Override public void onProviderEnabled(String provider) { Log.d(TAG, "Enabled location provider: " + provider); } //@Override + @Override public void onStatusChanged(String provider, int status, Bundle extras) { Log.d(TAG, String.format("Status changed for location provider: %s to %d", provider, status)); } //@Override + @Override public void onWifiLocationUpdated(Location l) { if (l != null) diff --git a/iphone/Maps/Classes/SearchVC.mm b/iphone/Maps/Classes/SearchVC.mm index e408834579..3fc5a72b2c 100644 --- a/iphone/Maps/Classes/SearchVC.mm +++ b/iphone/Maps/Classes/SearchVC.mm @@ -499,7 +499,7 @@ static void OnSearchResultCallback(search::Results const & res, int queryId) if (![m_locationManager getLat:lat Lon:lon]) return; - double const northDeg = (info.m_trueHeading < 0) ? info.m_magneticHeading : info.m_trueHeading; + double const northRad = (info.m_trueHeading < 0) ? info.m_magneticHeading : info.m_trueHeading; NSArray * cells = [m_table visibleCells]; for (NSUInteger i = 0; i < cells.count; ++i) { @@ -522,8 +522,7 @@ static void OnSearchResultCallback(search::Results const & res, int queryId) CompassView * compass = (CompassView *)cell.accessoryView; m2::PointD const center = res.GetFeatureCenter(); compass.angle = ang::AngleTo(m2::PointD(MercatorBounds::LonToX(lon), - MercatorBounds::LatToY(lat)), center) + - northDeg / 180. * math::pi; + MercatorBounds::LatToY(lat)), center) + northRad; } } } diff --git a/iphone/Maps/Platform/LocationManager.mm b/iphone/Maps/Platform/LocationManager.mm index 3e50aaf4ba..5c15567245 100644 --- a/iphone/Maps/Platform/LocationManager.mm +++ b/iphone/Maps/Platform/LocationManager.mm @@ -139,13 +139,12 @@ - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { location::CompassInfo newInfo; - newInfo.m_magneticHeading = newHeading.magneticHeading; - newInfo.m_trueHeading = newHeading.trueHeading; - newInfo.m_accuracy = newHeading.headingAccuracy; - //newInfo.m_x = newHeading.x; - //newInfo.m_y = newHeading.y; - //newInfo.m_z = newHeading.z; + newInfo.m_magneticHeading = my::DegToRad(newHeading.magneticHeading); + newInfo.m_trueHeading = my::DegToRad(newHeading.trueHeading); + newInfo.m_accuracy = my::DegToRad(newHeading.headingAccuracy); + newInfo.m_timestamp = [newHeading.timestamp timeIntervalSince1970]; + for (id observer in m_observers) [observer onCompassUpdate:newInfo]; } diff --git a/map/location_state.cpp b/map/location_state.cpp index 732e4dbf1c..7dea63339f 100644 --- a/map/location_state.cpp +++ b/map/location_state.cpp @@ -31,12 +31,11 @@ namespace location m_headingRad = ((info.m_trueHeading >= 0.0) ? info.m_trueHeading : info.m_magneticHeading); // 0 angle is for North ("up"), but in our coordinates it's to the right. - m_headingRad = my::DegToRad(m_headingRad) - math::pi / 2.0; + m_headingRad = m_headingRad - math::pi / 2.0; // Avoid situations when offset between magnetic north and true north is too small - static double const MIN_SECTOR_DEG = 10.0; - m_headingHalfSectorRad = (info.m_accuracy < MIN_SECTOR_DEG ? MIN_SECTOR_DEG : info.m_accuracy); - m_headingHalfSectorRad = my::DegToRad(m_headingHalfSectorRad); + static double const MIN_SECTOR_RAD = math::pi / 18.0; + m_headingHalfSectorRad = (info.m_accuracy < MIN_SECTOR_RAD ? MIN_SECTOR_RAD : info.m_accuracy); } void State::DrawMyPosition(DrawerYG & drawer, Navigator const & nav) diff --git a/platform/location.hpp b/platform/location.hpp index 46b8417351..af7ce21189 100644 --- a/platform/location.hpp +++ b/platform/location.hpp @@ -46,12 +46,9 @@ namespace location { public: double m_timestamp; //!< how many seconds ago the heading was retrieved - double m_magneticHeading; //!< positive degrees from the magnetic North - double m_trueHeading; //!< positive degrees from the true North - double m_accuracy; //!< offset from magnetic to true North -// int m_x; -// int m_y; -// int m_z; + double m_magneticHeading; //!< positive radians from the magnetic North + double m_trueHeading; //!< positive radians from the true North + double m_accuracy; //!< offset from the magnetic to the true North in radians }; static inline bool IsLatValid(double lat)