andy
andy

Reputation: 484

MyLocationOverlay or LocationManager for updating current location

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:

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

Upvotes: 6

Views: 2127

Answers (2)

Pierry
Pierry

Reputation: 989

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!

Upvotes: 1

Pierry
Pierry

Reputation: 989

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.

Upvotes: 2

Related Questions