Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何解决在android中集成片段式谷歌地图的问题_Android_Google Maps_Google Maps Android Api 2 - Fatal编程技术网

我如何解决在android中集成片段式谷歌地图的问题

我如何解决在android中集成片段式谷歌地图的问题,android,google-maps,google-maps-android-api-2,Android,Google Maps,Google Maps Android Api 2,我在android中集成谷歌地图片段时遇到问题。我知道如何在活动中做到这一点,但该网站上的片段引用非常陈旧,在2018年无法使用。我没有任何错误。下面是片段文件。我们将非常感谢您的帮助。我已经添加了API密钥和适当的清单文件 package com.example.narmail.truck30mint.Api.Fragments; import android.Manifest; import android.content.Context;

我在android中集成谷歌地图片段时遇到问题。我知道如何在活动中做到这一点,但该网站上的片段引用非常陈旧,在2018年无法使用。我没有任何错误。下面是片段文件。我们将非常感谢您的帮助。我已经添加了API密钥和适当的清单文件

package com.example.narmail.truck30mint.Api.Fragments;

        import android.Manifest;
        import android.content.Context;
        import android.content.DialogInterface;
        import android.content.pm.PackageManager;
        import android.net.Uri;
        import android.os.Bundle;
        import android.support.annotation.NonNull;
        import android.support.v4.app.ActivityCompat;
        import android.support.v4.app.Fragment;
        import android.support.v4.content.ContextCompat;
        import android.support.v7.app.AlertDialog;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.TextView;

        import com.example.narmail.truck30mint.R;
        import com.google.android.gms.maps.CameraUpdateFactory;
        import com.google.android.gms.maps.GoogleMap;
        import com.google.android.gms.maps.MapView;
        import com.google.android.gms.maps.MapsInitializer;
        import com.google.android.gms.maps.OnMapReadyCallback;
        import com.google.android.gms.maps.model.CameraPosition;
        import com.google.android.gms.maps.model.LatLng;
        import com.google.android.gms.maps.model.MarkerOptions;

        import java.util.ArrayList;

        public class ViewTrucksFragment extends Fragment {

            TextView pageTitle;
            MapView mMapView;
            private GoogleMap googleMap;

            public ViewTrucksFragment() {
                // Required empty public constructor
            }

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                // Inflate the layout for this fragment
                View rootView = inflater.inflate(R.layout.fragment_view_trucks, container, false);
                pageTitle = rootView.findViewById(R.id.view_trucks_title);
                String load_id =  getArguments().getString("load_id");
                String load_from = getArguments().getString("load_from");
                String load_to = getArguments().getString("load_to");
                if (load_id != null && load_from != null && load_to != null) {
                    pageTitle.setText("Matching Trucks for "+load_from+" to "+load_to);
                }

                mMapView= rootView.findViewById(R.id.view_trucks_map);
                mMapView.onCreate(savedInstanceState);

                mMapView.onResume();

                try {
                    MapsInitializer.initialize(getActivity().getApplicationContext());
                } catch (Exception e) {
                    e.printStackTrace();
                }

                mMapView.getMapAsync(new OnMapReadyCallback() {
                    @Override
                    public void onMapReady(GoogleMap mMap) {
                        googleMap = mMap;
                        googleMap.getUiSettings().setCompassEnabled(true);
                        googleMap.getUiSettings().setMyLocationButtonEnabled(true);
                        googleMap.getUiSettings().setRotateGesturesEnabled(true);
                        // For dropping a marker at a point on the Map
                        LatLng sydney = new LatLng(30.374219,76.782055);
                        googleMap.addMarker(new MarkerOptions().position(sydney).
                                title("Title").snippet("TitleName"));

                        // For zooming automatically to the location of the marker
                        CameraPosition cameraPosition =                                                                           new CameraPosition.Builder().target(sydney).zoom(12).build();
                        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition
                                (cameraPosition ));
                    }
                });
                /*----------------*/
                return rootView;
            }
        }
下面是我的布局文件

<?xml version="1.0" encoding="utf-8"?>
        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorWhite"
            tools:context=".Api.Fragments.ViewTrucksFragment">

           <LinearLayout
               android:orientation="vertical"
               android:layout_width="match_parent"
               android:layout_height="match_parent">

            <TextView
                android:id="@+id/view_trucks_title"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textAlignment="center"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="10dp"
                android:padding="10dp"
                android:textColor="@color/colorPrimary"
                android:textSize="15sp"
                android:text="@string/hello_blank_fragment" />

            <com.google.android.gms.maps.MapView
                android:id="@+id/view_trucks_map"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </com.google.android.gms.maps.MapView>

           </LinearLayout>

        </FrameLayout>

请按照以下步骤完成任务。只需要创建3个文件

(1) 在片段布局fragment_map.XML中为地图创建XML布局

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
(3) 创建活动以加载映射片段MapsActivity.java

public class MapsActivity extends FragmentActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.content,new MapFragment());
        fragmentTransaction.commit();
    }

}
对于MAP key,您需要遵循在项目中完成的相同步骤。希望这一步能对你有所帮助

内梯段请在梯段下方使用

implementation 'com.google.android.gms:play-services-maps:15.0.1'
AndroidManifest.xml定义以下内容

<uses-permission android:name="android.permission.INTERNET"/>

内部应用程序标记

<meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="YOUR MAP KEY" />


它给出错误:找不到适合add(int,MapFragment)方法FragmentTransaction的方法。add(Fragment,String)不适用(参数不匹配;int无法转换为FragmentTransaction)方法FragmentTransaction。add(int,Fragment)不适用(参数不匹配;MapFragment无法转换为Fragment)请检查正确的add()功能我认为你使用了错误的功能。你的解决方案在整个页面上显示活动,但我不能这样做,因为我在主活动中使用导航抽屉,并在几个片段中显示所有内容。请根据这一点显示一些解决方案。这只是一个片段示例,你需要根据你的要求修改布局。我已经完成了布局中有mapView错误。TextLayout高度设置为与父级匹配,地图隐藏在其后面
<meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="YOUR MAP KEY" />