在选项卡式 android 应用程序中获取 google 地图
getting google maps in a tabbed android app
我花了一段时间试图让它工作,但在每个教程中我总是卡在某个地方。简而言之,我正在尝试制作一个选项卡式应用程序,其中一个选项卡是 google 映射。
我已经修正了所有常见的错误:
我已经通过SDK下载了所有相关的东西。
我有一个 API 键。
我添加了 compile com.google.android.gms:play-services:7.5.0 to my dependencies.
我正在尝试跟随this code,但它对我来说并不完全有效,所以我稍微改变了它,但我仍然运行撞墙了。
我当前的错误是java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
。我认为这会在 getMapAsync
.
中初始化
这是我的 java 代码:
public class MapFragment extends Fragment{
MapView mMapView;
private GoogleMap googleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.fragment_location_info, container,
false);
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getApplicationContext());
// latitude and longitude
double latitude = 17.385044;
double longitude = 78.486671;
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
}
});
//MapsInitializer.initialize(getApplicationContext());
googleMap = mMapView.getMap();
// latitude and longitude
double latitude = 17.385044;
double longitude = 78.486671;
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(17.385044, 78.486671)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
// Perform any camera updates here
return v;
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
}
您的代码似乎正在进行转换。您正在调用 getMapAsync()
和 getMap()
。
只需使用 getMapAsync()
,在收到 onMapReady()
回调之前不要对地图进行任何操作。
此外,您不需要 MapsInitializer。来自文档:
Use this class to initialize the Google Maps Android API if features
need to be used before obtaining a map. It must be called because some
classes such as BitmapDescriptorFactory and CameraUpdateFactory need
to be initialized.
If you are using MapFragment or MapView and have already obtained a
(non-null) GoogleMap by calling getMap() on either of these classes,
then you do not need to worry about this class. See the sample
application for some examples.
这里是你的代码修正了一点:
public class MapFragment extends Fragment implements OnMapReadyCallback {
MapView mMapView;
private GoogleMap googleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate and return the layout
View v = inflater.inflate(R.layout.fragment_location_info, container,
false);
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
//mMapView.onResume(); // should not be needed
mMapView.getMapAsync(this);
//MapsInitializer.initialize(getApplicationContext());
// Perform any camera updates here
return v;
}
@Override
public void onMapReady(GoogleMap gMap) {
googleMap = gMap;
mMapView.onResume(); //call this here if you really need to
//MapsInitializer.initialize(getApplicationContext());
// latitude and longitude
double latitude = 17.385044;
double longitude = 78.486671;
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(17.385044, 78.486671)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
//.........................
我花了一段时间试图让它工作,但在每个教程中我总是卡在某个地方。简而言之,我正在尝试制作一个选项卡式应用程序,其中一个选项卡是 google 映射。
我已经修正了所有常见的错误:
我已经通过SDK下载了所有相关的东西。
我有一个 API 键。
我添加了 compile com.google.android.gms:play-services:7.5.0 to my dependencies.
我正在尝试跟随this code,但它对我来说并不完全有效,所以我稍微改变了它,但我仍然运行撞墙了。
我当前的错误是java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
。我认为这会在 getMapAsync
.
这是我的 java 代码:
public class MapFragment extends Fragment{
MapView mMapView;
private GoogleMap googleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.fragment_location_info, container,
false);
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getApplicationContext());
// latitude and longitude
double latitude = 17.385044;
double longitude = 78.486671;
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
}
});
//MapsInitializer.initialize(getApplicationContext());
googleMap = mMapView.getMap();
// latitude and longitude
double latitude = 17.385044;
double longitude = 78.486671;
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(17.385044, 78.486671)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
// Perform any camera updates here
return v;
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
}
您的代码似乎正在进行转换。您正在调用 getMapAsync()
和 getMap()
。
只需使用 getMapAsync()
,在收到 onMapReady()
回调之前不要对地图进行任何操作。
此外,您不需要 MapsInitializer。来自文档:
Use this class to initialize the Google Maps Android API if features need to be used before obtaining a map. It must be called because some classes such as BitmapDescriptorFactory and CameraUpdateFactory need to be initialized.
If you are using MapFragment or MapView and have already obtained a (non-null) GoogleMap by calling getMap() on either of these classes, then you do not need to worry about this class. See the sample application for some examples.
这里是你的代码修正了一点:
public class MapFragment extends Fragment implements OnMapReadyCallback {
MapView mMapView;
private GoogleMap googleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate and return the layout
View v = inflater.inflate(R.layout.fragment_location_info, container,
false);
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
//mMapView.onResume(); // should not be needed
mMapView.getMapAsync(this);
//MapsInitializer.initialize(getApplicationContext());
// Perform any camera updates here
return v;
}
@Override
public void onMapReady(GoogleMap gMap) {
googleMap = gMap;
mMapView.onResume(); //call this here if you really need to
//MapsInitializer.initialize(getApplicationContext());
// latitude and longitude
double latitude = 17.385044;
double longitude = 78.486671;
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(17.385044, 78.486671)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
//.........................