Reputation: 97
I am trying to add the MapView in the Fragment, my app was running well but unable to load the map details or geolocations. Please find the details in the mentioned screenshot. Google Codes work but not loading the maps in MapView. I have also mentioned the code below which I am trying to perform but getting the runtime issues.
NearbyFragment.java
public class NearbyFragment extends Fragment implements OnMapReadyCallback {
private MapView mMapView;
private static final String MAPVIEW_BUNDLE_KEY = "MapViewBundleKey";
View rootNearby;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootNearby = inflater.inflate(R.layout.fragment_nearby, container, false);
// Inflate the layout for this fragment
initGoogleMaps(savedInstanceState);
return rootNearby;
}
private void initGoogleMaps(Bundle savedInstanceState) {
Bundle mapViewBundle = null;
if (savedInstanceState != null) {
mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY);
}
mMapView = (MapView) rootNearby.findViewById(R.id.mapView_NearbyMap);
mMapView.onCreate(mapViewBundle);
mMapView.getMapAsync(this);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Bundle mapViewBundle = outState.getBundle(MAPVIEW_BUNDLE_KEY);
if (mapViewBundle == null) {
mapViewBundle = new Bundle();
outState.putBundle(MAPVIEW_BUNDLE_KEY, mapViewBundle);
}
mMapView.onSaveInstanceState(mapViewBundle);
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onStart() {
super.onStart();
mMapView.onStart();
}
@Override
public void onStop() {
super.onStop();
mMapView.onStop();
}
@Override
public void onMapReady(GoogleMap map) {
map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
map.setMyLocationEnabled(true);
}
@Override
public void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
public void onDestroy() {
mMapView.onDestroy();
super.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}}
Upvotes: 1
Views: 110
Reputation: 7439
Edit: Added a more comprehensive answer with all the possible issues to render/display map correctly with links and steps, there is a nice tutorial here too.. below are additional steps to troubleshoot.
Follow these steps:
google_maps_api.xml
In GoogleMaps for Android, you need both keys - debug and release.
The "debug
" key is used in other important places, its also used when you
develop the app in Eclipse. The same debug key is used for
development, testing, debugging.
The debug key for signing your application can be found in the userhome/.android/debug.keystore file.
To create the SHA-1 for your debug keystore you use the keytool command from your JDK installation.
keytool -list -v -alias androiddebugkey \
-keystore <path_to_debug_keystore>debug.keystore \
-storepass android -keypass android
Prior to publishing app to Market, change the
android:debuggable="false"
in the AndroidManifest.xml
and use
your Signed API key
here.
Check to use the correct Fingerprint like #3 above keytool -list -keystore ~\.android\debug.keystore ...
Then go to your user dir. on windows and clean this old file out to C:\Users\dev2.Tykt.org\.android
and deleted debug.keystore
and default.keysetTykt
Lastly, after this, goto Eclipse run a clean build
on your project
and its done!
Upvotes: 1