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);
Related
I am using AndroidPlot for displaying dynamic data on a graph in my android App. The graph is displayed in a fragment, which is a part of a tabbed screen (view pager). The graph needs to be displayed in landscape and portrait mode.
When the graph is displayed on the portrait mode, I can see it properly. However, when the phone is turned into landscape mode, the graph disappears completely. This happens only when the AndroidPlot graph is used in a Fragment. When AndroidPlot graph is used in an activity directly without fragment, the graph is displayed correctly in landscape and portrait mode.
Has anyone experienced this? Any help in resolving this issue is appreciated.
My layout.xml file is as follows:
<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" tools:context="com.monitrahealth.mhsmartmct.TestFragment" android:id="#+id/fragment_test">
<com.androidplot.xy.XYPlot
android:id="#+id/mplot"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="4"
androidplot.renderMode="use_background_thread"
androidPlot.titleWidget.labelPaint.textSize="#dimen/title_font_size"
androidPlot.domainLabelWidget.visible="false"
androidPlot.domainLabelWidget.labelPaint.textSize="#dimen/domain_label_font_size"
androidPlot.rangeLabelWidget.visible="false"
androidPlot.rangeLabelWidget.labelPaint.textSize="#dimen/range_label_font_size"
androidPlot.graphWidget.marginTop="0dp"
androidPlot.graphWidget.marginLeft="0dp"
androidPlot.graphWidget.marginBottom="0dp"
androidPlot.graphWidget.marginRight="0dp"
androidPlot.graphWidget.rangeLabelPaint.textSize="#dimen/range_tick_label_font_size"
androidPlot.graphWidget.rangeOriginLabelPaint.textSize="#dimen/range_tick_label_font_size"
androidPlot.graphWidget.domainLabelPaint.textSize="#dimen/domain_tick_label_font_size"
androidPlot.graphWidget.domainOriginLabelPaint.textSize="#dimen/domain_tick_label_font_size"
/>
And my Fragment code is as follows:
public class TestFragment extends android.support.v4.app.Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View ecgGraphView = inflater.inflate(R.layout.fragment_ecggraph, container, false);
XYPlot dynamicPlot = (XYPlot) ecgGraphView.findViewById(R.id.dynamicXYPlot);
return ecgGraphView;
}
public void onPause() {
super.onPause();
}
public void onDestroy(){
super.onDestroy();
}
#Override
public void onResume() {
super.onResume();
}}
I use the PanoramaGL Library (https://code.google.com/p/panoramagl/) to display spheric 360° Images.
What I want:
The top half of the screen should contain a OSMdroid-Map (https://osmdroid.net/) and the bottom half shows panoramic images with PanoramaGL like this:
Problems:
You create panoramic Views in PanoramaGL using the class PLView which extends Activity.
If you show a panoramic image it always occupies the whole screen, and I see no way to change the layout. As soon as I try to define any xml-Layout, the panoramic image does not show up anymore.
My current code, where the map-part is commented out, so that only the panoramic image is displayed:
package com.example.campusnav;
//lots of imports...
public class MainActivity extends PLView {
private MapView mMapView;
private MapController mMapController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*setContentView(R.layout.activity_main);
mMapView = (MapView) findViewById(R.id.mapview);
mMapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
mMapView.setBuiltInZoomControls(true);
mMapView.setMultiTouchControls(true);
mMapController = (MapController) mMapView.getController();
mMapController.setZoom(13);
GeoPoint startPoint = new GeoPoint(52.432846, 8.369147);
mMapController.setCenter(startPoint);*/
PLSpherical2Panorama panorama = new PLSpherical2Panorama();
panorama.setImage(new PLImage(PLUtils.getBitmap(this, R.raw.p_1), false));
this.setPanorama(panorama);
}
}
Any suggestions how to work around this problem? Is there a way to display two seperate activities arranged in a vertical layout?
yes you can display both Panorama and map together
Extend your activity from PLView
XML
<RelativeLayout
android:id="#+id/rl_streetview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:layout_weight="1"
android:background="#00FF40" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/rl_mapview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/rl_streetview"
android:layout_margin="0dp"
android:layout_weight="1" >
<fragment
android:id="#+id/location_map"
android:layout_width="match_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
Activity
#Override
protected View onContentViewCreated(View contentView) {
ViewGroup mainView = (ViewGroup) this.getLayoutInflater().inflate(R.layout.activity_main, null);
rl_streetview = (RelativeLayout) mainView.findViewById(R.id.rl_streetview);
rl_mapview = (RelativeLayout) mainView.findViewById(R.id.rl_mapview);
}
I'm working on a mapping app. And I want to show some views under the MapView.
I'm using the method:
mapView.setAlpha(0.5f);
Nothing happens to the mapView. When I try to apply this method to my buttons it works fine, even applying it to the parent view of the mapView makes every child view transparent except for the mapView.
How can we make the mapView transparent? I have looked into other questions here and I cant seem to find a solution.
Is there some special method in the mapView.getMap() that we can use to make it transparent?
You can set the transparence for the map using View.setAlpha() (documentation). Here is a working example:
activity_maps.xml:
<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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="THIS TEXT IS VISIBLE"
android:textSize="24sp"
android:gravity="center"/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
MapsActivity.java
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
Marker m = googleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
m.showInfoWindow();
View v = getSupportFragmentManager().findFragmentById(R.id.map).getView();
v.setAlpha(0.5f); // Change this value to set the desired alpha
}
}
The result looks like this (as you see the text is visible because of the map's transparency):
You can do this through the setAlpha(float) method of your MapView reference, like this:
mMapView = (MapView) findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
mMapView.getMapAsync(this);
mMapView.setAlpha(0.5f);
If you are using MapFragment, then just use setAlpha(float) for your MapFragment.
Use a Framelayout where you put your 2 Views (CameraView and MapView) over themselfes.
And use the xml attribute alpha to make the MapView transparent.
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 :)