我需要从MainActivity
向Android Google地图(片段)添加标记这是我的代码;
class Map extends android.app.Fragment implements OnMapReadyCallback
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_mapa, container,false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng marker = new LatLng(19.33978502, -99.19086277);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 15));
googleMap.addMarker(new MarkerOptions().title("Marca de Prueba 1").position(marker));
}
}
我想在这里添加一个标记:
这一切都是因为我想从我的主Activity中与我的地图进行交互,其中我有一些按钮和EditText
MainActivity
public class MainActivityextends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_side_bar);
}
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new Mapa()).commit();
}
答案 0 :(得分:1)
您只需要在Fragment中定义一个可以从Activity调用的公共方法:
class Mapa extends android.app.Fragment implements OnMapReadyCallback
GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_mapa, container,false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
//Added public method to be called from the Activity
public void placeMarker(String title, double lat, double lon) {
if (mMap != null) {
LatLng marker = new LatLng(lat, lon);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 15));
mMap.addMarker(new MarkerOptions().title(title).position(marker));
}
}
}
然后,在Activity中,保留对Fragment的引用,以便您可以调用placeMarker()
方法:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private Mapa mMapFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_side_bar);
}
mMapFragment = new Mapa();
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, mMapFragment).commit();
}
private void placeMarkerInMap(String title, double lat, double lon) {
if (mMapFragment != null) {
mMapFragment.placeMarker(title, lat, lon);
}
}
}
然后,当用户填写EditText并单击按钮时,您可以在Activity中调用placeMarkerInMap()
方法。
请注意,如果您尝试在执行onMapReady()
回调之前调用此方法,则不会有用于放置标记的有效GoogleMap参考。
如果需要在Activity的初始启动时放置Marker,则需要在FragmentTransaction中使用参数。 See here for more details