I have a listview of addresses in screen#1. On click of any item navigating to next screen which shows the map of clicked address. Now i want to swipe the screen to view the map of next address. Here is my code..
This is my map_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayoutTopBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/scarlet"
android:gravity="center_vertical|center_horizontal" >
<TextView
android:id="#+id/textViewTopBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="Map"
android:textColor="#color/white"
android:textSize="20dp" />
</LinearLayout>
<com.xxx.android.yyyy.activities.HorizontalPager
android:id="#+id/horizontal_pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</com.xxx.android.yyyy.activities.HorizontalPager>
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:background="#null"
android:scaleType="fitCenter"
android:src="#drawable/button123" />
This is my map.xml
<?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" >
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:apiKey="xxxxxxxxxxxxxxxxxxxxx"
android:clickable="true" />
</LinearLayout>
Activity :
public class MyMapActivity extends MapActivity
{
private MapView mapView;
private Drawable mapMarker;
private MyLocationOverlay myMap;
private MapController mapController;
private List<Overlay> overlayList;
private HorizontalPager hPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_screen);
hPager = (HorizontalPager) findViewById(R.id.horizontal_pager);
View inflatedView;
for (int i = 0; i < listViewSize; i++) //// getting listviewSize from intent extras
{
inflatedView = (View) View.inflate(MyMapActivity.this, R.layout.map, null);
mapView = (MapView) inflatedView.findViewById(R.id.mapview);
showMap(latt, longt); //getting all the latt, longt values of addresses from previous activity
hPager.addView(inflatedView);
}
hPager.setCurrentScreen(selectedAddressIndexInListView, false); //<<---- intent extras
}
void showMap(double latitude , double longitude )
{
mapView.setBuiltInZoomControls(true);
mapMarker = getResources().getDrawable(R.drawable.map_marker);
overlayList = mapView.getOverlays();
myMap = new MyLocationOverlay(this, mapView);
myMap.enableMyLocation();
mapController = mapView.getController();
GeoPoint geoPoint = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
mapController.animateTo(geoPoint);
mapController.setZoom(12);
OverlayItem overlayItem = new OverlayItem(geoPoint,"xxx", "yyy");
CustomPinpoint customPinpoint = new CustomPinpoint(mapMarker,MyMapActivity.this);
customPinpoint.insertPinpoint(overlayItem);
overlayList.clear();
overlayList.add(myMap);
overlayList.add(customPinpoint);
}
#Override
protected void onResume() {
super.onResume();
myMap.enableCompass();
myMap.enableMyLocation();
}
#Override
protected void onPause() {
super.onPause();
myMap.disableCompass();
myMap.disableMyLocation();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
{ Reference : https://github.com/ysamlan/horizontalpager/tree/master/src/com/github/ysamlan/horizontalpager }
App is immediately force closing on click of any listview item..
Caused by: java.lang.IllegalStateException: You are only allowed to have a single MapView in a MapActivity
Where iam going wrong? Everything works fine in following two cases.. 1. if i iterate the for loop only one time. 2. if i remove map view related lines from xml, activity files.
How can i have multiple maps in a single MapActivity with swiping?. Please help me
You can have only one Mapview per map activity, however, you can have multiple map activities in one application, which in your case wouldnt be a very ideal solution.
What you should do instead of having a totally new mapview for an address, you could have a different set of overlays for them. This way you'll only need to change the set of overlays when you want to view map details for another address. Besides being possible to make this with Android, its much more optimal than having multiple mapviews.
So far I know Currently Android supports only one MapView per MapActivity.
Related
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);
}
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....
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 the osmdroid map in a sample application. The map is seen,zoom works,and everything is fine except that i'm unable to pan/move the map.
My main xml code is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<org.osmdroid.views.MapView
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
</org.osmdroid.views.MapView>
</RelativeLayout>
My java code is:
public class MapTest extends Activity {
private MapView mapView;
private MapController mapController;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_test);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(false);
mapView.setMultiTouchControls(true);
mapController = this.mapView.getController();
mapController.setZoom(10);
GeoPoint mapCenter = new GeoPoint(17.4467, 78.4667);
mapController.setCenter(mapCenter);
}
}
Any ideas as to how to enable the movement?
Try removing:
android:clickable="true"
from your MapView. That makes the MapView consume touch events and it messes with the dispatchTouchEvent method. You might also try removing the focusable stuff too.
mapView.setClickable(false);
It worked for me.
You should give it a shot.
Just install previous version for now, looks like there's a bug in 3.0.9.
Try to use
mapView.setClickable(false);
So i have an app that has to show the location of a number of shops, depending on which one you choose.
The XML layout for the page with the mapView is part of a ViewFlipper, and the part that has the map layout is:
<RelativeLayout
android:id="#+id/RelativeLayout3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/back2"
android:gravity="left" >
<ImageButton
android:id="#+id/backLocation3"
android:layout_width="72dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:src="#drawable/btn_zuruck2" />
<ImageView
android:id="#+id/imageViewMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/backLocation3"
android:layout_marginTop="14dp"
android:src="#drawable/businesslounge" />
<RelativeLayout
android:id="#+id/mapPlaceHolderLayout"
android:layout_width="300dp"
android:layout_height="200dip"
android:layout_alignLeft="#+id/backLocation3"
android:layout_below="#+id/imageViewMap"
android:layout_marginTop="92dp" >
</RelativeLayout>
<TextView
android:id="#+id/mapLocation"
android:layout_width="240dp"
android:layout_height="40dp"
android:layout_alignLeft="#+id/mapPlaceHolderLayout"
android:layout_below="#+id/imageViewMap"
android:layout_marginTop="47dp"
android:text="#string/maplocation"
android:textSize="25dp"
android:textColor="#color/purple" />
<ImageButton
android:id="#+id/phonereserve"
android:layout_width="220dp"
android:layout_height="36dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="24dp"
android:src="#drawable/rezervephone" />
</RelativeLayout>
The part of the code that relates to the mapview is:
public class StandorteFragment extends MapHostFragment {
public String text2;
private RelativeLayout mapPlaceHolderLayout;
public AQuery aQuery;
private MapView myMapView;
private GeoPoint geoPoint;
private MapController mc;
#Override
protected Class<? extends Activity> getActivityClass() {
return LocationMapActivity.class;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.location, container, false);
ViewFlipperStandorte = (ViewFlipper) v.findViewById(R.id.viewFlipperLocation);
geoPoint = new GeoPoint( (int) (48.949997* 1E6), (int) (22.140213 * 1E6));
mapPlaceHolderLayout = (RelativeLayout)v.findViewById(R.id.mapPlaceHolderLayout);
mapPlaceHolderLayout.addView(onCreateMapView());
// myMapView = (MapView) v.findViewById(R.id.mapPlaceHolderLayout);
// mc = myMapView.getController();
// mc.animateTo(geoPoint);
//
It works but doesn't show nothing in the mapView. If i don't comment those 3 lines with myMapView and animate geoPoint, it force closes.
What do I have to do?
EDIT: onCreateMapView:
public View onCreateMapView() {
Intent intent = new Intent(getActivity(), getActivityClass());
final Window w = getLocalActivityManager().startActivity(ACTIVITY_TAG,
intent);
final View wd = w != null ? w.getDecorView() : null;
if (wd != null) {
ViewParent parent = wd.getParent();
if (parent != null) {
ViewGroup v = (ViewGroup) parent;
v.removeView(wd);
}
wd.setVisibility(View.VISIBLE);
wd.setFocusableInTouchMode(true);
if (wd instanceof ViewGroup) {
((ViewGroup) wd)
.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
}
}
return wd;
}
Well the R.id.mapPLaceHolderLayout in your xml is not a mapview so you can't cast it to one. That line will cause an exception.
mapPlaceHolderLayout.addView(onCreateMapView()); called MapHostFragment which called LocalActivityManagerFragment which somehow goes to LocationMapActivity which extends MapActivity, so here i could instantiate the Mapcontroller and everything went as normal form there