繁体   English   中英

在活动之间切换GoogleMap

[英]Switching GoogleMap between activities

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

我需要他们两个都将显示GoogleMap ...在第一个活动中我可以立即看到google map,但是当我从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,因为我不知道它是否必要。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM