我想實現谷歌地圖到我的項目,我只是添加了一個新的片段和下面的代碼。目前,我可以在我的項目中顯示地圖,但添加標記不起作用,並且mapFragment.getMapAsync(this)
會顯示以下錯誤消息。無法解析方法'getMapAsync(testCodes.project2.MapFragment)
。谷歌地圖getMapAsync不會工作在片段
public class MapFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
// private MapView mapView;
private Boolean mapReady = false;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
public MapFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
MapFragment mapFragment = (MapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);//remember getMap() is deprecated!
// mapFragment.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(-33.852, 151.211);
MarkerOptions markerOptions = new MarkerOptions().position(sydney).title("Random position");
googleMap.addMarker(markerOptions);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
checkPermission("Permission", 1, 1);
mMap.setMyLocationEnabled(true);
}
}
該片段的xml。
<fragment
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"></fragment>
您需要添加googleservice.json。希望你已經在清單中添加了所需的東西以及 –
我已經完成了所需的工作,例如獲取google sdk,api key等。但不清楚googleservice.json的意思。 。 <使用權限android:name =「android.permission.ACCESS_COARSE_LOCATION」/> <使用權限android:name =「android.permission.ACCESS_FINE_LOCATION 「/> android:name =」com.google.android.geo.API_KEY「android:value =」myKey「/> –
Amar
試試這個鏈接。它會指導你如何做到這一點:http://stackoverflow.com/questions/34367870/where-do-i-get-a-google-services-json –