1

我知道有关于在stackoverflow片段内的地图很多问题,但他们都过时了,并没有为我工作..所以,我试图实施谷歌地图里面片段,显示在地图,但我不能使用setMyLocationEnabler(真),或添加标记,或任何..这里是我的代码:Android谷歌地图里面的片段

public class MapFragment extends Fragment { 

private SupportMapFragment mSupportMapFragment; 

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


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    mSupportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.location_map); 
    if (mSupportMapFragment == null) { 
     FragmentManager fragmentManager = getChildFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     mSupportMapFragment = SupportMapFragment.newInstance(); 
     fragmentTransaction.replace(R.id.location_map, mSupportMapFragment).commit(); 
    } 
    if (mSupportMapFragment != null) { 
     mSupportMapFragment.getMapAsync(new OnMapReadyCallback() { 
      @Override 
      public void onMapReady(GoogleMap googleMap) { 
       if (googleMap != null) { 
        googleMap.getUiSettings().setAllGesturesEnabled(true); 
        if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
         return; 
        } 
        googleMap.setMyLocationEnabled(true); 
        LatLng sydney = new LatLng(-34, 151); 
        googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
       } 
      } 
     }); 
    } 

    // Inflate the layout for this fragment 
    return inflater.inflate(R.layout.fragment_map, container, false); 
} 

} 

我的API密钥在清单文件中正确添加:

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

<application 

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

</application> 

这里是片段布局:

<RelativeLayout 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="mk.com.urban.urbanrider.MapFragment"> 

<!-- TODO: Update blank fragment layout --> 
<FrameLayout 
    android:id="@+id/location_map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="com.google.android.gms.maps.SupportMapFragment" /> 

</RelativeLayout> 

我有谷歌发挥服务依存关系由的build.gradle:

dependencies { 
compile fileTree(include: ['*.jar'], dir: 'libs') 
testCompile 'junit:junit:4.12' 
compile project(':volley') 
compile 'com.android.support:appcompat-v7:23.4.0' 
compile 'com.android.support:design:23.4.0' 
compile 'com.android.support:support-v4:23.4.0' 
compile 'com.google.android.gms:play-services:8.4.0' 
} 

我真的不知道下一步该怎么做,我用尽了一切......

+0

你应该仔细检查你的搜索,今天我正在寻找片段上的G地图... 2小时后我得到了它[链接](http://stackoverflow.com/a/35460657/1332549)。如果它帮助你,告诉我,我会添加一个链接到答案:) – MiguelHincapieC

回答

0

我已经试过了,我发现在自定义片段中使用map没有问题。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     class="com.google.android.gms.maps.SupportMapFragment" /> 

</LinearLayout> 

这就是你需要:

活动类定制的片断:

public class DemoActivity extends AppCompatActivity { 
    public static class WrapFragment extends Fragment { 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
     } 

     @Nullable 
     @Override 
     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
      View view = inflater.inflate(R.layout.wrap_demo_fragment, container, false); 
      return view; 
     } 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.save_state_demo); 
     if (savedInstanceState == null) { 
      getSupportFragmentManager().beginTransaction() 
        .add(R.id.map_wrap, new WrapFragment(), "qwerty_tag") 
        .commit(); 
     } 
    } 
} 

活动布局save_state_demo.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="1337" /> 

    <FrameLayout 
     android:id="@+id/map_wrap" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</LinearLayout> 

您的自定义布局片段wrap_demo_fragment.xml。