I'm new with StackOverflow and Maps API. I'm developing an app where a Drawer Layout is being used. I want to use one of the Drawer fragments to show Maps and work with Markers.
private SupportMapFragment mapFragment;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mapFragment = (SupportMapFragment)
getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_maps, container, false);
mapFragment.getMapAsync(this);
return view;
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
this is the fragment_maps.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"
tools:context="com.labcontrol.lca.view.MapsFragment">
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
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.labcontrol.lca.controller.MainActivity" />
</FrameLayout>
In the official guide they made an example using an Activity, as you can see I tried to do it inside a fragment. is it possible?
The result is this: maps is working properly, but I can't see the Marker I created, and I spent too many hours trying to figure out why.
The only thing make me suspicious is this error when playing with Maps on the phone and changing the camera:
W/Google Maps Android API: GLHudOverlay deprecated; draw(): no-op
Found out it's a common issue these days, a very new bug. Somebody knows how to get it up and running? Is there something wrong with the code?
thanks a lot
Related
I am implementing Google maps in my android app. It works well but sometimes the app crashes and gives me this error: (Caused by: android.view.InflateException: Binary XML file line #2 in Error inflating class fragment).
I tried all solutions suggested but most of them are old and I still has the same issue.
Can you please help me with this issue while it still cause app crash.
this is the code used in Google Maps:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityTruckLocationBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
lat=lang="0";
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map2);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng myplace = new LatLng(29.2722744, 47.8585885);
//mMap.addMarker(new MarkerOptions().position(myplace).title("Marker in myplace"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myplace,11));
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(#NonNull LatLng latLng) {
lat=String.valueOf(latLng.latitude);
lang=String.valueOf(latLng.longitude);
redirect();
}
});
}
public void redirect(){
Intent intent=new Intent(TruckLocation.this,Truck_Profile.class);
intent.putExtra("intenttype","location");
intent.putExtra("lat",lat);
intent.putExtra("lang",lang);
startActivity(intent);
finish();
}
Here is the XML code:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map2"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TruckLocation" />
You have an issue in TruckLocationActivity.xml , you can share this xml with us to help you .
Iam practicing on Google Maps for android development. I have followed the steps as in https://developers.google.com/maps/documentation/android-api/map-with-marker
While when playing the app I got the screen like this below, no marker and the map cannot show any thing
My code as following:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in Sydney, Australia,
// and move the map's camera to the same location.
LatLng sydney = new LatLng(-33.852, 151.211);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
******** . XML Layout ************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.ae.map_04.MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
This is most likely an API Key issue. You can find some guidance here: Android Studio - Google map still blank on real Android device on release apk
If that does not work, try a new API Key.
I'm making an application using Android Studio and even though I've got the map and it's being displayed, I can't add markers or do anything else to it using the GoogleMap object obtained from OnMapReady.
Here's the code from my Main Activity:
public class MainActivity extends AppCompatActivity implements OnFragmentInteractionListener, OnMapReadyCallback {
MapFragment mapFragment;
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchFragments(GlobalConsts.FRAGMENT.MAP);
}
public void switchFragments(GlobalConsts.FRAGMENT fragment){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
switch (fragment) {
case MAP:
if (mapFragment == null) {
mapFragment = new MapFragment();
}
mapFragment.getMapAsync(this);
transaction.replace(R.id.fragmentContainer, mapFragment);
transaction.addToBackStack(null);
break;
}
transaction.commit();
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions()
.position(sydney)
.title("Sydney")
.snippet("Population: 4,627,300")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
Here's the xml layout for my Main Activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
tools:context="jarett.familymap.MainActivity">
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
My MapFragment class:
public class MapFragment extends SupportMapFragment {
public MapFragment() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_map, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
and the xml layout for my MapFragment in fragment_map.xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mapFragment"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
tools:context="jarett.familymap.MapFragment">
</fragment>
Like I said, everything seems to be working fine as far as displaying the map fragment but for the life of me I can't figure out why the code for zooming and adding markers isn't doing anything. I appreciate the help.
I can't figure out why the code for zooming and adding markers isn't doing anything
Well, you have two maps. Your MapFragment extends SupportMapFragment, so that will give you one map. Then, your MapFragment inflates a layout that, in turn, creates a SupportMapFragment. This gives you two maps.
Either:
Delete MapFragment entirely, along with its associated layout file, and just have your activity create a SupportMapFragment, or
Delete your onCreateView() method from MapFragment, and move your map-management code into MapFragment, triggered by a getMapAsync() call in onViewCreated() (as I worry that you are calling it too soon in your existing code)
In my project im trying to implementthis solution for managing a map in a dynamic added fragment.
public class MapFragment extends Fragment {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v =inflater.inflate(R.layout.fragment_map, container, false);
map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_arrow)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(KIEL, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
return v;
}
}
My view:
<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"
android:background="#color/ColorBackground"
tools:context="com.example.sven.questy.GameFragments.MapFragment">
<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
WHen i run the app i get a nullpointer exception on:
map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
The id of the map = map ( android:id="#+id/map").
Why the nullpointer?
Since you are using nested fragments, you should use getChildFragmentManager()
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
And I recommend you to use getMapAsync method, because getMap is deprecated
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
setupMap(googleMap);
}
});
You need to use findViewById on the view you get, not using the fragment manager. The map is attached to the view
View v =inflater.inflate(R.layout.fragment_map, container, false);
map = v.findViewById(R.id.map))
.getMap();
i am working with swipeable tabs and want to display a mapsfragment inside the second tab, but everytime i try to get access to the map, there appears a nullpointerexception. Following the code of my Fragment/Tab.
public class tab2_advanced extends Fragment implements OnMapReadyCallback{
private GoogleMap gmap;
private static View view;
MapView ourMap;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab2_erweitertezusage, container, false);
Log.i("TAB2", "Start.");
//without the following lines regarding null or not null, the fragment loads the map, but i want to set markers and select a position on the map.
if (gmap==null) {
Log.i("Tab2", "Map null");
gmap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
gmap.setMyLocationEnabled(true);
}
if (gmap != null) {
Log.i("Tab2", "Map not null");
gmap.setMyLocationEnabled(true);
}
return v;
}
#Override
public void onMapReady(GoogleMap googleMap) {
Log.i("Aufruf onMapReady", "Darstellung"+gmap);
//Später Übergabe der Pos aus DB
LatLng sidney= new LatLng(49.7685, 9.9382);
gmap.setMyLocationEnabled(true);
gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(wuerzburg, 13));
gmap.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(wuerzburg));
}
My XML-File for this is the following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="50dp">
<LinearLayout
android:orientation="vertical"
android:id="#+id/layoutMap"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="200dp"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
Without the described part in comments in the code of the fragment, the map loads the content. There are still some parts missing in the case, if the map is not null, but i tried it and it lands everytime in null with a following NullpointerException.
Hoping for hints :)
Greetings
Okay, i solved the issue by:
if (gmap==null) {
Log.i("Tab2", "Map null");
gmap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
gmap.setMyLocationEnabled(true);
}
if (gmap != null) {
Log.i("Tab2", "Map not null");
gmap.setMyLocationEnabled(true);
gmap.getUiSettings().setZoomControlsEnabled(true);
actLocation.getLocation(getActivity(), locationResult);
LatLng actPos = new LatLng(organizerGPS.latitude, organizerGPS.longitude);
Log.i("tab2", "actPos: " + actPos);
gmap.setMyLocationEnabled(true);
gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(actPos, 13));
...
The hint with the getChildFragmentManager was good, but the reason for the NullpointerException was, that the view was null and that results to the error. Now the initialisation of the map is right behind setting the view and in addition i added a test.