Can't initialize GoogleMap, returns null - android

I'm trying to initialize a GoogleMap instance, but SupportMapFragment returns null.
Here is my MainActivity Class:
public class MainActivity extends FragmentActivity
{
GoogleMap gMap;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
initMap(); //here is the NullPointerExeption
setContentView(R.layout.activity_map);
}
public boolean initMap()
{
if (gMap == null) {
SupportMapFragment mapFrag =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragMap);
gMap = mapFrag.getMap();
}
return (gMap != null);
}
}
And this is my activity_map.xml:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/fragMap"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
what is the problem? if I comment out the initMap() function, it works fine and displays the map.

I think you should change the order like
setContentView(R.layout.activity_map);
initMap();
First you should setContentView(R.layout.activity_map); and then reference Map from SupportMapFragment.

Related

Not able to show map marker in android

I want to add map marker in my map. I have a fragment in which I want to show map marker. I have added 'com.google.android.gms:play-services-maps:10.0.1' in my app's gradle. But I an having error i.e.
android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class fragment
// xml file code
<?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="#dimen/dp176"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
// code for map
public class KantorenOfficeDetailsFragment extends Fragment implements View.OnClickListener, OnMapReadyCallback {
GoogleMap googleMap;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_kantoren_office_details, container, false);
setUpMapIfNeeded();
return v;
}
private void setUpMapIfNeeded() {
if (googleMap == null) {
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
if (googleMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
googleMap.addMarker(new MarkerOptions().position(new LatLng(22.7253, 75.8655)).title("Indore")); // here is marker Adding code
}
}
you are not initialize you googleMap object.
MapFragment Initialize must like this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
mapFragment= (MyMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
else
mapFragment= (MyMapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
and then import the callback method
#Override
public void onMapReady(GoogleMap map) {
googleMap = map;
}
call this setUpMap in inside callback method
if (googleMap != null) {
setUpMap();
}
Your OnMapReady Must be like
#Override
public void onMapReady(GoogleMap map) {
googleMap = map;
if (googleMap != null) {
setUpMap();
}
}
Hope it will help you

Map Return NULL

When Application run always return NULL Exception
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
if (map == null) {
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googlemap) {
// TODO Auto-generated method stub
map=googlemap;
// startMap();
}
});
}
Xml Code is here where i used Map *
<fragment
android:id="#+id/map"
class="com.shahi.driver.locationfiles.TouchableMapFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Your class will be:
public class YourActivity extends Activity implements OnMapReadyCallback {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
try {
// Loading map
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
protected void onResume() {
super.onResume();
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(final GoogleMap googleMap) {
final LatLng current_position = new LatLng(your_latitude, your_longitude);
MarkerOptions marker = new MarkerOptions().position(current_position).title("Your Address").snippet("Anything");
googleMap.addMarker(marker);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(current_position, 12));
googleMap.getUiSettings().setMapToolbarEnabled(true);
googleMap.setMyLocationEnabled(true);
}
}
and your_layout.xml will be:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
This should work:
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng location = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(location ).title("Marker in location "));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
Then your xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.polaris.project_x.MapsActivity" />
I can see you have used custom Map Fragment on your XML code. By searching through its name I chanced upon its code on GitHub.
The issue on your code is that you are trying to get MapFragment from your layout but your Custom Map code is extending SupportMapFragment.
So to make your code work change your MapFragment to SupportMapFragment.
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map);
Also if your BaseActivity is extending AppCompatActivity then use getSupportFragmentManager() instead of getFragmentManager().

Marker is not working(not exact location is displaying), only world Map is visible in Frgament

CustomMapFragment
public class CustomMapFragment extends SupportMapFragment {
public static final LatLng NurissLife = new LatLng(28.602012, 77.098750);
private SupportMapFragment supportMapFragment;
#Override
public void onActivityCreated(Bundle bundle) {
// FragmentManager fm=getChildFragmentManager();
supportMapFragment=SupportMapFragment.newInstance();
//supportMapFragment=(SupportMapFragment)getChildFragmentManager
//().findFragmentById(R.id.map_container);
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.addMarker(new MarkerOptions().position
(NurissLife).title("Nuriss LifeCare"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom
(NurissLife, 15));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000,
null);
}
});
super.onActivityCreated(bundle);
}
}
Fragment xml
<FrameLayout 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"
android:id="#+id/map_content"
tools:context="com.demo.stuartabhi.nurisslife.Fragment.ContactFragment">
<!-- TODO: Update blank fragment layout -->
<FrameLayout
android:layout_width="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_height="match_parent"
android:id="#+id/map_container">
</FrameLayout>
</FrameLayout>'
Marker is not working(exact location is not displaying), only world Map
is visible in Fragement
OnMapReady is not working either showing
no error in log, location code being described in OnMapReady method.
Please help regarding this, how to access the described location LatLng NurissLife inside SupportMapFragment.
Thanks in advance.
If you extend SupportMapFragment, there is no need to inflate any xml layout. Just call this.getMapAsync() in order to get a reference to the GoogleMap:
public class CustomMapFragment extends SupportMapFragment implements
OnMapReadyCallback {
private GoogleMap mMap;
public CustomMapFragment() {
}
#Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
getMapAsync(this);
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
Log.d("MyMap", "onMapReady");
mMap = googleMap;
mMap.addMarker(new MarkerOptions().position(NurissLife).title("Nuriss LifeCare"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(NurissLife, 15));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
}
}
Try this:
Create a method getCustomMapFragment() in your Activity class like this:
private SupportMapFragment getCustomMapFragment(){
SupportMapFragment supportMapFragment;
supportMapFragment= SupportMapFragment.newInstance();
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.addMarker(new MarkerOptions().position
(NurissLife).title("Nuriss LifeCare"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom
(NurissLife, 15));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000,
null);
}
});
return supportMapFragment;
}
And when you perform your FragmentTransaction, try this:
...
transaction=getChildFragmentManager().beginTransaction();
transaction.add(R.id.map_container,getCustomMapFragment()).commit();
...
Also, with this implementation, the class="com.google.android.gms.maps.SupportMapFragment" line in your XML might be unnecessary.

GoogleMaps.oncameraChangedListener is never called

I have an Activity (which inflates a map fragment) which implements the GoogleMap.onCameraChangedListener, and I have overriden the onCameraChange method.
The problem is that whenever I move across the map, i.e. the camera position changes, the onCameraChange method is never called.
What could be causing this?
Try this :
public class MapActivity extends AppCompatActivity implements
GoogleMap.OnCameraChangeListener{
private GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.googlemap_layout);
setUpMapIfNeeded();
}
#Override
public void onCameraChange(CameraPosition cameraPosition) {
double latitude = cameraPosition.target.latitude;
double longitude = cameraPosition.target.longitude;
}
private void setUpMapIfNeeded() {
if (googleMap == null) {
// Try to obtain the map from the SupportMapFragment.
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (googleMap != null) {
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(false);
googleMap.getUiSettings().setZoomControlsEnabled(false);
googleMap.setOnCameraChangeListener(this);
}
}
}
}
Layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>

How to set up OnMapLoadedCallback GoogleMaps

I want to know when everything I have being loaded into my googlemap fragment has loaded everything. This is what my code currently looks like
public class MainMapActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMapLoadedCallback {
GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_map);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
//addLatestLocations();
// TestVectorQuery();
}
#Override
public void onMapReady(GoogleMap map) {
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(32.0516, -78.925), 3));
map.setOnMapLoadedCallback(this);
}
#Override
public void onMapLoaded() {
//SetMapMarker(26.1333, 80.1500);
//Whatever I want to do
}
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
class="com.google.android.gms.maps.MapFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
There doesn't seem to be anything wrong with this; however, when I try to do something within onMapLoaded() nothing happens, so it seems that the callback isn't properly set. Any help would be appreciated. Thank you.
You are using a native fragment (com.google.android.gms.maps.MapFragment) and getFragmentManager() in a FragmentActivity. That does not work. Either change the fragment to SupportMapFragment and use getSupportFragmentManager(), or change your activity to inherit from Activity.
try to use support fragment because it works for me .
below is my code run successfully.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_map);
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
#Override
public void onMapLoaded() {
map.addMarker(new MarkerOptions()
.title("your title")
.snippet("your desc")
.position(new LatLng(-22.2323,-2.32323))
);
}
});
}

Categories

Resources