gpt4 book ai didi

android - 从 Activity 添加标记到 fragment 映射

转载 作者:行者123 更新时间:2023-11-29 01:08:41 25 4
gpt4 key购买 nike

我需要从 MainActivity 向 Android Google map ( fragment )添加标记

这是我的代码;

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 与我的 map 进行交互,那里有一些按钮和 EditText

主 Activity

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();
}

Here is how it looks, already has a mark, but insted of calling it from the map activity i need to set it from the MainActivity

最佳答案

你只需要在 Fragment 中定义一个可以从 Activity 调用的公共(public)方法:

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 初始启动时放置标记,则需要在 FragmentTransaction 中使用参数。 See here for more details .

关于android - 从 Activity 添加标记到 fragment 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45064713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com