Show Google Map in Material Dialog Android - android

I want to show google map in to a custom dialogue.. But i can not access SupportFragmentManager from dialogue class...
Here is my code....
public class AddressViewDialog extends MaterialDialog.Builder implements OnMapReadyCallback {
private MaterialDialog reviewDialog;
private double latitude = 23.0;
private double longitude = 90.0;
private SupportMapFragment mMapFragment;
private GoogleMap mMap;
public AddressViewDialog(Context context) {
super(context);
this.context = context;
initializeView();
}
public void initializeView() {
reviewDialog = new MaterialDialog.Builder(this.getContext())
.title(R.string.request_to_introduce)
.customView(R.layout.dialog_pending_introducer_review, true)
.show();
View v = reviewDialog.getCustomView();
mMapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.fragment);
mMapFragment.getView().setVisibility(View.GONE);
if (latitude != 0.0 && longitude != 0.0) {
mMapFragment.getView().setVisibility(View.VISIBLE);
mMapFragment.getMapAsync(this);
} else
mMapFragment.getView().setVisibility(View.GONE);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng currentLoc = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(currentLoc).title("My Location"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLoc, 14.0f));
}
}
Here is my xml..
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_margin="#dimen/activity_horizontal_margin">
<fragment
android:id="#+id/fragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="250dp" />
</LinearLayout>
It shows error in this line:
mMapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.fragment);
I also Tried :
mMapFragment = (SupportMapFragment) this.getSupportFragmentManager()
.findFragmentById(R.id.fragment);

You should call getSupportFragmentManager() not on MaterialDialog.Builder object (your AddressViewDialog extends MaterialDialog.Builder) but on AppCompatActivity object. So use
mMapFragment = (SupportMapFragment) ((AppCompatActivity) this.context).getSupportFragmentManager()
.findFragmentById(R.id.fragment);
instead of
mMapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.fragment);
and create your AddressViewDialog from MainActivity this way:
new AddressViewDialog(MainActivity.this).show();
(MainActivity should extends AppCompatActivity).

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.

How to add a satellite view in android studio?

My map is working fine.However, i want to add a satellite view along with my normal view? How can i achieve that?
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#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) {
mMap = googleMap;
LatLng location = new LatLng(x,y);
mMap.addMarker(new MarkerOptions().position(ReduitBusStop).title("you are here!"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
Try with setting the type of map tiles as below
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
Use the below function to set the satellite view
setMapType(com.google.android.gms.maps.GoogleMap.MAP_TYPE_SATELLITE);

How to get Google Maps object inside a Fragment

I am using fragments in my application and I am new for this. There is a fragment in which I want to display Google Map and want to get its object, for this I have a fragment in my XML and I want to inflate it in the Fragment itself. When I am trying to inflate the map view its showing me error for getSupportFragmentManager().
How do I get the map object then that is the main issue. My XML is like this :-
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="250dp"
class="com.google.android.gms.maps.SupportMapFragment" />
And my Fragment in which I want to get the Google Map object is like that :-
public class FindMyCar extends Fragment {
private TextView mTvFind;
private TextView mTvPark;
private EditText mEtParkHint;
private GoogleMap map;
#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.findmycar, null);
initViews(view);
Typeface type = Typeface.createFromAsset(getActivity().getResources().getAssets(),"Multicolore.otf");
mTvFind.setTypeface(type);
mTvPark.setTypeface(type);
mEtParkHint.setTypeface(type);
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
return view;
}
#Override
public void onDestroyView() {
super.onDestroyView();
}
#Override
public void onDestroy() {
super.onDestroy();
}
private void initViews(View view){
mTvPark = (TextView) view.findViewById(R.id.tvFindCarPark);
mTvFind = (TextView) view.findViewById(R.id.tvFindCar);
mEtParkHint = (EditText) view.findViewById(R.id.etParkHint);
}
Can someone help me to get the object for my map so that I can show current location and draw the marker there.
Any help would be appreciable.
Thanks in advance.
getMap() method is deprecated
getMap() method is deprecated but after the play-services 9.2it is removed, so better use getMapAsync(). You can still use getMap() only if you are not willing to update the play-services 9.2 for your app.
To use getMapAsync(), implement the OnMapReadyCallback interface on your activity or fragment:
For fragment:
public class MapFragment extends android.support.v4.app.Fragment
implements OnMapReadyCallback { /* ... */ }
Then, while initializing the map, use getMapAsync() instead of getMap():
//call this method in your onCreateMethod
private void initializeMap() {
if (mMap == null) {
SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.fragment_map);
mapFrag.getMapAsync(this);
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUpMap();// do your map stuff here
}
For Activity:
public class MapActivity extends FragmentActivity
implements OnMapReadyCallback { }
Then, while initializing the map, use getMapAsync() instead of getMap():
//call this method in your onCreateMethod
private void initializeMap() {
if (mMap == null) {
SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_map);
mapFrag.getMapAsync(this);
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUpMap();// do your map stuff here
}
Fragment in XML:
<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"/>
Try this
GoogleMap map = ((SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
and in your xml
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
Try this it works for me
How to put Google Maps V2 on a Fragment Using ViewPager
<Fragment
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
GoogleMap mGoogleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
Try this i have made this as a sample according to your need
public class LocationMapActivity extends FragmentActivity {
private GoogleMap mMap;
static boolean Iscamera = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.locatio_map);
setUpMapIfNeeded();
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
mMap.setMyLocationEnabled(true);
if (mMap != null) {
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getMaxZoomLevel();
mMap.getMinZoomLevel();
mMap.getUiSettings();
mMap.animateCamera(CameraUpdateFactory.zoomIn());
mMap.animateCamera(CameraUpdateFactory.zoomOut());
mMap.animateCamera(CameraUpdateFactory.zoomTo(20));
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location arg0) {
// TODO Auto-generated method stub
mMap.clear();
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(arg0.getLatitude(), arg0
.getLongitude()))
.title("I am Here!!")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.map_icon)));
if (!Iscamera) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(arg0.getLatitude(), arg0
.getLongitude()), 14));
Iscamera = true;
}
try {
if (Constant.FORTNAME.equals("Arad Fort")) {
mMap.addMarker(new MarkerOptions()
.position(new LatLng(26.2525, 50.6269))
.title(Constant.FORTNAME)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.greenicon)));
} else if (Constant.FORTNAME.equals("Bahrain Fort")) {
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(26.2333598,
50.52035139))
.title(Constant.FORTNAME)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.greenicon)));
} else if (Constant.FORTNAME
.equals("International Circuit")) {
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(26.0303251,
50.51121409))
.title(Constant.FORTNAME)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.greenicon)));
} else if (Constant.FORTNAME
.equals("Khamis Mousque")) {
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(26.158719, 50.516426))
.title(Constant.FORTNAME)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.greenicon)));
} else if (Constant.FORTNAME
.equals("king fahad causeway")) {
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(26.1723987,
50.4579942))
.title(Constant.FORTNAME)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.greenicon)));
} else if (Constant.FORTNAME.equals("Riffa Fort")) {
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(26.11771965026855,
50.56298065185547))
.title(Constant.FORTNAME)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.greenicon)));
} else if (Constant.FORTNAME
.equals("Royal Golf Club")) {
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(26.13000, 50.55500))
.title(Constant.FORTNAME)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.greenicon)));
} else if (Constant.FORTNAME.equals("Tree of life")) {
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(25.9940396,
50.583135500000026))
.title(Constant.FORTNAME)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.greenicon)));
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
e.getMessage(), Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
});
}
}
}
// ********************************************************************************************
#SuppressWarnings("unused")
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title(
"Marker"));
}
}
public class MapActivity extends FragmentActivity
implements OnMapReadyCallback { }
Then, while initializing the map, use getMapAsync() instead of getMap():
//call this method in your onCreateMethod
private void initializeMap() {
if (mMap == null) {
SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_map);
mapFrag.getMapAsync(this);
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
You can use this to making map but it will return map object is null because you don't find solution with getMapAsync to create a new object mMap. It only background task with return null
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/location_map"
android:layout_above="#id/atmLocation_recyclerView"
/>
View root= inflater.inflate(R.layout.fragment_a_t_m_locations, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.location_map);
mapFragment.getMapAsync(googleMap -> {
mMap=googleMap;
if(mMap!=null){
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
});
Revised code:
Your xml file-
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Your java file (only relevant code)-
View view = null, mapView = null;
private GoogleMap mMap;
view = inflater.inflate(R.layout.findmycar, container,false);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
SupportMapFragment supportMapFragment = (SupportMapFragment) fragmentManager
.findFragmentById(R.id.map);
mMap = supportMapFragment.getMap();
mapView = (View) view.findViewById(R.id.map);

Categories

Resources