在活动之间切换GoogleMap

时间:2014-05-17 21:35:44

标签: android

我有两个活动,一个发送坐标,另一个收集和绘制它们。

我需要他们两个都会显示GoogleMap ...在第一个活动中我可以立即看到谷歌地图但是当我从BroadcastReceiver调用第二个活动时我只看到空白的白色屏幕。

这是我的第二个活动代码:

public class NewActivity extends FragmentActivity {
    GoogleMap googleMap;
    String message;
    String number;
    double[] d = new double[4];
    ArrayList<LatLng>  points= new  ArrayList<LatLng>() ;
    @Override
    public void onStart() {

      super.onStart();
        final LocalBroadcastManager localBroadcastManager =
          LocalBroadcastManager.getInstance(this);
       final IntentFilter localFilter = new IntentFilter();

      localBroadcastManager.registerReceiver(localBroadcastReceiver, localFilter);
  }
@Override
    public void onStop() {
       super.onStop();
      final LocalBroadcastManager localBroadcastManager =
               LocalBroadcastManager.getInstance(this);
           // Make sure to unregister!!
           localBroadcastManager.unregisterReceiver(localBroadcastReceiver);
}

    BroadcastReceiver localBroadcastReceiver = new BroadcastReceiver()

        {

            @Override
            public void onReceive(Context context, Intent intent)
           {
                setContentView(R.layout.ye);
                 SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
                 googleMap = fm.getMap();
                 googleMap.setMyLocationEnabled(true);
                 message=intent.getStringExtra("message");
                 number=intent.getStringExtra("number");
                 int p=0;


                    Matcher m = Pattern.compile("(?!=\\d\\.\\d\\.)([\\d.]+)").matcher(message);

                    while(m.find())
                    {
                       double  k = Double.parseDouble(m.group(1));
                       d[p]=k;
                       p++;
                       }




                 PolylineOptions polylineOptions = new PolylineOptions();
                 polylineOptions.color(Color.BLUE);

                    // Setting the width of the polyline
                    polylineOptions.width(6);

                    // Adding the taped point to the ArrayList

                    LatLng coordlocation = new LatLng(d[0], d[1]);
                     points.add(coordlocation);
                     LatLng coordlocation2 = new LatLng(d[2], d[3]);
                     points.add(coordlocation2);


                    // Setting points of polyline
                    polylineOptions.addAll(points);
                    googleMap.addPolyline(polylineOptions);

           }
        };

}

注意:我没有在清单文件中注册localBroadcastReceiver,因为我不知道它是否是必要的。

1 个答案:

答案 0 :(得分:0)

我通过添加以下内容解决了这个问题:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ye);
}
相关问题