fatesses
2018-04-25 13:44
采纳率: 98%
浏览 1.4k

关于安卓开发中谷歌地图在 android fragment

各位大佬,我通过Android studio生成了一个谷歌地图的页面 MapsActivity,这个是自动生成的
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

}
然后我做了一个Fragment的导航:
public class fragmentmap extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_maps, null);
        return view;
    }

}

我将这个activity放入了framentmap

public class fragmentmap extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_maps, null);
return view;
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

}

就是现在项目运行只显示地图,坐标并没有实现,moveCamera也没有运行,求大佬们指点下我哪里出了问题。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 暖暖的云阳 2018-04-26 00:59
    已采纳

    将onCreateView()方法进行修改。
    如下所示:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_blank, container, false);
    mapFragment= (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
    if(mapFragment == null){
    FragmentManager fm= getFragmentManager();
    FragmentTransaction ft= fm.beginTransaction();
    mapFragment= SupportMapFragment.newInstance();
    ft.replace(R.id.map, mapFragment).commit();
    }
    mapFragment.getMapAsync(this);
    return view;
    }

    打赏 评论
  • dac415 2018-04-25 14:11

    我通过Android studio生成了一个谷歌地图的页面 MapsActivity,这个是自动生成的 public class MapsActivity extends FragmentActivity...

    打赏 评论

相关推荐 更多相似问题