please help.
I am trying to display a google map in a scroll tab, but I am getting a blank google map.
Since I am using the scroll tab, I use android.support.v4.app.Fragment in the PagerAdapter class and the fragment class which should display the map.
The following is fragment class to display a google map
package ui;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bdushimi.bjhangout.R;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapTab extends Fragment implements OnMapReadyCallback {
public static View view;
private GoogleMap mGoogleMap;
private MapView mapView;
private FusedLocationProviderClient mFusedLocationProviderClient;
private SupportMapFragment mapFragment;
public MapTab() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_map, container, false);
return view;
}
#Override
public void onViewCreated(View v, #Nullable Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
mapView = (MapView) this.view.findViewById(R.id.map);
if (mapView != null) {
mapView.onCreate(savedInstanceState);
mapView.onResume();
mapView.getMapAsync(this);
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(this.getActivity());
mGoogleMap = googleMap;
googleMap.addMarker(new MarkerOptions().position(new LatLng(40.689247, -74.044502))
.title("You're here"));
}
}
The following is the xml
<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"
tools:context="ui.MapTab">
<com.google.android.gms.maps.MapView
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.google.android.gms.maps.MapView>
</RelativeLayout>
I would like to know where I am getting wrong or possibly another approach I should consider. Thanks
Related
I have a problem integrating google map in fragment in android. I know how to do it in activity but fragment reference on this site are very old and not working in 2018. I don't have any error.below is fragment file. Any help will be highly appreciated. I have added API key and proper manifest file.
package com.example.narmail.truck30mint.Api.Fragments;
import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.narmail.truck30mint.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.util.ArrayList;
public class ViewTrucksFragment extends Fragment {
TextView pageTitle;
MapView mMapView;
private GoogleMap googleMap;
public ViewTrucksFragment() {
// Required empty public constructor
}
#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
View rootView = inflater.inflate(R.layout.fragment_view_trucks, container, false);
pageTitle = rootView.findViewById(R.id.view_trucks_title);
String load_id = getArguments().getString("load_id");
String load_from = getArguments().getString("load_from");
String load_to = getArguments().getString("load_to");
if (load_id != null && load_from != null && load_to != null) {
pageTitle.setText("Matching Trucks for "+load_from+" to "+load_to);
}
mMapView= rootView.findViewById(R.id.view_trucks_map);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
// For dropping a marker at a point on the Map
LatLng sydney = new LatLng(30.374219,76.782055);
googleMap.addMarker(new MarkerOptions().position(sydney).
title("Title").snippet("TitleName"));
// For zooming automatically to the location of the marker
CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition
(cameraPosition ));
}
});
/*----------------*/
return rootView;
}
}
and below is my layout file
<?xml version="1.0" encoding="utf-8"?>
<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:background="#color/colorWhite"
tools:context=".Api.Fragments.ViewTrucksFragment">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/view_trucks_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:textColor="#color/colorPrimary"
android:textSize="15sp"
android:text="#string/hello_blank_fragment" />
<com.google.android.gms.maps.MapView
android:id="#+id/view_trucks_map"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.google.android.gms.maps.MapView>
</LinearLayout>
</FrameLayout>
Please follow below step to finish your task. Just need to create 3 files
(1) Create XML layout for map inside your fragment layout fragment_map.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
(2) Create Fragment to load MAP MapFragment.Java
public class MapFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
public MapFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_map, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
if(getActivity()!=null) {
SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager()
.findFragmentById(R.id.map);
if (mapFragment != null) {
mapFragment.getMapAsync(this);
}
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//Do your stuff here
}
}
(3) Create Activity to load MAP fragment MapsActivity.java
public class MapsActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.content,new MapFragment());
fragmentTransaction.commit();
}
}
For MAP key you need to follow same step you have done in your project. Hope this step will help you.
Inside Gradle please use below gradle
implementation 'com.google.android.gms:play-services-maps:15.0.1'
AndroidManifest.xml define below things.
<uses-permission android:name="android.permission.INTERNET"/>
Inside Application Tag.
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR MAP KEY" />
I've got my new application based on Google Maps activity. I set my onMapLongClickListener to add as many new marker as the user want to. But there is my question: I want to set a ListView on the top of my map in order to allow the user to chose between different marker types. How can I set a ListView that appears when I hold a point on the map and then disappear after the selection of one of its items?
Here is my app code, if it can help you on giving me some solutions!
MapsActivity.java
package com.example.mancu_000.onclickmarkerlistener;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, OnMapLongClickListener {
private GoogleMap map;
#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) {
map = googleMap;
map.setOnMapLongClickListener(this);
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(MapsActivity.this, "OnMarkerListener", Toast.LENGTH_SHORT).show();
return true;
}
});
LatLng home = new LatLng(44.42913, 8.84072);
map.addMarker(new MarkerOptions().position(home).title("Home, bitches!"));
map.moveCamera(CameraUpdateFactory.newLatLng(home));
CameraUpdate center = CameraUpdateFactory.newLatLng(home);
map.moveCamera(center);
CameraUpdate zoom = CameraUpdateFactory.zoomTo(17);
map.animateCamera(zoom);
}
#Override
public void onMapLongClick(LatLng location) {
// Here I'm supposed to implements thee code of the ListView on the top of the map
}
}
activity_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.example.mancu_000.onclickmarkerlistener.MapsActivity" >
<fragment xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
class="com.google.android.gms.maps.SupportMapFragment"/>
</FrameLayout>
Thanks for you support!!
push a dialog to get the list of markers like
#Override
public void onMapLongClick(LatLng location) {
AlertDialog.Builder builderSingle = new AlertDialog.Builder(context);
builderSingle.setIcon(R.drawable.title_icon_resource);
builderSingle.setTitle("Select One Name:-");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>();//create adapter with your markers list
builderSingle.setNegativeButton();//to exit directly if you want
builderSingle.setAdapter(arrayAdapter,new DialogInterface.OnClickListener());//set your adapter and listener
builderSingle.show();
}
hope this will helps you
I have created a Github project that demonstrates the issue.
git clone https://github.com/anujgoyal/buggymap.git
I realize there are existing questions, but very few of them have distilled the program down to this level. In addition I have a hard requirement to use the android.support.v4.app.Fragment class, not FragmentActivity, not MapFragment.
Note that you will have to change android:value for your com.google.android.maps.v2.API_KEY in AndroidManifest.xml
Why is SupportMapFragment NULL?
// MainActivity.java
package com.txt2lrn.buggymap;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public static class PlaceholderFragment extends Fragment implements OnMapReadyCallback {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//SupportMapFragment smf0 =
// (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
SupportMapFragment smf1 =
(SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
SupportMapFragment smf2 =
(SupportMapFragment)
getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
Log.d("maps", "smf1: "+smf1); // always NULL
Log.d("maps", "smf2: "+smf2); // always NULL
//smf.getMapAsync(this);
return rootView;
}
#Override
public void onMapReady(GoogleMap map) {
// code will go here once getMapAsync works
}
}
}
Here is the fragment_main.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment">
<fragment
android:id="#+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
You appear to be trying to use nested fragments. In that case, you get those nested fragments via the FragmentManager returned by getChildFragmentManager() called on your fragment, not getSupportFragmentManager() called on your activity.
I'm new in android programming;
I have that small app with 3 pages (fragments), swiping between them using pageradapter and viewpager;
one of these pages contains checkbox [and other controls] and a map;
my problem is that the program is crashing on start.
Fragment_compass.java
package com.ibdiab.emadaldien;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class Fragment_Compass extends Fragment {
MapView mapView;
GoogleMap map;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.activity_compass, container, false);
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.eamap);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
//map.getUiSettings().setMyLocationButtonEnabled(false);
//map.setMyLocationEnabled(true);
map.addMarker(new MarkerOptions().position(new LatLng(50.167003,19.383262)));
// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
try {
MapsInitializer.initialize(this.getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
map.animateCamera(cameraUpdate);
return v;
}
#Override
public void onResume() {
mapView.onResume();
super.onResume();
}
#Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
activity_compass.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="#+id/autolocate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="view live map" />
<fragment
android:id="#+id/eamap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
MainScr.java
public class MainScr extends FragmentActivity {
private PagerAdapter el_pagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
initialisePaging();
}
private void initialisePaging() {
// TODO Auto-generated method stub
List<Fragment> eafragments = new Vector<Fragment>();
eafragments.add(Fragment.instantiate(this, Fragment_PraysTable.class.getName()));
eafragments.add(Fragment.instantiate(this, Fragment_Main.class.getName()));
eafragments.add(Fragment.instantiate(this, Fragment_Compass.class.getName()));
el_pagerAdapter = new PagerAdapter(this.getSupportFragmentManager() , eafragments);
ViewPager pager = (ViewPager) findViewById(R.id.mainviewpager);
pager.setAdapter(el_pagerAdapter);
pager.setCurrentItem(1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_scr, menu);
return true;
}
}
When I replace the container with SupportMapFragment in runtime, it doesn't fill Parent. It occupies screen only partially.
HomeActivity2.java
package com.example.mapdemo;
import com.meteraid.android.services.LocationService;
import android.location.Location;
import android.os.Bundle;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
public class HomeActivity2 extends BaseActivity2 {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_activity2);
LocationService locationService=LocationService.getInstance(this);
Location location=locationService.getCurrentLocation();
GoogleMapFragment mapFragment=new GoogleMapFragment();
mapFragment.setCurrentLocation(location);
android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container,mapFragment);
fragmentTransaction.commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home_activity2, menu);
return true;
}
}
GoogleMapFragment
package com.example.mapdemo;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import android.location.Location;
public class GoogleMapFragment extends SupportMapFragment {
private static View view;
private Location location;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = super.onCreateView(inflater, container, savedInstanceState);
Toast.makeText(getActivity(), "kkd",Toast.LENGTH_LONG).show();
initMap(location);
return view;
}
public void setCurrentLocation(Location location) {
this.location = location;
}
public void initMap(Location location) {
GoogleMap map = getMap();
Log.e("Locationnnnnnnnnn", location+"");
CameraPosition currentLocation = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location
.getLongitude())).zoom(4f).bearing(0).tilt(0).build();
map.moveCamera(CameraUpdateFactory.newCameraPosition(currentLocation));
//map.setMyLocationEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
}
activity_home_activity2.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/fragment_container"
android:background="#cccccc"
tools:context=".HomeActivity2">
</FrameLayout>
It looks like you have set paddings. Remove your paddings, build your app again and check if that works.