【问题标题】:Can't display the map in FragmentActivity无法在 FragmentActivity 中显示地图
【发布时间】:2015-07-23 20:50:42
【问题描述】:

大家好,如果我做错了,请见谅

所以我想显示一张地图,我的代码可以完美地单独运行,但是当我在我的应用程序片段中复制/粘贴它时,地图没有出现!

这是我的活动代码:

package com.example.lokman.tryouts;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import java.util.List;

public class FragmentTab3 extends FragmentActivity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener
{
    private static final int GPS_ERRORDIALOG_REQUEST = 9001;
    GoogleMap mMap;
    @SuppressWarnings("unused")
    private static final double


    GoogleApiClient googleApiClient;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Parse.enableLocalDatastore(this);
        Parse.initialize(this, "gj36VxUgttoM2BxT0YF65ABEIXxKJn8a6Synb3Bj", "UoVZa1wOUYFGvMwDPyyrDbj0pGEKYGvdz2dmrmiB");

        if (servicesOk()) {
            setContentView(R.layout.fragmenttab3);
            ParseQuery<ParseObject> query = ParseQuery.getQuery("Lieu");
            query.findInBackground(new FindCallback<ParseObject>() {
                @Override
                public void done(List<ParseObject> arg0, com.parse.ParseException e) {
                    if (e == null) {
                        for (ParseObject item : arg0) {
                            String title = item.getString("name");
                            Double lat = (Double) item.getNumber("latitude");
                            Double lng = (Double) item.getNumber("longitude");
                            MarkerOptions marker = new MarkerOptions()
                                    .position(new LatLng(lat, lng))
                                    .title(""+title);
                            mMap.addMarker(marker);

                        }
                    }

                }
            });

            if(initMap()){
                mMap.setMyLocationEnabled(true);

                googleApiClient = new GoogleApiClient.Builder(this)
                        .addConnectionCallbacks(this)
                        .addOnConnectionFailedListener(this)
                        .addApi(LocationServices.API)
                        .build();
                googleApiClient.connect();

            }
            else{
                Toast.makeText(this, "Map Is Not Avaible !", Toast.LENGTH_SHORT).show();

            }


        } else {
            setContentView(R.layout.activity_main);

        }

    }

    public boolean servicesOk(){
        int isAvaible = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (isAvaible == ConnectionResult.SUCCESS) {
            return true;
        }
        else if (GooglePlayServicesUtil.isUserRecoverableError(isAvaible)) {

            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvaible, this, GPS_ERRORDIALOG_REQUEST);
            dialog.show();
        }
        else {
            Toast.makeText(this,"Can't Connect to google play services !",Toast.LENGTH_SHORT).show();
        }
        return false;
    }

    private boolean initMap(){
        if (mMap == null){
            SupportMapFragment mapFragment =
                    (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            mMap = mapFragment.getMap();
        }
        return(mMap != null);


    }





    @Override
    public void onConnected(Bundle bundle) {
        Toast.makeText(this,"Connected TO Location Service !",Toast.LENGTH_SHORT).show();


    }

    @Override
    public void onConnectionSuspended(int i) {

    }


    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }



}

【问题讨论】:

    标签: android google-maps android-fragments dictionary fragment


    【解决方案1】:

    有类似的问题。你可以试试这个。将 getSupportFragmentManager 更改为 GetChildFragmentManager

    private boolean initMap(){
        if (mMap == null){
            SupportMapFragment mapFragment =
                    (SupportMapFragment) getActivity().getChildFragmentManager().findFragmentById(R.id.map);
            mMap = mapFragment.getMap();
        }
        return(mMap != null);
    
    
    }
    

    【讨论】:

    • 无法解析“getChildFragmentManager”?我还应该改变什么?
    • 试试 SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getChildFragmentManager().findFragmentById(R.id.map);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2013-05-11
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多