Android 使用位置服务更新谷歌地图
我有一个LocationService,它在后台获取用户位置,并向具有纬度和经度的活动发送广播。 它是这个问题的公认答案中的代码 我用Android Studio提供的Google地图活动创建了这个项目。在MapsActivity中,我得到了这样的额外广播Android 使用位置服务更新谷歌地图,android,google-maps,android-location,Android,Google Maps,Android Location,我有一个LocationService,它在后台获取用户位置,并向具有纬度和经度的活动发送广播。 它是这个问题的公认答案中的代码 我用Android Studio提供的Google地图活动创建了这个项目。在MapsActivity中,我得到了这样的额外广播 public class newMessage extends BroadcastReceiver { @Override public void onReceive(Context context, Inte
public class newMessage extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equalsIgnoreCase(LocationService.BROADCAST_ACTION)) {
Bundle extra = intent.getExtras();
latitude = extra.getDouble("Latitude");
longitude = extra.getDouble("Longitude");
System.out.println("Latitude: "+latitude);
System.out.println("Longitude: "+longitude);
LatLng newLocation = new LatLng(latitude,longitude);
}
}
}
我现在想用新的位置更新地图,但我不知道怎么做。我的当前设置是否可行?您需要创建一个locationService,在这里您可以获取当前位置。检查此示例:
public class LocationService extends Service implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = LocationService.class.getSimpleName();
protected GoogleApiClient mGoogleApiClient;
protected LocationRequest mLocationRequest;
protected Location mCurrentLocation;
@Override
public void onCreate() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
buildGoogleApiClient();
mGoogleApiClient.connect();
return START_NOT_STICKY;
}
@Override
public void onConnected(Bundle bundle) {
Log.i("fixedrec", TAG + ">Connected to GoogleApiClient");
if (mCurrentLocation == null) {
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
}
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onDestroy() {
super.onDestroy();
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
Log.d("fixedrec", TAG+ ">StoppingService");
mNM.cancel(NOTIFICATION);
stopForeground(true);
}
@Override
public void onLocationChanged(Location location) {
//plase where you get your locations
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d("fixedrec", TAG + "> Connection failed: ConnectionResult.getErrorCode() = "
+ connectionResult.getErrorCode());
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
Log.i("fixedrec", TAG + "> StartLocationUpdates");
}
protected synchronized void buildGoogleApiClient() {
Log.i("fixedrec", TAG + "> Building GoogleApiClient");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
}
要在应用程序中广播您的位置,您可以使用BroadCastReceiver或类似Otto的EventBus。
然后,只需创建一个谷歌地图,并添加一个带有获取位置的标记
如果您正在处理SDK>=23,请不要忘记在清单文件和代码中写入locationPermissions
你也可以研究这个项目
您需要的一切都在那里。您需要创建一个locationService,从中可以获取您当前的位置。检查此示例:
public class LocationService extends Service implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = LocationService.class.getSimpleName();
protected GoogleApiClient mGoogleApiClient;
protected LocationRequest mLocationRequest;
protected Location mCurrentLocation;
@Override
public void onCreate() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
buildGoogleApiClient();
mGoogleApiClient.connect();
return START_NOT_STICKY;
}
@Override
public void onConnected(Bundle bundle) {
Log.i("fixedrec", TAG + ">Connected to GoogleApiClient");
if (mCurrentLocation == null) {
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
}
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onDestroy() {
super.onDestroy();
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
Log.d("fixedrec", TAG+ ">StoppingService");
mNM.cancel(NOTIFICATION);
stopForeground(true);
}
@Override
public void onLocationChanged(Location location) {
//plase where you get your locations
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d("fixedrec", TAG + "> Connection failed: ConnectionResult.getErrorCode() = "
+ connectionResult.getErrorCode());
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
Log.i("fixedrec", TAG + "> StartLocationUpdates");
}
protected synchronized void buildGoogleApiClient() {
Log.i("fixedrec", TAG + "> Building GoogleApiClient");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
}
要在应用程序中广播您的位置,您可以使用BroadCastReceiver或类似Otto的EventBus。
然后,只需创建一个谷歌地图,并添加一个带有获取位置的标记
如果您正在处理SDK>=23,请不要忘记在清单文件和代码中写入locationPermissions
你也可以研究这个项目
您所需的一切都在那里。在活动中声明广播接收器,并将标记显示到当前位置
public BroadcastReceiver locationUpdateReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
//show current location marker
mMap.addMarker(new MarkerOptions().position(/*your lat long*/).title("My Location")));
}
};
在活动中声明广播接收器并将标记显示到当前位置
public BroadcastReceiver locationUpdateReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
//show current location marker
mMap.addMarker(new MarkerOptions().position(/*your lat long*/).title("My Location")));
}
};
谢谢我来看看这个项目。我已经有了locationservice,并使用广播将位置发送到活动,因此我不知道出了什么问题。但是这个项目可能会让我朝着正确的方向前进。试着编译它,看看它在做什么。然后,您可以将代码复制并粘贴到项目中,以使其正常工作。尽量避免使用广播接收器-这很复杂。改用奥托,我得用广播接收机。这是学校项目的一项要求。但我现在已经开始工作了。谢谢你的帮助。一定会看看Fixedrec3项目和otto!谢谢我来看看这个项目。我已经有了locationservice,并使用广播将位置发送到活动,因此我不知道出了什么问题。但是这个项目可能会让我朝着正确的方向前进。试着编译它,看看它在做什么。然后,您可以将代码复制并粘贴到项目中,以使其正常工作。尽量避免使用广播接收器-这很复杂。改用奥托,我得用广播接收机。这是学校项目的一项要求。但我现在已经开始工作了。谢谢你的帮助。一定会看看Fixedrec3项目和otto!正是我需要的!非常感谢。这正是我需要的!谢谢。