MyLocationOverlay or LocationManager for updating current location
Asked Answered
B

2

6

I am using MapView to show current location of a user on a Map. To update the location while the user is on the move, I have to a choice:

  • I can use MyLocationOverlay to draw the current location and extend it to allow tracking of the users location on the move and to have custom marker. The problem with MyLocationOverlay is that I cannot control the frequency with which the device requests the location from GPS. I am worried about the battery drain here.
  • I can use Location Manager and subscribe using requestLocationUpdates to get location from GPS. Here I have more control over the frequency with which the GPS is queried. I can hook up a LocationListener and write the code in there.

But I have read here at SO that it might not be good approach to use Location Manager and MyLocationOverlay should be preferred over to this.

Also, consider that my app is a location based app the location of the user should be tracked as he moves.

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

Note that I am a beginner in Android, so pardon any obvious mistakes.

Thank you in advance, andy

Blocking answered 5/7, 2012 at 10:28 Comment(4)
Is the GPS provider mandatory? I mean you want precision, right?Newsstand
Yup I want precision. GPS has lower priority than Network location. But still my question stands.Blocking
Well, you can't be accurate and efficient at the same time. If you need accurate location info, use the first variant (proposed by yourself).Newsstand
@bicska88 - Thanks. I am inclined to use MyLocationOverlay. But since we can't control the frequency with which it queries the Providers, I am not sure about the battery drain.Blocking
V
2

I using Little Fluffy Location - Its a library, you setting time for updates or distance, this library make a broadcast receiver, implements LocationListener, n notify your application when location changes.

Simple, fast, easy!

Please refer this link:

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

if you need, i send my full solution for locations.

Volatilize answered 21/11, 2012 at 13:50 Comment(1)
buddy can i tell me how can i use this library ?Capstone
V
1

Using Little Fluffy Location 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 class to extends 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º Create 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 Use:

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? Please refer this

Works for me!

Volatilize answered 26/11, 2012 at 15:50 Comment(1)
I'm adding this to my code, but the broadcast receiver is never called. I don't see in your code above anywhere that references the Broadcast BroadcastReceiver. Where is that set?Horologium

© 2022 - 2024 — McMap. All rights reserved.