【发布时间】:2016-05-15 11:48:40
【问题描述】:
我知道在 stackoverflow 上的片段内有很多关于地图的问题,但所有这些问题都已过时并且不适合我。所以,我正在尝试在片段内实现谷歌地图,地图是显示,但我不能使用 setMyLocationEnabler(true),或添加标记,或任何东西。这是我的代码:
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 中有 google play 服务依赖项:
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'
}
我真的不知道下一步该怎么做,我已经尝试了所有...
【问题讨论】:
-
你应该仔细检查你的搜索,今天我想在片段上使用 G 地图...... 2 小时后我得到了它link。如果它对你有帮助,告诉我,我会添加一个链接到答案:)
标签: java android google-maps android-fragments google-maps-api-2