I have two Google Maps in my application, which I have implemented in the same way. One is working and one is not. The latter looks like an image and I can't interact with it. I can add markers and move the camera, but the info window doesn't show when I click the marker I can't move it, zoom in and zoom out. It looks like an image, a preview.
Here is the code:
public class MapFragment extends Fragment implements OnMapReadyCallback{
private GoogleMap mMap;
private static Double latitude;
private static Double longitude;
private static String title;
public static MapFragment newInstance(Double mLatitude, Double mLongitude, String mTitle) {
latitude=mLatitude;
longitude=mLongitude;
title=mTitle;
MapFragment fragment = new MapFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map_details);
mapFragment.getMapAsync(this);
return view;
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setAllGesturesEnabled(true);
mMap.getUiSettings().setMapToolbarEnabled(false);
mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon))
.position(new LatLng(latitude, longitude))
.title(title));
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude,longitude)).zoom(12f).build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
mMap.moveCamera(cameraUpdate);
}
public interface OnMapFragmentInteractionListener {
void onMapFragmentInteraction(Uri uri);
}
#Override
public void onResume() {
super.onResume();
}
#Override
public void onPause() {
super.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
}
}
You should take a look at XML. There is liteMode and when it's set to true it does exactly what you are describing.
<com.google.android.gms.maps.MapView
android:id="#+id/row_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:liteMode="true"
map:mapType="none" />
Here look at documentation
I'm just guessing but in code that you posted i see one mistake. To be sure you have to check your imports in the place when you try to use your MapFragment. As I said I guess that in one place it's imported from google pacage and another one from your class. The problem is that the MapFragment that you post here isn't a google MapFragment because you extants it from simple Fragment. To be sure rename your own MapFragment to CustomMapFragment and try to extands from MapFragment or just use it without customize.
Related
So, I basically put a GoogleMaps API inside a fragment in my app, and I want to give it a specific default search like we do with Intent, but in this case im using fragment instead of an intent.`public class LocalGymsFragment extends Fragment {
MapView mMapView;
private GoogleMap googleMap;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_local_gyms, container, false);
mMapView = (MapView) rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume(); // needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
// For dropping a marker at a point on the Map
LatLng sydney = new LatLng(-34, 151);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description"));
// 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;
}
#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();
}
}`
I suggest that you try implementing the GoogleMaps API on a constraint layout or new GoogleMaps Activity. This is because Fragments aren't just a cup of tea. It is tough implementing this on fragments.
I would like guidance on how to properly implement a google map in a view pager. I have done so in an activity and have no problems producing a map in a activity but when trying to do the same with a view pager I am not having much success.
I have found this question that asks something similar but trying it out leads me to an error that I can't resolve: How to put Google Maps V2 on a Fragment using ViewPager
I updated get map to get map Async following this guide: Replace getMap with getMapAsync.
However I am still no close to getting a google map to display in my view pager. I am uncertain as where else to go to for tutorials. I have found plenty of tutorials on how to get a map to show in an activity but none on how to get it to show with a view pager. Unable to find any guides on the subject relating to view pagers inside of maps.
I am currently unable to display a map in my fragment google map resources fragment.
If there are any resources or tutorials I can be provided that are up to date feel free to share them, I am currently unable to find a solution on my own.
Any ideas would be appreciated.
public Class MainActivity extends AppCompatActivity implements OnMapReadyCallback{
private GoogleMap googleMap;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
//The adapter adds the fragments created to the view so it can be seen by the user
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new SucessfullFosterYouthFragment(), "Sucessfull Foster Youth");
adapter.addFragment(new ContactusFragment(), "Contact US");
adapter.addFragment(new GoogleMapResourcesFragment(), "resources");
viewPager.setAdapter(adapter);
}
#Override
public void onMapReady(GoogleMap googleMap) {
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.googlemap);
mapFragment.getMapAsync(this);
}
public void setUpMap() {
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
googleMap.setMyLocationEnabled(true);
googleMap.setTrafficEnabled(true);
googleMap.setIndoorEnabled(true);
googleMap.setBuildingsEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
}
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
public class GoogleMapResourcesFragment 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_googlemapresources, container,
false);
mMapView = (MapView) v.findViewById(R.id.googlemap);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
// 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();
}
}
You should use MapView here as well instead of MapFragment:
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.googlemap);
mapFragment.getMapAsync(this);
Try this example below:
public class SomeFragment extends Fragment {
MapView mapView;
GoogleMap map;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.some_layout, container, false);
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
// 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();
}
}
Layout:
<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" >
<com.google.android.gms.maps.MapView android:id="#+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
I have a problem with pressing an option in my navigationView (R.id.create_marker) I want to create a marker in my MapFragment, but I can not do it, any suggestions?
Code of MainActivity.java where I press the menu option:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.create_marker:
Toast.makeText(MainActivity.this, "Crear Marker", Toast.LENGTH_SHORT).show();
// Create marker and display it on the map
break;
}
return false;
}
});
Code of MapFragment.java:
public class MapFragment extends Fragment
implements OnMapReadyCallback {
private View rootView;
private GoogleMap gMap;
private MapView mapView;
public MapFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_map, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mapView = (MapView) rootView.findViewById(R.id.map);
if (mapView != null) {
mapView.onCreate(null);
mapView.onResume();
mapView.getMapAsync(this);
}
}
#Override
public void onResume() {
super.onResume();
}
#Override
public void onMapReady(GoogleMap googleMap) {
}
}
This is the structure of my project:
To create a marker, you need to call addMarker method on your Map, once it's ready to be used.
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));
}
If you want your Map to open a marker for a fixed Lat-Lng, pass in the coordinates as shown in the code above, otherwise request location via Google Location Api and pass the obtained Lat-Lng values to the addMarker() method.
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.
this is my activity
public class FindPeopleFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap googleMap;
MapFragment fragment;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
initilizeMap();
return rootView;
}
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void initilizeMap() {
MapFragment fragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map1);
if (fragment != null) {
fragment.getMapAsync(this);
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
LatLng marker = new LatLng(44.797283, 20.460663);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(44.797283, 20.460663), 12));
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(44.797283, 20.460663))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
googleMap.setMyLocationEnabled(true);
}
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
#Override
public void onPause() {
final FragmentManager fragManager = this.getFragmentManager();
final Fragment fragment = fragManager.findFragmentById(R.id.map1);
if (fragment != null) {
fragManager.beginTransaction().remove(fragment).commit();
super.onPause();
}
}
#Override
public void onDestroy() {
super.onDestroy();
final FragmentManager fragManager = this.getFragmentManager();
final Fragment fragment = fragManager.findFragmentById(R.id.map1);
if (fragment != null) {
fragManager.beginTransaction().remove(fragment).commit();
}
}
}
I am using navigation drawer to show the map. my map is working but the marker on it does not show. I have used all permission and created the xml file well. I don't know what is the problem, I can't find a solution for this anywhere. I think I am having problem as I am using fragment, but I need it as I am using navigation drawer. please anybody? give me a solution, I'm stuck at this
You should implement onMapReadyCallback, you should set marker when map is ready. Once an instance of this interface is set on a MapFragment or MapView object, the onMapReady(GoogleMap) method is triggered when the map is ready to be used and provides a non null instance of GoogleMap.
The onMapReady() and method might not being called. Try to implement SupportMapFragment object that you retrieved from the XML layout. Then, call getMapAsync().
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(final GoogleMap googleMap) {
// ...
}
});