Android 如何访问BroadcastReceiver中用于另一个函数(map)的变量?
我有一个Broadcastreceiver,工作正常,可以接收通过蓝牙发送的数据。它将数据拆分为纬度和经度值 BroadcastReceiver已在OnCreate()中注册 我想使用这些btlatitude和btlatitude值在Google地图中绘制一个标记。我在同一个活动中包含了一个onMapReady(mMap)函数,但值总是为0,0,这是默认值。请帮帮我Android 如何访问BroadcastReceiver中用于另一个函数(map)的变量?,android,google-maps,broadcastreceiver,Android,Google Maps,Broadcastreceiver,我有一个Broadcastreceiver,工作正常,可以接收通过蓝牙发送的数据。它将数据拆分为纬度和经度值 BroadcastReceiver已在OnCreate()中注册 我想使用这些btlatitude和btlatitude值在Google地图中绘制一个标记。我在同一个活动中包含了一个onMapReady(mMap)函数,但值总是为0,0,这是默认值。请帮帮我 @Override public void onMapReady(GoogleMap googleMap) {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap= googleMap;
latlng = new LatLng(btlatitude,btlongitude);
mm=mMap.addMarker(new MarkerOptions().position(latlng).title("My Position"));
Log.i("text sample lat in bt", String.valueOf(btlatitude));
Log.i("text sample long in bt", String.valueOf(btlongitude));
}
您可以在google地图上创建显示标记的方法
public void showMarker(LatLng latlng){
mMap.addMarker(new MarkerOptions().position(latlng).title("My Position"));
}
从onReceive呼叫它
BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String text = intent.getStringExtra("theMessage");
Log.i("text sample", text);
String coordinates[] = text.split("&");
btlatitude = Double.parseDouble(coordinates[1]);
btlongitude = Double.parseDouble(coordinates[0]);
showMarker(new LatLng(btlatitude,btlongitude));
}
};
谢谢你的评论。我会试试这个。但是我要删除onMapReady函数吗?
BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String text = intent.getStringExtra("theMessage");
Log.i("text sample", text);
String coordinates[] = text.split("&");
btlatitude = Double.parseDouble(coordinates[1]);
btlongitude = Double.parseDouble(coordinates[0]);
showMarker(new LatLng(btlatitude,btlongitude));
}
};