如何使我的android应用在地图和中心地图上显示标记?

时间:2019-05-24 18:51:25

标签: java google-maps android-studio-3.4

我正在尝试创建一个带有两个标签的Android应用,其中一个包含Google地图。我创建了选项卡活动项目,手动添加了片段,地图显示在该片段中,但是到目前为止我什么也没做。

我创建了一个新项目,选择了选项卡活动。添加了片段。编辑gradle,添加权限,在清单文件中添加google API密钥。我首先尝试将MapView放在片段的xml中的FrameLayout中,然后尝试仅将<fragment ... />而不是整个FrameLayout放入。 最终没有任何效果,关于stackoverflow的确有很多类似的问题,但是我都没有找到解决我的情况的答案。你能告诉我我在做什么错吗?

清单文件(部分代码):

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

    <!-- google api key is here, that's for sure, I double checked that. -->

PlaceholderFragment.java(部分代码):

public class PlaceholderFragment extends Fragment {

    /* some more code here */

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = null;

        switch (getArguments().getInt(ARG_SECTION_NUMBER))
        {
            case 1:
                rootView = inflater.inflate(R.layout.fragment_freacking_support_map, container, false);
                break;
            case 2:
                rootView = inflater.inflate(R.layout.fragment_main, container, false);
                break;
            default:
                rootView = inflater.inflate(R.layout.fragment_main, container, false);
                break;
        }

        return rootView;
    }
}

FreackingSupportMapFragment.java:

public class FreackingSupportMapFragment extends SupportMapFragment implements OnMapReadyCallback {

    private MapView mapView;
    private GoogleMap mMap;

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
// previous attempt in commented code below
//        View view = inflater.inflate(R.layout.fragment_freacking_support_map, container, false);
////        mapView = (MapView) view.findViewById(R.id.freacking_mapView);
//        getMapAsync(this);
//        return view;

        View rootView = inflater.inflate(R.layout.fragment_freacking_support_map, container, false);
        MapsInitializer.initialize(getContext());

        mapView = (MapView) rootView.findViewById(R.id.freacking_mapView);
        mapView.onCreate(savedInstanceState);
        mapView.getMapAsync(this);
        // initialize other UI elements.
        return rootView;
    }

    @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_freacking_support_map.xml:

<!-- previous attempt in commented code below -->
<?xml version="1.0" encoding="utf-8"?>
<!--<fragment xmlns:android="http://schemas.android.com/apk/res/android"-->
    <!--xmlns:map="http://schemas.android.com/apk/res-auto"-->
    <!--xmlns:tools="http://schemas.android.com/tools"-->
    <!--android:id="@+id/freacking_map"-->
    <!--android:name="com.google.android.gms.maps.SupportMapFragment"-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="match_parent"-->
    <!--tools:context=".FreackingSupportMapFragment" />-->

<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"
    tools:context=".FreackingSupportMapFragment">

    <!--<TextView-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:text="I'm freacking text view"/>-->

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

</FrameLayout>

等级(部分代码):

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'android.arch.lifecycle:extensions:1.1.1'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.google.android.gms:play-services-location:16.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.android.gms:play-services-maps:16.1.0'
}

我希望该程序将标记放置在Sydney(默认选项)并在此处放置centem地图,以便我可以进行所需的其他更改,但这只是打开地图而已。当我以调试模式启动该应用程序时,我希望它可以同时在FreackingSupportMapFragment.onCreateView方法和FreackingSupportMapFragment.onMapReady中的断点处停止,但是都没有达到这些断点,这确实让我感到困惑。 我该如何解决?

P.S。抱歉,如果文本太多,我不确定问题出在哪里,似乎所有这些都必须证明我到目前为止所做的一切。并且不要对名称感到困惑。能够做任何事情,如果我把事情弄得糟透了,就不要后悔。

0 个答案:

没有答案