从广播接收器更新后显示地图

时间:2015-04-30 09:27:28

标签: java android google-maps broadcastreceiver

我希望在通过广播接收器接收到坐标后更新地图。 所以我发给我一个短信,接收器拿到了它,并在地图上放了一个标记。 问题是屏幕上没有显示活动:我手动切换以查看它。短信窗口仍在顶部。有什么想法吗?

此外,标记不在地图中心......

以下是代码:

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);

    }

2 个答案:

答案 0 :(得分:1)

您应该在广播接收时将活动带到前面:

Intent intent = getIntent(getApplicationContext(), MainActivity.class)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// add intent extras
startActivity(intent);

启动另一个摄像机移动会覆盖第一个命令,因此您应该构建一个CameraPosition,它可以缩放并设置动画到位置:

CameraPosition INIT = new CameraPosition.Builder()
        .target(latLng)
        .zoom(zoomLevel)
        .build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(INIT), 250, null);

答案 1 :(得分:0)

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);