Google Map with Bottom Navigation in android studio - android

I'm developing Android Google Map project in Android Studio. I have a MapActivtity to show location(The Blew Code:)
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private LatLng marker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// Add a marker in Sydney and move the camera
LatLng iran = new LatLng(29, 52);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(iran, 5.0f));
addMarker(29.1,52.2,"ssss");
}
public void addMarker(double Lat,Double Lng, String Title){
marker=new LatLng(Lat,Lng);
mMap.addMarker(new MarkerOptions().position(marker).title(Title));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(marker, 10.0f));
//mMap.moveCamera(CameraUpdateFactory.newLatLng(marker));
}
And my layout is like this:
<?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/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
when the App run, Map Show Correctly, But I want to show Map like below image with bottom navigation. when the map button select, The map show.

Related

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().

How to get marker position when drag googleMap in android

In my application i set one image in my layout center of map, and i want when drag googleMap show latLong of this image (marker).
layout code:
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/mainToolbar"
layout="#layout/toolbar_main" />
<fragment
android:id="#+id/mainMap"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/mainToolbar"
tools:context="com.tida.utils.safine.Activities.MainActivity" />
<ImageView
android:id="#+id/location_setter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/ic_location_origin_pin" />
</RelativeLayout>
Java code:
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private UiSettings mUiSettings;
private CameraUpdate mapCenter, mapZoom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mainMap);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mUiSettings = mMap.getUiSettings();
mUiSettings.setScrollGesturesEnabled(true);
mUiSettings.setZoomGesturesEnabled(true);
mUiSettings.setTiltGesturesEnabled(true);
mUiSettings.setRotateGesturesEnabled(true);
/*Set center of Tehran location*/
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(35.724414, 51.427059), 18.0f));
/*Map listener*/
mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
#Override
public void onMarkerDragStart(Marker marker) {
Log.e("mapDrag", "DragStart : " + marker.getPosition());
}
#Override
public void onMarkerDrag(Marker marker) {
Log.e("mapDrag", "Drag : " + marker.getPosition());
}
#Override
public void onMarkerDragEnd(Marker marker) {
Log.e("mapDrag", "DragEnd : " + marker.getPosition());
}
});
}
}
How can i set my image for marker and get this position ?
marker.getPosition() returns LatLng
you should use:
marker.getPosition().latitude
&
marker.getPosition().longitude
set marker using global variable of marker and call mMarker.setPosition(marker.getPosition());

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.

android google maponClick not responsive

I have a map in my layout which when clicked should addmarker to it and display it on map.But the onMapClick method is not triggered when clicked.Below is the code":
Details.java:
public class Details extends AppCompatActivity implements OnMapReadyCallback,GoogleMap.OnMapClickListener {
SupportMapFragment mapFragment;
private GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map1);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
map=googleMap; LatLng bolg = new LatLng(9.984616,76.267082);
map.addMarker(newMarkerOptions().position(bolg).title("Demo")).showInfoWindow(); map.moveCamera(CameraUpdateFactory.newLatLng(bolg));
map.animateCamera(CameraUpdateFactory.zoomTo(7), 3000, null);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
#Override
public void onMapClick(LatLng latLng) {
Log.e("LongClick","clicked");
map.addMarker(new MarkerOptions().position(latLng).title("X")).showInfoWindow();
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
map.animateCamera(CameraUpdateFactory.zoomTo(7), 3000, null);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
}
details.xml:
<?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:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/map1"
android:name="com.google.android.gms.maps.SupportMapFragment">
</fragment>
</LinearLayout>
To detect user click event on GoogleMap, modify the AppCompatActivity
implements OnMapClickListener, override onMapClick(LatLng point), and
call map.setOnMapClickListener(this).
map.setOnMapClickListener(this);
In your onMapReady set an on click listener map.setOnclicklistener
After getting google map from mapfragment you need to do the following for receiving click events.
GoogleMap mGoogleMap = mapFragment.getMap();
mGoogleMap.setOnMapClickListener(this);
you need to implement this listener also GoogleMap.OnMapClickListener

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);

Categories

Resources