fatesses 2018-04-25 13:44 采纳率: 50%
浏览 1460
已采纳

关于安卓开发中谷歌地图在 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;
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 chipyard环境搭建问题
  • ¥20 matlab可以把多个二维图合成为三维瀑布图吗
  • ¥15 EEPROM,软件i2c
  • ¥500 求解读该段JS代码,需要知道是用干什么的
  • ¥20 qt4代码实现二进制文件读取显示,以及显示的内容进行搜索
  • ¥15 Labview获取LK-G3001数据
  • ¥15 我知道什么是混合树,但是怎么写代码啊
  • ¥50 开发板linux系统安装dpkg,apt函数库 有偿
  • ¥15 浏览器时间循环 交互事件和延时事件的 优先级与执行问题
  • ¥15 GD模块安装出错,libgd无法正常安装