MyLocationOverlay or LocationManager for updating current location

为君一笑 提交于 2019-12-06 01:07:00

问题


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


回答1:


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.




回答2:


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!



来源:https://stackoverflow.com/questions/11342449/mylocationoverlay-or-locationmanager-for-updating-current-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!