片段中的 Android 谷歌地图

Posted

技术标签:

【中文标题】片段中的 Android 谷歌地图【英文标题】:Android Google Maps in Fragment 【发布时间】:2013-11-04 04:46:10 【问题描述】:

我正在开发一个顶部有一个菜单的应用程序,该菜单内有一些按钮。我正在尝试将谷歌地图添加到其中一个按钮,但我不是很成功。我在关注this tutorial,但没有直接将谷歌地图实现到MainActivity,而是将它们添加到按下按钮时启动的片段中。一切正常。当我按下按钮时,地图会加载并且它们会正常工作。当我按下主页按钮返回MainActivity 时,它工作得很好,但是当我想再次加载地图时,它给了我debugging error: Class File Editor: Source not found

这是GoogleMaps片段的代码:

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

任何帮助将不胜感激。

编辑: 如果我不够具体。当我加载fragment 一次(第一次)时它可以工作,但当我尝试再次加载它时它不起作用(第二次)。

编辑 v2: 我实际上自己找到了解决方案。 我所要做的就是添加OnDestroyView 方法:

public void onDestroyView() 
 
         super.onDestroyView(); 
         Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));  
         FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
         ft.remove(fragment);
         ft.commit();
 

还是谢谢。

【问题讨论】:

我也面临同样的问题,我确实添加了onDestroyView() 方法,但它没有帮助.. 如果你能帮助我 如果你不解释你的问题,如果我没有看到代码,我帮不了你...... 感谢 Izak,但我已经解决了。:) 为我工作@Izak,非常感谢! :) 它可以工作,但它在方向更改时崩溃。我该怎么办?请帮忙 【参考方案1】:
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_
    android:layout_ >

    <com.google.android.gms.maps.MapView
         android:id="@+id/mapview"
        android:layout_ 
        android:layout_ />

</LinearLayout>

【讨论】:

【参考方案2】:

看看我的实现,它工作得很好,并且在尝试显示地图之前对设备进行一些检查,就像安装了谷歌播放服务一样。也许它会帮助你。我正在使用一个简单的 FragmentActivity 并膨胀包含 SupportedMapFragment 的布局。

代码如下:

public class LocationActivity extends FragmentActivity 

    private GoogleMap map;
    private long rec_id;
    private String address;
    private Marker selectedLoc;
    private boolean isSessionClosed;

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        BugSenseHandler.initAndStartSession(this, Constants.BugSenseKEY);
        setContentView(R.layout.preview_location);
        RelativeLayout cancel_btn = (RelativeLayout) findViewById(R.id.cancel_btn);
        LinearLayout save_location_btn = (LinearLayout) findViewById(R.id.delete_btn);

        save_location_btn.setOnClickListener(new OnClickListener() 

            @Override
            public void onClick(View v) 

                if (address != null) 
                    ReceiptDataSource r = new ReceiptDataSource();
                    r.updateReceiptLocation(rec_id, address);
                    Intent returnIntent = new Intent(getBaseContext(), EditReceiptActivity.class);
                    returnIntent.putExtra("result", address);
                    setResult(RESULT_OK, returnIntent);
                    finish();
                 else 
                    Utils.showToast(getApplicationContext(), getString(R.string.c_unknownLocation), Toast.LENGTH_SHORT);
                
            
        );

        cancel_btn.setOnClickListener(new OnClickListener() 

            @Override
            public void onClick(View v) 
                onBackPressed();
            
        );

        if (map == null) 

            map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

            if (isGoogleMapsInstalled()) 
                if (map != null) 
                    retrieveLocation();
                
             else 
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Install Google Maps");
                builder.setCancelable(false);
                builder.setPositiveButton("Install", new DialogInterface.OnClickListener() 
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) 
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.google.android.apps.maps"));
                        startActivity(intent);

                        //Finish the activity so they can't circumvent the check
                        finish();
                    
                );
                AlertDialog dialog = builder.create();
                dialog.show();
            
        

    

    @Override
    protected void onResume() 
        super.onResume();
        if (isSessionClosed) 
            BugSenseHandler.startSession(this);
            isSessionClosed = false;
        
    

    @Override
    protected void onPause() 
        super.onPause();
        BugSenseHandler.closeSession(this);
        isSessionClosed = true;
    

    @Override
    public void onBackPressed() 
        map.clear();
        super.onBackPressed();
    

    private void retrieveLocation() 
        Intent intent = getIntent();
        address = intent.getStringExtra("location");
        assert address != null;
        if (address.equalsIgnoreCase("")) 
            address = Utils.getCurrentLocation(LocationActivity.this);
        
        rec_id = intent.getLongExtra("receipt_to_update_location", 0);
        final Geocoder geocoder = new Geocoder(this, Locale.US);
        double latitude = 0, longitude = 0;
        try 
            List<Address> loc = geocoder.getFromLocationName(address, 5);
            if (loc.size() > 0) 
                latitude = loc.get(0).getLatitude();
                longitude = loc.get(0).getLongitude();
             else 
                //Toast.makeText(getBaseContext(), getResources().getString(R.string.UnableToFindLocation),Toast.LENGTH_SHORT).show();
                Utils.showToast(LocationActivity.this, getString(R.string.UnableToFindLocation), Toast.LENGTH_SHORT);
            


            selectedLoc = map.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title(address).draggable(true));
            map.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() 
                @Override
                public void onMarkerDragStart(Marker marker) 

                

                @Override
                public void onMarkerDrag(Marker marker) 

                

                @Override
                public void onMarkerDragEnd(Marker marker) 
                    try 
                        List<Address> addresses = geocoder.getFromLocation(selectedLoc.getPosition().latitude, selectedLoc.getPosition().longitude, 1);
                        StringBuilder sb = new StringBuilder();
                        if (addresses.size() > 0) 
                            Address address = addresses.get(0);

                            if (address.getAddressLine(0) != null)
                                sb.append(address.getAddressLine(0)).append(", ");
                            if (address.getLocality() != null)
                                sb.append(address.getLocality()).append(", ");
                            if (address.getCountryName() != null)
                                sb.append(address.getCountryName());
                        

                        address = sb.toString();
                     catch (IOException e) 
                    

                
            );

            map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 12));

            // Zoom in, animating the camera.
            map.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);

         catch (IOException e) 
            Log.e("IOException", e.getMessage());
            //Toast.makeText(this, getString(R.string.c_unknownLocation), Toast.LENGTH_LONG).show();
            Utils.showToast(LocationActivity.this, getString(R.string.c_unknownLocation), Toast.LENGTH_LONG);
        

    

    public boolean isGoogleMapsInstalled() 
        try 
            ApplicationInfo info = getPackageManager().getApplicationInfo("com.google.android.apps.maps", 0);
            return true;
         catch (PackageManager.NameNotFoundException e) 
            return false;
        
    

以及对应的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:custom="http://schemas.android.com/apk/res/ro.gebs.captoom"
              android:id="@+id/location_stuff"
              android:layout_
              android:layout_
              android:background="@color/greenish"
              android:orientation="vertical">

    <RelativeLayout
            android:id="@+id/location_header"
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:background="@color/greenish">

        <RelativeLayout
                android:id="@+id/cancel_btn"
                android:layout_
                android:layout_
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="20dp"
                android:background="@drawable/done_rounded_btn"
                android:gravity="center">

            <ro.gebs.captoom.utils.fonts.CustomFontTextView
                    android:id="@+id/retake_txt"
                    android:layout_
                    android:layout_
                    android:layout_centerInParent="true"
                    android:layout_marginTop="10dp"
                    android:gravity="center"
                    android:paddingLeft="7dp"
                    android:paddingRight="7dp"
                    android:text="@string/cancel"
                    android:textAllCaps="true"
                    android:textColor="#FFFFFF"
                    android:textSize="14sp"
                    custom:fontName="Bold"/>
        </RelativeLayout>

        <ro.gebs.captoom.utils.fonts.CustomFontTextView
                android:id="@+id/location_txt"
                android:layout_
                android:layout_
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:gravity="center_vertical"
                android:text="@string/location_map"
                android:textAllCaps="true"
                android:textColor="@color/done_color"
                android:textSize="16sp"
                custom:fontName="SemiBold"/>
    </RelativeLayout>

    <fragment
            android:layout_weight="6"
            android:id="@+id/map"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_
            android:layout_
            class="com.google.android.gms.maps.SupportMapFragment"/>


    <RelativeLayout
            android:id="@+id/save_location_layout"
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:background="@color/greenish">

        <LinearLayout
                android:id="@+id/delete_btn"
                android:layout_
                android:layout_
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="25dp"
                android:layout_marginRight="25dp"
                android:background="@drawable/done_btn_selector"
                android:orientation="horizontal"
                android:paddingBottom="7dip"
                android:paddingTop="7dip"
                android:weightSum="7">

            <ro.gebs.captoom.utils.fonts.CustomFontTextView
                    android:id="@+id/delete_txt"
                    android:layout_
                    android:layout_
                    android:layout_weight="6"
                    android:background="@null"
                    android:paddingLeft="10dp"
                    android:text="@string/save_location"
                    android:textAllCaps="true"
                    android:textColor="@color/white"
                    android:textSize="16sp"
                    custom:fontName="Bold"/>

            <ImageView
                    android:layout_
                    android:layout_
                    android:layout_gravity="center"
                    android:layout_weight="1"

                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:src="@drawable/input_map_white"/>
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>

【讨论】:

我敢打赌你的代码会完美运行,但我只需要包含OnDestroyView 方法。无论如何,谢谢。

以上是关于片段中的 Android 谷歌地图的主要内容,如果未能解决你的问题,请参考以下文章

xml 中的 Android 谷歌地图片段。我得到“意外的命名空间前缀”

谷歌地图在片段android

带有标记的Android谷歌地图片段

谷歌地图Android片段崩溃

在 Android 片段中使用谷歌地图

谷歌地图片段无法膨胀