MyLocationOverlay or LocationManager to update the current location

I use MapView to display the user's current location on a map. To update the location when the user is on the move, I have a choice:

  • I can use MyLocationOverlay to draw the current location and expand it to track the location of users on the go and have a custom marker. The problem with MyLocationOverlay is that I cannot control the frequency with which the device requests location from GPS. I am worried about the battery leak.
  • I can use the location manager and subscribe using requestLocationUpdates to get the location from GPS. Here I have more control over the frequency at which GPS is requested. I can connect LocationListener and write the code there.

But I read here at SO that this may not be a very good approach to using a location manager, and MyLocationOverlay should be preferable to this.

Also think that my application is a location-based application, the user's location should be tracked when moving it.

Can people suggest which is the best approach to implementation and has a relatively less impact on the battery.

Please note that I am new to Android, so I apologize for any obvious errors.

Thanks in advance Andy

+6
source share
2 answers

I use Little Fluffy Location - its library, you set the time for updates or distance, this library makes a broadcast receiver, implements a LocationListener, n notifies your application when the location changes.

Simple, fast, simple!

Please refer this link:

After changing the location, update your overlay on the map! =)

if you need, I post my complete solution for locations.

+2
source

Using a small fluffy location of 1ΒΊ:

in manifest:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-feature android:name="android.hardware.location" android:required="true" /> <uses-feature android:name="android.hardware.location.gps" android:required="false" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <Application...> <service android:name="com.littlefluffytoys.littlefluffylocationlibrary.LocationBroadcastService" /> <receiver android:name="com.littlefluffytoys.littlefluffylocationlibrary.StartupBroadcastReceiver" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <receiver android:name="com.littlefluffytoys.littlefluffylocationlibrary.PassiveLocationChangedReceiver" android:exported="true" /> 

2ΒΊ Create a class to extend the application

 public class YourApplication extends Application { public void onCreate(){ super.onCreate(); LocationLibrary.showDebugOutput(true); LocationLibrary.initialiseLibrary(getBaseContext(), 0, 2 * 60 * 1000, "com.yourpackage.broadcastpackage"); } public static Context getAppContext() { return ApplicationVexPromoter.context; } } 

3ΒΊ Creating a Broadcast Receiver

 public class Broadcast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { final LocationInfo locationInfo = (LocationInfo) intent.getSerializableExtra(LocationLibraryConstants.LOCATION_BROADCAST_EXTRA_LOCATIONINFO); } } 

And Usage:

in your activity:

 public class YourActivity extends Activity { LocationInfo info; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.id.yourview); info = new LocationInfo(getBaseContext()); double latitude = info.lastLat; double longitude = info.lastLong; } } 

Need more? Refer to this question

It works for me!

+1
source

Source: https://habr.com/ru/post/919742/


All Articles