[英]Display map after update from broadcast receiver
I'd like to update a map, after receiving the coordinates through a broadcast receiver. 通过广播接收器接收坐标后,我想更新地图。 So I send me a sms, the receiver got it, and put a marker on the map. 所以我给我发了一条短信,接收者收到了短信,然后在地图上标记了一个标记。 The problem is that the activity is not shown on screen : I've to switch manually to see it. 问题是该活动未显示在屏幕上:我必须手动切换才能看到它。 The sms windows remains on the top. 短信窗口保留在顶部。 Any Idea ? 任何想法 ?
Besides, the marker isn't centered in the map... 此外,标记不在地图中居中...
Here is the code : 这是代码:
public class MainActivity extends Activity
{
private GoogleMap mMap;
private ArrayList<MyMarker> mMyMarkersArray = new ArrayList<MyMarker>();
private HashMap<Marker, MyMarker> mMarkersHashMap;
private BroadcastReceiver updateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
Log.e("DEBUG", "private broadcast" );
Double longitude = intent.getDoubleExtra("long",0);
Double latitude = intent.getDoubleExtra("lat",0);
Log.e("DEBUG", "gotsms : " + longitude + " et " + latitude );
mMarkersHashMap = new HashMap<Marker, MyMarker>();
mMyMarkersArray.add(new MyMarker("I'm here", "icon1", latitude, longitude));
LatLng latLng = new LatLng(latitude, longitude);
updateMap(intent, latLng);
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addAction(UPDATE_MAP);
registerReceiver(updateReceiver, filter);
setUpMap();
}
protected void updateMap(Intent intent, LatLng latLng) {
plotMarkers(mMyMarkersArray);
CameraUpdate center=CameraUpdateFactory.newLatLng(latLng);
CameraUpdate zoom=CameraUpdateFactory.zoomTo(12);
mMap.moveCamera(center);
mMap.animateCamera(zoom);
}
You should bring the activity to front on broadcast receive: 您应该在广播中将活动放在最前面:
Intent intent = getIntent(getApplicationContext(), MainActivity.class)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// add intent extras
startActivity(intent);
Starting another camera movement would override the first command so you should build a CameraPosition
which does zoom and animate to position: 开始另一个摄影机移动将覆盖第一个命令,因此您应该构建一个CameraPosition
,它可以缩放并设置动画位置:
CameraPosition INIT = new CameraPosition.Builder()
.target(latLng)
.zoom(zoomLevel)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(INIT), 250, null);
After 2 hours, here is how I finally got it : 2小时后,这就是我最终得到的方法:
Intent newIntent = new Intent();
newIntent.setClassName("com.mypackage", "com.mypackage.MainActivity");
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(newIntent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.