Add marker on map ANDROID - android

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

Related

Add Marker to Google Map in SupportFragment

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.

onMapReady Marker does not work

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

Google Maps Android API - Two Maps with getMapAsync()

I have two maps in one view fragment (see screenshot below), and although the maps run correctly and I can access one map via getMapAsync(), I am unable to understand how to run another getMapAsync() and distinguish it from the first.
Two maps together working well:
A fairly simple View Layout (called "fragment_map.xml"):
<com.google.android.gms.maps.MapView
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<android.support.v7.widget.CardView
android:id="#+id/map_card"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp">
<com.google.android.gms.maps.MapView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/miniMap"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp" />
</android.support.v7.widget.CardView>
In the Activity (or Fragment in my case):
private MapView mv, miniMap;
private GoogleMap gm;
private View myLayout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myLayout = inflater.inflate(R.layout.fragment_map, container, false);
mv = (MapView) myLayout.findViewById(R.id.mapview);
mv.onCreate(savedInstanceState);
miniMap = (MapView) myLayout.findViewById(R.id.miniMap);
miniMap.onCreate(savedInstanceState);
mv.getMapAsync(this);
miniMap.getMapAsync(this);
return myLayout;
}
#Override
public void onMapReady(GoogleMap map) {
gm = map;
gm.setMyLocationEnabled(true);
gm.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition position) {
gm.animateCamera(CameraUpdateFactory.newLatLng(position.target));
}
});
}
The maps inflate correctly and everything works smoothly, but as you can guess, whatever I do to "GoogleMap gm" happens to both maps due to
mv.getMapAsync(this);
miniMap.getMapAsync(this);
both pointing to a single
#Override
public void onMapReady(GoogleMap map) {
Any ideas as to how I can attach a separate GoogleMap to each MapView?
EDIT:
Thanks to Zeeshan Shabbir I used a boolean to first target the main map, then initialize the mini-map:
private GoogleMap gm;
private View myLayout;
private boolean miniMapReady = false;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myLayout = inflater.inflate(R.layout.fragment_map, container, false);
mv = (MapView) myLayout.findViewById(R.id.mapview);
mv.onCreate(savedInstanceState);
miniMap = (MapView) myLayout.findViewById(R.id.miniMap);
miniMap.onCreate(savedInstanceState);
mv.getMapAsync(this);
return myLayout;
}
#Override
public void onMapReady(GoogleMap map) {
if (!miniMapReady) {
gm = map;
gm.setMyLocationEnabled(true);
miniMap.getMapAsync(this);
miniMapReady = true;
} else {
miniGm = map;
}
gm.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition position) {
miniGm.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(position.target, position.zoom - 5.0f, position.tilt, position.bearing)));
}
});
}
The ChangeCamera Listener here allows the mini-map to follow the user's interaction on the main map while remaining at a lower zoom level.
Try to use boolean when minimap is ready set it to false. And in onMapReady check that boolean if it is true then do what you want for mv otherwise if boolean is false write logic for minimap

satellite map view not show in fragment

#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

onResume Method in Fragment is not working

In my application I am using navigation drawer to move from one fragment to another.
If I move from about_us fragment to another then if I again comeback to same the activity is not getting resumed, instead the application is getting closed unfortunately.
Can anyone tell me what is the problem in my code, pls tell me the solution to fix this.
public class AboutUsFragment extends Fragment {
public AboutUsFragment(){}
private GoogleMap gmap;
static final double latitude = 33.12615;
static final double longitude = 80.21932;
TextView aboutUs;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_about_us, container, false);
initilizeMap();
return rootView;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
View aboutUs = getActivity().findViewById(R.id.about_us_content_view);
try {
// Loading map
initilizeMap();
//To enable compass
gmap.getUiSettings().setCompassEnabled(true);
//To locate the school - Creating Marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("School Location");
// adding marker
gmap.addMarker(marker);
//We assigned latitude and longitude in Coordinate
LatLng coordinate = new LatLng(latitude, longitude);
CameraUpdate schoolLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5);
gmap.animateCamera(schoolLocation);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(coordinate) // Sets the center of the map to Mountain View
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
gmap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
} catch (Exception e) {
e.printStackTrace();
}
}
private void initilizeMap()
{
if (gmap == null) {
gmap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (gmap == null) {
Toast.makeText(getActivity(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
}
}
#Override
public void onResume() {
super.onResume();
initilizeMap();
}
}
And my about_us.xml is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="220dp" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/map"
android:layout_centerHorizontal="true" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/about_us_heading_View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/about_us"
android:paddingTop="5dp"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/about_us_content_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/abouts_us_content"
android:paddingTop="55dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
As I am completely new to Fragment. Someone pls tell me a noob way to fix it. Thanks a lot in advance.
add fragment to backstack before committing fragmnetTransaction
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.content_frame, fragment).commit();
Fragment with mapFragment in layout crashes when you resume cause for a fragment onCreateView is called every time you visit it every time.
Try to make your rootView static like below,
public static View mRootView;
public static boolean mOnRangeChange= false;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(mRootView==null){
mRootView = inflater.inflate(R.layout.fragment_map, container, false);
}else{
ViewGroup parent = (ViewGroup) mRootView.getParent();
if (parent != null)
parent.removeView(mRootView);
}
map_layout = (FrameLayout) mRootView.findViewById(R.id.map_layout);
mcategory = (Button) mRootView.findViewById(R.id.contact_categories);
mSharedpreferenceUtility = SharedpreferenceUtility.getInstance(getActivity());
mcategory.setOnClickListener(this);
try {
initializeMap(mRootView);
} catch (Exception e) {
e.printStackTrace();
}
return mRootView;
}

Categories

Resources