Actually i have a View pager with 3 tabs, in the first fragment i have a container for insert the map, this is my xml fragment_store
<layout xmlns:app="http://schemas.android.com/apk/res-auto">
...
<LinearLayout
android:id="#+id/linear_layout_container_map"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
/>
</layout>
and this is my FragmentStoreActivity:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
mapFragment = new SupportMapFragment();
getFragmentManager().beginTransaction().add(R.id.linear_layout_container_map, mapFragment).commitAllowingStateLoss();
mapFragment.getMapAsync(this);
return rootView;
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
BitmapDrawable bitmapdraw = (BitmapDrawable) ContextCompat.getDrawable(getActivity(), R.drawable.ic_ubicacion_mapa);
Bitmap b = bitmapdraw.getBitmap();
Bitmap smallMarker = Bitmap.createScaledBitmap(b, 115, 115, false);
location = new LatLng(Double.parseDouble(branche.getCoordLat()), Double.parseDouble(branche.getCoordLong()));
mMap.addMarker(new MarkerOptions().position(location).title(branche.getAddress())
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(location, ZOOM_CAMERA_MAP));
mMap.getUiSettings().setScrollGesturesEnabled(false);
}
All work fine, but in the verison to Android 8... when i swip the tabs the map cut off and the container turn black, if i turn off/on the screen the map recovery his normal shape
Screen work ok
Swip of th taps
Problem with the map
Related
I'm having trouble setting up a ViewPager using a PageTransformer and a MapView. The problem is when the viewpager is set to use the PageTransformer, the mapview disappears(goes transparent) while panning. However, if I don't use a transformer and just pan the Mapview is fine. Does anyone know what is causing this behavior?
Currently the PageTransformer does nothing special but just setting it seems to affect the way the mapview is drawn. I should note that the map goes transparent while the mapview(Legal Notice)remains. When the ViewPager enters an Idle state the Map appears.
viewPager.setPageTransformer(true, this);
...
#Override
public void transformPage(View view, float position) {
}
XML: ViewPager Fragment
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<com.google.android.gms.maps.MapView
android:id="#+id/mapcontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"/>
</FrameLayout>
Map Fragment:
private GoogleMap map;
private MapView mapView;
private Bundle mapBundle;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mapBundle = savedInstanceState;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.mapfragment, container, false);
MapsInitializer.initialize(getActivity());
mapView = (MapView) rootView.findViewById(R.id.mapcontainer);
mapView.onCreate(mapBundle);
map = mapView.getMap();
return rootView;
}
#Override
public void onResume(){
super.onResume();
mapView.onResume();
}
#Override
public void onPause(){
super.onPause();
mapView.onPause();
}
#Override
public void onDestroy(){
super.onDestroy();
mapView.onDestroy();
}
I found a solution from the discussion thread Bug: Black rectangle background on scroll that pointed me in the right direction. There seems to be some bugs in Google Play Services that can be resolved by setting the map to "Lite Mode". In my case it did the trick and the map is now working in the pagetransformer. From what I understand lite mode changes the map to a Bitmap Image. You can read more about it here Google Maps - Lite Mode. Lite mode has some limitation such as not being able to pan or zoom, but in my case it is not a requirement.
I updated my XML to this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<com.google.android.gms.maps.MapView
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/mapcontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
map:liteMode="true"/>
</FrameLayout>
You can also set this option in the code:
GoogleMapOptions options = new GoogleMapOptions().liteMode(true);
MapView mapView = MapView (context, options);
What i am doing::
I am using mapquest widget and api for maps
I am haveing the mapwidget inside the scrollview
I am able to click the map markers in the map also
What is happening::
I am not able to to zoom the map,
I can see zoom buttons clickit also but it wont zoom
code::
MyFragment.java
public class MyFragment extends Fragment{
protected MapView map;
AnnotationView annotation;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment, container,false);
map = (MapView) rootView.findViewById(R.id.map);
map.getController().setZoom(10);
map.getController().setCenter(new GeoPoint(12.918609,77.668912));
map.setBuiltInZoomControls(true);
// initialize the annotation to be shown later
annotation = new AnnotationView(map);
//addPolyOverlay();
// addLineOverlay();
return rootView;
}
#Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
addPoiOverlay();
}
// add an itemized overlay to map
private void addPoiOverlay() {
// use a custom POI marker by referencing the bitmap file directly,
// using the filename as the resource ID
Drawable icon = getResources().getDrawable(R.drawable.distance_icon_large);
final DefaultItemizedOverlay poiOverlay = new DefaultItemizedOverlay(icon);
// set GeoPoints and title/snippet to be used in the annotation view
OverlayItem poi1 = new OverlayItem(new GeoPoint (12.918609,77.668912), "Denver, Colorado", "MapQuest Headquarters");
poiOverlay.addItem(poi1);
OverlayItem poi2 = new OverlayItem(new GeoPoint (12.956868,77.701148), "Palo Alto, California", "AOL Offices");
poiOverlay.addItem(poi2);
// add a tap listener for the POI overlay
poiOverlay.setTapListener(new ItemizedOverlay.OverlayTapListener() {
#Override
public void onTap(GeoPoint pt, MapView mapView) {
// when tapped, show the annotation for the overlayItem
int lastTouchedIndex = poiOverlay.getLastFocusedIndex();
if(lastTouchedIndex>-1){
OverlayItem tapped = poiOverlay.getItem(lastTouchedIndex);
annotation.showAnnotationView(tapped);
}
}
});
map.getOverlays().add(poiOverlay);
}
}
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.mapquest.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="700dp"
android:apiKey="My-Key"
android:focusableInTouchMode="true"
android:clickable="true"
android:padding="5dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
You may need to override the touchlistener in the scrollview and then forward those events to the mapview if appropriate. The problem is that the scrollview is taking over the touch listener without properly forwarding it out.
mapview inside of a scrollview seems messy though....
so I am currently doing a navigation application where I need to display several tabs, one of which show a google map.
I have used FragmentPagerAdapter to set up the multi-pages UI and so far for the 2 remaining fragments which do not implements any other nested fragment, it works just fine.
For the map fragment, I noticed the problem of nested fragment, so I followed this guide: https://code.google.com/p/gmaps-api-issues/issues/detail?id=5064#c1 and put the things accordingly into my MapFragment, however, the Fragment does not show me anything, it is just a blank page (I have some other things in that fragment, not just the google map fragment).
Please see where I made mistake, thank you very much :) If you need any other part of the code please tell me.
Here is the code of the Map Fragment
public class MapFragment extends FragmentControl implements OnSeekBarChangeListener{
public static MapFragment getInstance() {
if(MapFragment.instance==null) {
Log.e(TAG,"Map Fragment is not loaded");
}
return MapFragment.instance;
}
public static MapFragment newInstance(int position, String title){
MapFragment mapFragment = new MapFragment();
Bundle args = new Bundle();
args.putInt("current_page", 1);
args.putString("page_tile", "Map Information");
mapFragment.setArguments(args);
return mapFragment;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCurrentPage = getArguments().getInt("current_page", 1);
pageTitle = getArguments().getString("page_title");
MapFragment.instance=this;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
layout = inflater.inflate(R.layout.map, container, false);
if (layout==null){
Log.d(TAG,"layout null");
return null;
}
CheckBox camera = (CheckBox)layout.findViewById(R.id.cameraMove);
camera.setChecked(MapFragment.moveCamera);
camera.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
MapFragment.moveCamera = isChecked;
}
});;
mTransparencyBar = (SeekBar)layout.findViewById(R.id.transparencySeekBar);
mTransparencyBar.setMax(TRANSPARENCY_MAX);
mTransparencyBar.setProgress(0);
markerList = new ArrayList<Marker>();
return layout;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
supportfragment = (SupportMapFragment) fm.findFragmentById(R.id.googlemap);
if (supportfragment == null) {
supportfragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.googlemap, supportfragment).commit();
fm.executePendingTransactions();
}
}
#Override
public void onResume() {
super.onResume();
this.createUiMap();
mapTimer = new Timer();
mapTimer.scheduleAtFixedRate(new updateCalculationTask(), 0, 100);
mapTimer.scheduleAtFixedRate(new updateUITask(), 50, MainActivity.uiUpdateRate);
}
void createUiMap() {
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = supportfragment.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
populateMapStartPointsSpinner(layout);
setStartPointSpinnerListener(layout);
this.setUpMarker(this.defaultStartPoint.x, this.defaultStartPoint.y);
FetchSQL.setDRFixData(this.setLocation(defaultStartPoint));
}
}
}
private void setUpMap() {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(NEWARK, 20));
mImages.add(BitmapDescriptorFactory.fromResource(R.drawable.i3f2));
mCurrentEntry = 0;
LatLngBounds newarkBounds = new LatLngBounds(
new LatLng(1.291926, 103.775114), // South west corner
new LatLng(1.292833, 103.776310)); // North east corner
mGroundOverlay = mMap.addGroundOverlay(new GroundOverlayOptions()
.image(mImages.get(mCurrentEntry))
.positionFromBounds(newarkBounds));
mTransparencyBar.setOnSeekBarChangeListener(this);
}
}
And here is the map.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="#+id/cameraMove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cameraMove"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/mapStartPointSpinnerLabel"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cameraMove"
android:layout_toRightOf="#id/cameraMove"
android:text="#string/mapStartPointSpinnerLabel" />
<Spinner
android:id="#+id/mapStartPointSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cameraMove"
android:layout_toRightOf="#id/mapStartPointSpinnerLabel" />
<LinearLayout
android:id="#+id/seekbarmenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/mapStartPointSpinnerLabel"
android:orientation="horizontal"
android:layout_marginTop="15dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/transparency"
android:layout_gravity="center_vertical" />
<SeekBar
android:id="#+id/transparencySeekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/seekbarmenu"
android:layout_marginTop="15dip" >
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dip"
class="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/googlemap" />
</LinearLayout>
How it is supposed to look like (inside Eclipse View):
How it is on my phone:
Update: After some times looking through the internet, I suspects that the problem comes from the inflating part of the code (if not at least it should show something). May be to in inflate a Relative layout, you need to have some special code than when you inflate a Linear Layout.
All the codes I found is for Activity inflation, if anyone know how to inflate a Relative Layout in Fragment, please help. Thank you :)
from android developers :
Note: You cannot inflate a layout into a fragment when that layout includes a <fragment>. Nested fragments are only supported when added to a fragment dynamically.
So, instead of <fragment> element use some container view (FrameLayout for example) for adding fragment dynamicly
Update: My problem comes from the most unexpected place, I declare a getView() function in the MapFragment which seems to be overriding a function in Fragment class and everything get wrong from that part. Just delete the function out and it will work like a champ.
This do shows me that error can come from place where you dont expect (What I do is to comment the whole thing out then test block by block till I find the part that makes my program goes wrong).
Thank you very much for others to reply to me. Very appreciate your help :)
Hi in my application i m displaying one fragment. Inside that fragment I am displaying map. So my code looks like :
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/innerrelativelay"
android:padding="10dp">
<TextView
android:id="#+id/storename"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Store Name:"
android:textSize="15sp"
/>
<TextView
android:id="#+id/storeaddress"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#+id/storename"
android:text="Store Address:"
android:textSize="15sp"
/>
</RelativeLayout>
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mapView"
android:layout_below="#+id/innerrelativelay"
/>
</RelativeLayout>
and fragment looks like :
public class MyAccount extends SherlockFragment {
MapView map;
GoogleMap mMap;
TextView storeName,storeAddress;
SharedPreferences storeInfo,authenticationInfo;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.maplayout, container, false);
map = (MapView) v.findViewById(R.id.mapView);
map.onCreate(savedInstanceState);
mMap = map.getMap();
map.onCreate(savedInstanceState);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
mMap.setMyLocationEnabled(true);
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);
mMap.animateCamera(cameraUpdate);
mMap.setMyLocationEnabled(true);
storeName = (TextView)v.findViewById(R.id.storename);
storeAddress = (TextView)v.findViewById(R.id.storeaddress);
storeInfo = getActivity().getSharedPreferences(CommonSharedPref.QCMERCHANT_STOREID, 0);
authenticationInfo = getActivity().getSharedPreferences(CommonSharedPref.QCMERCHANT_AUTHENTICATION_INFO, 0);
storeName.setText(storeName.getText().toString()+storeInfo.getString(CommonSharedPref.QCMERCHANT_STORENAME, ""));
storeAddress.setText(storeAddress.getText().toString()+storeInfo.getString(CommonSharedPref.QCMERCHANT_ADDRESS1, "")
+"\n"+storeInfo.getString(CommonSharedPref.QCMERCHANT_ADDRESS1, ""));
return v;
}
}
It shows me map properly but creating marker, showing current location or move camera at given location not working properly.
How to do this. Need help. Thank you.
UPDATE
Try changing the layout of fragment as
<fragment
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
Also it seems you havent initialised the markers yet. Thats why markers arent visible..
If you need pointers of how to do it.. There is a very good tutrial page...
http://www.vogella.com/articles/AndroidGoogleMaps/article.html
From here https://developers.google.com/maps/documentation/android/v1/hello-mapview, you can see that
Version 1 of the Google Maps Android API as been officially
deprecated as of December 3rd, 2012
So you shouldn't use MapView anymore. I suggest you use a MapFragment instead. See the excellent documentation for it here: https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment
I'm using GoogleMap V2 and I have a gridView and a mapView like that:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >
<com.google.android.gms.maps.MapView
android:id="#+id/map_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5" />
<GridView
android:id="#+id/gv_all_pubs"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:background="#color/BurlyWood"
android:columnWidth="300dp"
android:horizontalSpacing="5dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="3dp" />
</LinearLayout>
</FrameLayout>
the code is that:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = null;
try
{
view = inflater.inflate(R.layout.all_pubs_layout, container, false);
mapView = (MapView)view.findViewById(R.id.map_view);
mapView.onCreate(savedInstanceState);
map = mapView.getMap();
//set the map options
map.setMyLocationEnabled(false);
map.getUiSettings().setMyLocationButtonEnabled(false);
map.getUiSettings().setZoomControlsEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.getUiSettings().setCompassEnabled(true);
try
{
MapsInitializer.initialize(mContext);
}
catch (Exception e){}
gvAllPubs = (GridView)view.findViewById(R.id.gv_all_pubs);
gvAdapter = new AllPubsGridViewAdapter(mContext, App.pubSortedByLastUpdate);
gvAllPubs.setAdapter(gvAdapter);
gvAllPubs.setOnItemClickListener(gridViewClickListener);
//select the first item in the list
try
{
gvAllPubs.setSelection(lastPubSelected);
drawLastPubSelected(lastPubSelected);
}
catch (Exception e) {}
}
catch(Exception e){}
return view;
}
and the gridview item click listener is:
OnItemClickListener gridViewClickListener = new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id)
{
lastPubSelected = position;
drawLastPubSelected(lastPubSelected);
}
};
private void drawLastPubSelected(int position)
{
//get the selected pub
PubObj selectedPub = App.pubSortedByLastUpdate.get(position);
double pubLat = selectedPub.pubLatitude;
double pubLong = selectedPub.pubLongtitude;
//get the pub location
LatLng pubLocation = new LatLng(pubLat, pubLong);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(pubLocation, 15);
//zoom the map to the pub
map.animateCamera(cameraUpdate);
//create the marker at the pub location
MarkerOptions mapMarkerOption = new MarkerOptions();
mapMarkerOption.position(pubLocation);
mapMarkerOption.title(selectedPub.pubName.toString() +" - "+selectedPub.pubAddress.toString());
mapMarkerOption.snippet(selectedPub.pubCurrentStatus.toString());
mapMarkerOption.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
mapMarkerOption.draggable(false);
map.clear();
Marker marker = map.addMarker(mapMarkerOption);
marker.showInfoWindow();
//on marker click open pub details activity
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener()
{
#Override
public void onInfoWindowClick(Marker marker)
{
//open pub details activity
Log.d("pubs", "a");
}
});
}
now I have two devices that I debug on, the first one is samsung galaxy s2 with android version 4.1.2 and the other one is nexus 4 with android version 4.2.2 ,
the problem is that when I zoom in on my nexus 4 I see a blank screen (grey squares and I can see only the zoom buttons) and on my sgs2 i can zoom in and out without a problem and i see everything perfect.
What can be the problem?
I'm struggling with this issue for a quiet some time now.
Thank's in advance for all who can help.
Best,
Shalom Melamed.
Problem solved by completely delete the from my nexus 4 and re-install it.
Hope it will help anyone.