问题描述
我正在开发与该菜单里面的一些按键在顶部菜单的应用程序。我想谷歌地图添加到按钮中的一个,但我不是很成功。我在下面的本教程但不是implementig直接google地图进入 MainActivity
我说他们到时候按钮pressed时启动一个片段。所有工作的罚款。当我preSS的按钮,地图加载,他们,他们应该工作。当我preSS的home键返回到 MainActivity
它完美的作品,但是当我想再次加载地图它给了我调试错误:类文件编辑:未找到源
这是 Google地图
片段code:
公共类的GoogleMaps扩展片段{
私人GoogleMap的GoogleMap的;
双纬度= 46.514249;
双东经= 15.080183;
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
最后查看myFragmentView = inflater.inflate(R.layout.maps_layout,集装箱,假);
尝试 {
//载入地图
initilizeMap();
}赶上(例外五){
Log.e(错误,错误code:+ e.toString());
e.printStackTrace();
}
返回myFragmentView;
}
私人无效initilizeMap(){
如果(GoogleMap的== NULL){
使用GoogleMap =((MapFragment)getFragmentManager()findFragmentById(R.id.map)。)的GetMap()。
(新MarkerOptions()位置(新的经纬度(纬度,经度)))googleMap.addMarker;
CameraPosition cameraPosition =新CameraPosition.Builder()。目标(
新经纬度(纬度,经度))放大(12).build()。
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
//如果映射成功与否创建检查
如果(GoogleMap的== NULL){
Toast.makeText(getActivity(),
对不起!无法创建地图,Toast.LENGTH_SHORT)
。显示();
}
}
}
}
任何帮助将AP preciated。
编辑:如果我是不够具体。它的工作原理,当我打开片段
一次(第一次),但是当我尝试再次装入(第2次),这是行不通的。
编辑版:我居然发现自己的解决方案。我所要做的就是添加 OnDestroyView
方法:
公共无效onDestroyView()
{
super.onDestroyView();
片段片段=(getFragmentManager()findFragmentById(R.id.map));
FragmentTransaction英尺= getActivity()getFragmentManager()的BeginTransaction()。
ft.remove(片段);
ft.commit();
}
不管怎样,谢谢您。
公共类MapCurrentLoactionFragment扩展片段{
公共静态图形页面图形页面;
公共静态GoogleMap的地图;
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
视图V = inflater.inflate(R.layout.fragment_currentlocation_map,
集装箱,假);
尝试
{
//获取从XML布局的图形窗口和创建它
图形页面=(图形页面)v.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
mapView.onResume();
//获取从图形页面来使用GoogleMap并执行初始化的东西
地图= mapView.getMap();
//更改地图类型
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
// googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
// googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
//显示/隐藏您的当前位置
map.setMyLocationEnabled(假);
//启用/禁用变焦控制
map.getUiSettings()setZoomControlsEnabled(真)。
//启用/禁用我的位置按钮
map.getUiSettings()setMyLocationButtonEnabled(真)。
//启用/禁用罗盘图标
map.getUiSettings()setCompassEnabled(真)。
//启用/禁用旋转手势
map.getUiSettings()setRotateGesturesEnabled(真)。
//启用/禁用变焦功能
map.getUiSettings()setZoomGesturesEnabled(真)。
MapsInitializer.initialize(this.getActivity());
}
赶上(例外五)
{
的System.out.println(E);
}
返回伏;
}
XML:
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
< com.google.android.gms.maps.MapView
机器人:ID =@ + ID /图形页面
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT/>
< / LinearLayout中>
I'm developing an app with a menu on top with some buttons inside that menu. I'm trying to add google maps to one of the button, but I'm not very successful. I was following this tutorial but instead of implementig google maps directly into MainActivity
I added them into a fragment that is launched when button is pressed. All worked fine. When I press the button the maps load and they work as they should. When I press the home button to go back to MainActivity
it works perfectly, but when I want to load maps again it gives me debugging error: Class File Editor: Source not found
This is code of GoogleMaps
fragment:
public class GoogleMaps extends Fragment{
private GoogleMap googleMap;
double latitude = 46.514249;
double longitude = 15.080183;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View myFragmentView = inflater.inflate(R.layout.maps_layout, container, false);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
e.printStackTrace();
}
return myFragmentView;
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)));
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(latitude, longitude)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getActivity(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
}
Any help will be appreciated.
EDIT:If I wasn't specific enough. It works when I load fragment
once (for the first time) but it doesn't work when I try to load it again (second time).
EDIT v2:I actually found solution myself.All I had to do was to add OnDestroyView
method:
public void onDestroyView()
{
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
Thanks anyway.
public class MapCurrentLoactionFragment extends Fragment {
public static MapView mapView;
public static GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_currentlocation_map,
container, false);
try
{
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
mapView.onResume();
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
// Changing map type
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
// googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
// googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
// Showing / hiding your current location
map.setMyLocationEnabled(false);
// Enable / Disable zooming controls
map.getUiSettings().setZoomControlsEnabled(true);
// Enable / Disable my location button
map.getUiSettings().setMyLocationButtonEnabled(true);
// Enable / Disable Compass icon
map.getUiSettings().setCompassEnabled(true);
// Enable / Disable Rotate gesture
map.getUiSettings().setRotateGesturesEnabled(true);
// Enable / Disable zooming functionality
map.getUiSettings().setZoomGesturesEnabled(true);
MapsInitializer.initialize(this.getActivity());
}
catch(Exception e)
{
System.out.println(e);
}
return v;
}
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.gms.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
这篇关于Android的谷歌地图中的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!