How to set fixed image over google maps on android - android

I am new to android
I have to add three images calendar, cytilife, and sun images and they should be fixed that is if map is re sized, zoomed, etc at any point of time they should not changed their position.
I am able to display map and the circle. To display images i used ovelay/markers but they are changing if map change.
This is my activity class
public class MainActivity extends FragmentActivity {
private GoogleMap map;
private int offset;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
if (map != null) {
MarkerOptions options = new MarkerOptions();
options.position(getCoords(17.3700, 88.4800)).anchor(0.5f, 0.5f);
options.icon(BitmapDescriptorFactory.fromBitmap(getBitmap(17.3700,
78.4800)));
final Marker marker = map.addMarker(options);
UiSettings uiSettings = map.getUiSettings();
uiSettings.setScrollGesturesEnabled(true);
LatLng latLng = new LatLng(-33.796923, 150.922433);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.addMarker(new MarkerOptions()
.position(latLng)
.title("My Spot")
.snippet("This is my spot!")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.calendar)));
map.getUiSettings().setZoomControlsEnabled(true);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
map.setOnCameraChangeListener(new OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition paramCameraPosition) {
LatLng centerOfMap = map.getCameraPosition().target;
marker.setPosition(centerOfMap);
}
});
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private Bitmap getBitmap(double latitude, double longitude) {
// fill color
Paint paint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
paint1.setColor(0x110000FF);
paint1.setStyle(Style.FILL);
// stroke color
Paint paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
paint2.setColor(Color.GRAY);
paint2.setStyle(Style.STROKE);
// circle radius - 200 meters
int radius = 400;
// create empty bitmap
Bitmap b = Bitmap
.createBitmap(radius * 2, radius * 2, Config.ARGB_8888);
Canvas c = new Canvas(b);
c.drawCircle(radius, radius, radius, paint1);
c.drawCircle(radius, radius, radius, paint2);
return b;
}
private LatLng getCoords(double lat, double lng) {
LatLng latLng = new LatLng(lat, lng);
Projection proj = map.getProjection();
Point p = proj.toScreenLocation(latLng);
p.set(p.x, p.y + offset);
return proj.fromScreenLocation(p);
}
}
This is my activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.naveen.myfirstapp.MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginBottom="86dp"
class="com.google.android.gms.maps.MapFragment"
/>
Thank you

You can use FrameLayout in your XML Layout file to place an image on top of your Map fragment like the following, XML Layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.naveen.myfirstapp.MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginBottom="86dp"
class="com.google.android.gms.maps.MapFragment"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true|left"
android:src="#drawable/calendar_icon
/>
</FrameLayout>

Try this code In this code a image is add over the map fragment and this is showing in the top of map.
<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=".MapsActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>

This is because you are extending a FragmentActivity:
public class MainActivity extends FragmentActivity {...
If you don't have to do so, try extending AppCompatActivity:
public class MainActivity extends AppCompatActivity {...

Related

Mapbox 'setMyLocationEnabled' doesn't work

I'm trying to enable a location in my mapbox map.
However, the line mapboxMap.setMyLocationEnabled(true); fail and Android Studio gives the 'Cannot resolve method setMyLocationEnabled(boolean).
I'm using MapBox version 6.0.1 (the latest) in my Gradle:
//
compile ('com.mapbox.mapboxsdk:mapbox-android-sdk:6.0.1#aar') { // Map BOX
transitive=true
}
When I switch to version 5.1.3 (which I had used in a previous app I made), Android studio detects the method, but on runtime the app throws the following exception:
android.view.InflateException: Binary XML file line #0: Error inflating class com.mapbox.mapboxsdk.maps.MapView
Would love some help on this one.
Relevant code is below, thanks!
This is the relevant code in the activity:
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(MapboxMap mapboxMap) {
// Set map style
mapboxMap.setStyleUrl(Style.MAPBOX_STREETS);
// Set the camera's starting position
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(32.1622,34.8338)) // set the camera's center position on the first coordinate of the tour
.zoom(14) // set the camera's zoom level
.tilt(0) // set the camera's tilt
.build();
// Move the camera to that position
mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
// Set user location
mapboxMap.setMyLocationEnabled(true);
//Set MyLocation Button
//userLocationFAB(DrivingActivity.get, mapboxMap);
// Add route line
//mapboxMap.addPolyline(getRoute());
// // Add markers
// ArrayList<MarkerOptions> markers = getMarkers();
//
// for (int i=0 ; i<markers.size() ; i++) {
// mapboxMap.addMarker(markers.get(i));
// }
}
});
}
And this is the code from the XML file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="36dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score:"
android:layout_centerInParent="true"
android:id="#+id/driving_window_txt"
android:textStyle="bold"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0000000"
android:layout_below="#id/driving_window_txt"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:id="#+id/score_counter"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Distance: 0"
android:layout_below="#id/score_counter"
android:layout_centerHorizontal="true"
android:id="#+id/total_distance"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mapbox.mapboxsdk.maps.MapView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/map"/>
<!-- Implementation of find my location button -->
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:src="#android:drawable/ic_menu_mylocation"
app:borderWidth="0dp"
app:elevation="6dp"
app:pressedTranslationZ="12dp"
app:fabSize="normal"
android:id="#+id/fab_location"/>
</RelativeLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
#Hey mickey check this , how to get current location:
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(MapboxMap mapboxMap) {
mapboxmap = mapboxMap;
mapboxMap.setOnMapClickListener(DetailDirectionFragment.this);
mapboxMap.getUiSettings().setZoomControlsEnabled(true);
initLocationEngine();
}
});
#SuppressWarnings( {"MissingPermission"})
private void initLocationEngine() {
locationEngine = new LocationEngineProvider(activity).obtainBestLocationEngineAvailable();
locationEngine.setPriority(HIGH_ACCURACY);
locationEngine.setInterval(0);
locationEngine.setFastestInterval(1000);
locationEngine.addLocationEngineListener(this);
locationEngine.activate();
if (locationEngine.getLastLocation() != null) {
Location lastLocation = locationEngine.getLastLocation();
onLocationChanged(lastLocation);
currentLocation = Point.fromLngLat(lastLocation.getLongitude(), lastLocation.getLatitude());
updateLocation(lastLocation.getLongitude(), lastLocation.getLatitude());
}
}
#Override
public void onLocationChanged(Location location) {
currentLocation = Point.fromLngLat(location.getLongitude(), location.getLatitude());
Log.e("currentlocation",location.getLatitude()+"lat"+location.getLongitude()+"lng");
onLocationFound(location);
}
#SuppressWarnings( {"MissingPermission"})
#Override
public void onConnected() {
locationEngine.requestLocationUpdates();
}
private void onLocationFound(Location location) {
if (!locationFound) {
animateCamera(new LatLng(location.getLatitude(), location.getLongitude()));
MarkerViewOptions markerViewOptions = new MarkerViewOptions()
.position(new LatLng(location.getLatitude(), location.getLongitude()));
currentMarker = mapboxmap.addMarker(markerViewOptions);
locationFound = true;
}
}
private void animateCamera(LatLng point) {
mapboxmap.animateCamera(CameraUpdateFactory.newLatLngZoom(point, DEFAULT_CAMERA_ZOOM), CAMERA_ANIMATION_DURATION);
}
check this link.

How to draw a line between static view's center points to google map marker?

I have a static Linear layout and a google map in my xml file. I want to draw a straight line between center bottom of linear layout to the marker position.I have tried using polylines in the maps but it is also drawing between markers in the maps.
Below is my xml file -
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/rel_m1"
android:layout_above="#+id/bottom_ll"
android:id="#+id/frameLayout">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
tools:context="com.business.introslider.MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:orientation="vertical"
android:id="#+id/pickup_ll"
android:onClick="OpenPickup"
android:elevation="30dp"
android:background="#ffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pickup from"
android:layout_marginTop="5dp"
android:textSize="14sp"
android:textColor="#555555"
android:padding="5dp"
android:drawableLeft="#drawable/s_green"
android:drawablePadding="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pin location"
android:layout_marginTop="5dp"
android:textSize="16sp"
android:textColor="#000000"
android:padding="5dp"
android:id="#+id/text_pickup_location"
android:layout_marginBottom="5dp"/>
</LinearLayout>
</FrameLayout>
and also my java code is as below -
public void setCurrentLocation(){
try {
//to show my current location in the map
mMap.setMyLocationEnabled(true);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
Toast.makeText(getApplicationContext(), latLng.toString(), Toast.LENGTH_LONG).show();
}
});
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//to request location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this);
ViewTreeObserver vto = pickup_ll.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
pickup_ll.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
pickup_ll.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
float x = pickup_ll.getX() + pickup_ll.getMeasuredWidth() / 2;
float y = pickup_ll.getY() + pickup_ll.getMeasuredHeight() / 2;
// float x= getWindowManager().getDefaultDisplay().getWidth()/2;
point1 = new Point(Math.round(x),Math.round(y));
Log.e(TAG, "point1===" + point1);
LatLng latLng1 = mMap.getProjection().fromScreenLocation(point1);
Log.e(TAG,"latLng1==="+latLng1);
points.add(latLng1);
}
});
#Override
public void onLocationChanged(Location location) {
Log.e(TAG, "onlocation changed");
//To clear map data
mMap.clear();
//To hold location
if(latLng !=null) {
Log.e(TAG, "latLng onlocation changed==" + latLng);
}
else {
latLng = new LatLng(location.getLatitude(), location.getLongitude());
}
//to create a marker in the app
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("My Location");
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.color(Color.BLACK);
polylineOptions.width(2);
points.add(latLng);
polylineOptions.addAll(points);
mMap.addPolyline(polylineOptions);
mMap.addMarker(markerOptions);
//opening position with some zoom level in the app
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.0f));
}
I am calling this setCurrentLocation() in onMapReady method of googleApiClient. Any help would be appreciated.
Have you trying to do drawing path in this way?
https://github.com/hiepxuan2008/GoogleMapDirectionSimple/
this will let you to draw a path between tow places. Make sure you change the API key.

Zoom not working in a map inside scrollview in android

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

Blank screen only when zoom in mapView

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.

Multiple maps (with swiping) in a MapActivity in android

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.

Categories

Resources