i Have a Fragment that should show a Specific place on Google Map
public class ContactFragment extends Fragment implements OnMapReadyCallback {
public ContactFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
FragmentManager fragment = getActivity().getSupportFragmentManager();
final SupportMapFragment myMAPF = (SupportMapFragment) fragment.findFragmentById(R.id.map);
return inflater.inflate(R.layout.fragment_contact, container, false);
}
#Override
public void onMapReady(GoogleMap googleMap) {
LatLng marker = new LatLng(34.877737,-1.326545);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker,13));
googleMap.addMarker(new MarkerOptions().position(marker));
}
}
and in the XML file of the Fragment i have this :
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
android:layout_below="#+id/contact_domiciliation"
class="com.google.android.gms.maps.MapFragment"
></fragment>
Yet when i launch click on the button that opens this fragment i get a map of the entire globe .
Add this line of code to the fragment:
myMAPF.getMapAsync(this);
Related
I did a tabbed-activity with for tabs. In the fourth tab I did a map, and I added a marker but the marker is not visible and I do not undertand why. I tried many solutions for my problem, but for now I have come to this situation.
This is my Java code:
public class tab_mappa extends SupportMapFragment implements OnMapReadyCallback {
private GoogleMap googleMap;
SupportMapFragment mSupportMapFragment;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
View root = inflater.inflate(R.layout.tabmappa, null, false);
mSupportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
if (mSupportMapFragment == null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
mSupportMapFragment = SupportMapFragment.newInstance();
fragmentTransaction.replace(R.id.map, mSupportMapFragment).commit();
}
return root;
}
#Override
public void onMapReady(final GoogleMap googleMap) {
this.googleMap = googleMap;
this.googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
this.googleMap.getUiSettings().setZoomControlsEnabled(true);
this.googleMap.getUiSettings().setCompassEnabled(true);
this.googleMap.getUiSettings().setMyLocationButtonEnabled(true);
this.googleMap.getUiSettings().setZoomGesturesEnabled(true);
this.googleMap.getUiSettings().setRotateGesturesEnabled(true);
LatLng pos1 = new LatLng(44.783878,10.879663);
final LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(pos1);
this.googleMap.addMarker(new MarkerOptions().position(pos1));
this.googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
#Override
public void onMapLoaded() {
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 100));
}
});
}
#Override
public void onPause() {
super.onPause();
}
#Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (googleMap == null) {
getMapAsync(this);
}
}
}
This is my xml 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"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:orientation="vertical"
tools:context="esame.progetto.xhondar.github.com.info.tab_mappa">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.03"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map" />
</LinearLayout>
And this is my final result:
Do you have any ideas?
I solved the problem, this is the solution for those who need it:
Java code:
import android.support.v4.app.Fragment;
public class tab_mappa extends Fragment implements OnMapReadyCallback {
GoogleMap map;
SupportMapFragment mapFragment;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tabmappa, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return v;
}
#Override
public void onMapReady(GoogleMap googleMap){
map = googleMap;
this.map = googleMap;
this.map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
this.map.getUiSettings().setZoomControlsEnabled(true);
this.map.getUiSettings().setCompassEnabled(true);
this.map.getUiSettings().setMyLocationButtonEnabled(true);
this.map.getUiSettings().setZoomGesturesEnabled(true);
this.map.getUiSettings().setRotateGesturesEnabled(true);
LatLng pp = new LatLng(44.783878,10.879663);
map.addMarker(new MarkerOptions().position(pp).title("Carpi"));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(pp, 8));
}
}
Xml code:
<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="esame.progetto.xhondar.github.com.info.tab"/>
The error was located in xml code: is Fragment and not FrameLayout, this thing brought conflict within the code.
How about use marker without the builder? you wrote the code in onMapReady()
LatLng pos1 = new LatLng(44.783878,10.879663);
final LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(pos1);
this.googleMap.addMarker(new MarkerOptions().position(pos1));
this.googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
#Override
public void onMapLoaded() {
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 100));
}
});
it change to
LatLng pos1 = new LatLng(44.783878,10.879663);
googleMap.addMarker(new MarkerOptions().position(pos1));
this.googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
#Override
public void onMapLoaded() {
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 100));
}
});
this one. I removed builder and this before googleMap.
Excuse me for any grammatical errors.
I followed a tutorial to view the google map in a fragment, but something has gone wrong.
this is my file .java that is hooked with the fragment:
public class MapFragment extends Fragment implements OnMapReadyCallback{
#Nullable
#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 onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this); //Here i get the error
}
#Override
public void onMapReady(GoogleMap googleMap) {
}
}
This is the layout fragment:
<fragment
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"></fragment>
What did I do wrong?
Thank you!
You have to use SupportMapFragment rather than MapFragment
in XML
<fragment
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"></fragment>
in Fragment
SupportMapFragment fragment = (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this);
I have simple Fragment class, specialized for displaying Google Map:
public class MapFragment extends Fragment implements Map, OnMapReadyCallback {
private static final String TAG = MapFragment.class.getSimpleName();
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
MapView googleMapView = (MapView) view.findViewById(R.id.mapFragment_mapView);
googleMapView.onCreate(savedInstanceState);
googleMapView.getMapAsync(this);
return view;
}
#Override
public void onMapReady(GoogleMap googleMap) {
Log.d(TAG, "onMapReady have called");
MapsInitializer.initialize(this.getActivity());
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mapFragment_mapView" />
</FrameLayout>
When i run my app, i can see the log message, which means i've got a valid object GoogleMap. But on the screen i see this picture:
Object GoogleMap is really valis - for example i can turn on My Location button and i will see it on screen. So whymap tiles not loading???
You can make it work like this. This is having an additional benefit that the fragment can have other elements too displayed if needed.
public class DashBoardFragment extends Fragment{
private MapView mMapView;
private GoogleMap mGoogleMap;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_dash_board, container, false);
mMapView = (MapView) view.findViewById(R.id.map_dashBoard);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mGoogleMap = mMapView.getMap();
return view;
}
}
xml for the fragment goes like this
<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.screens.DashBoardFragment">
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map_dashBoard"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
Google play services should be available in device. You can check by using this.
public static boolean isGooglePlayServiceAvailable(Activity context){
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
if(status==ConnectionResult.SUCCESS){
return true;
}else{
GooglePlayServicesUtil.getErrorDialog(status, context, 10).show();
return false;
}
}
And last but very much important , our map key should be present in Manifest
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your google map key" />
You have to call the 'getMapAsync()' function to display the map.In your onCreateView use getMapAsync() to set the callback on the fragment.
Here's a sample code for getMapAsync:
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map); mapFragment.getMapAsync(this);
You may also try to read the Official Google Documentation for MapFragment:
https://developers.google.com/android/reference/com/google/android/gms/maps/MapFragment#getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)
#EFragment(R.layout.main_map)
public class MapActivity extends Fragment {
private GoogleMap map;
static final LatLng DURG = new LatLng(21.1900,81.2800);
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_map, container, false);
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
return view;
}
}
Null pointer exception error occure at map.setMapType(GoogleMap.MAP_TYPE_SATELLITE).
R.layout.main_map:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/map_fragment_id"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
You need to add MapFragment Dynamically when you want to inflate a MapFragment inside a Fragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_map, container, false);
SupportMapFragmentmf = SupportMapFragment.newInstance();
FragmentManager fm = getChildFragmentManager();
fm.beginTransaction().replace(R.id.mapView, mf, "normal_map").commit();
mf.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap arg0) {
map = arg0;
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
});
return view;
}
where your main_map should be like below
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I hope that helps. Please let me know if you have any doubts.
you should use
getMapAsync(OnMapReadyCallback callback)
then in the callback you can set the map to satellite view
Good night,
I have an application with a map in a swipe tab, but I want that map select a point and start at that point.
Thank you in advance.
Enclose JAVA:
public class Mapa extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View android = inflater.inflate(R.layout.mapa_3, container, false);
return android;
}
#Override
public void onDestroyView() {
super.onDestroyView();
Fragment f = getFragmentManager().findFragmentById(R.id.mapFragment);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
}
the layout:
<?xml version="1.0" encoding="utf-8"?>
<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:id="#+id/map_container"
tools:context=".Mapa" >
<fragment
android:id="#+id/mapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
I guess you want to add a marker to a map and set camera to look at it. Here is how you can achieve it(more info here and here):
GoogleMap map;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View android = inflater.inflate(R.layout.mapa_3, container, false);
final LatLng position = new LatLng(MARKER_LATITUDE, MARKER_LONGITUDE);
// marker
map = ((MapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment)).getMap();
map.addMarker(new MarkerOptions()
.position(position)
.title("Hello world"));
// camera position
map.setOnCameraChangeListener(new OnCameraChangeListener()
{
#Override
public void onCameraChange(CameraPosition arg0)
{
CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(position, 14);
map.animateCamera(cu);
map.setOnCameraChangeListener(null);
}
});
return android;
}