[英]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.