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.
Related
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
Question
Why does my custom infowindow not 'inflate' after a configuration change?
Summary
I have an app that places a marker on a map using MarkerManager, and sets a custom info-window using setInfoWindowAdapter.
After loading the app and clicking on the marker, the info-window correctly displays my custom layout.
However, after rotating the app so that the configuration changes, selecting the marker no longer displays the custom info-window, and instead only displays the generic 'title'.
What have I missed in all the hours of reading and tutorials that means I'm not correctly implementing this?
Code
Here is a stripped-down version of my app to replicate the issue
MapsActivity
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
MarkerManager.Collection mMarkerManagerCollection;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
if(savedInstanceState == null){
mapFragment.setRetainInstance(true); // maintains the map fragment on orientation changes
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
MarkerManager mMarkerManager = new MarkerManager(mMap);
mMap.setInfoWindowAdapter(mMarkerManager);
mMarkerManagerCollection = mMarkerManager.newCollection("myMarker");
mMarkerManagerCollection.setOnInfoWindowAdapter(new GoogleMap.InfoWindowAdapter(){
#Override
public View getInfoWindow(Marker marker) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
View v = getLayoutInflater().inflate(R.layout.info_window, null);
TextView tv = (TextView) v.findViewById(R.id.myTv);
tv.setText("hello world, this is my marker");
return v;
}
});
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
MarkerOptions options = new MarkerOptions()
.position(sydney)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
.flat(true)
.title("Marker in Sydney");
Marker marker = mMarkerManagerCollection.addMarker(options);
marker.setTag("test marker");
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
info_window.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="93dp"
android:layout_height="96dp"
android:id="#+id/rect_load"
android:padding="2dp"
android:src="#mipmap/ic_launcher"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/myTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="myText"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Gradle
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.google.android.gms:play-services:9.8.0'
compile 'com.google.maps.android:android-maps-utils:0.4'
compile 'com.google.android.gms:play-services:9.8.0'
The problem is that you are creating and populating a new MarkerManager on each onMapReady call. As onMapReady gets called when the orientation changes but you are not removing old markers, the map has two markers on the same position.
When you click the marker after the orientation changes, the old marker is triggered and you see the default behaviour (show title). If you click the marker again you will see your info window.
One possible solution can be calling mMap.clear(); on the onMapReady before adding markers.
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....
How can I change google map my location default button?
I set my location enable and map draw standard image to find location, is it possible to change default image?
See below xml file to custom button:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/maps"
android:name="pl.mg6.android.maps.extensions.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp" >
<ImageView
android:id="#+id/imgMyLocation"
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="fitXY"
android:src="#drawable/track_my_location" />
</LinearLayout>
</RelativeLayout>
Then in java class, declare your location button:
private ImageView imgMyLocation;
imgMyLocation = (ImageView) findViewById(R.id.imgMyLocation);
Click Event:
imgMyLocation.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
getMyLocation();
}
Get Location Method, in that just pass your current latitude and longitude.
private void getMyLocation() {
LatLng latLng = new LatLng(Double.parseDouble(getLatitude()), Double.parseDouble(getLongitude()));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 18);
googleMap.animateCamera(cameraUpdate);
}
});
With a simple trick, you can replace the my location button with a custom one.
Customize the Layout of your new button.
Set my location enabled in maps api.
Hide default button of my location.
call click method of my location on your custom button click.
1. Customize the layout of your new button
add a new button to your desired location in layout file of map activity.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent">
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MapsActivity" />
<ImageView
android:id="#+id/ic_location"
android:layout_width="18dp"
android:layout_height="18dp"
android:src="#drawable/ic_location"
android:layout_marginRight="8dp"
android:layout_marginBottom="18dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
2. Set my location enabled in maps api
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Enable My Location
mMap.setMyLocationEnabled(true);
/* and DON'T disable the default location button*/
}
3. Hide default button of my location.
//Create field for map button.
private View locationButton;
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
// get your maps fragment
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Extract My Location View from maps fragment
locationButton = mapFragment.getView().findViewById(0x2);
// Change the visibility of my location button
if(locationButton != null)
locationButton.setVisibility(View.GONE);
}
4. call click method of my location on your custom button click.
findViewById(R.id.ic_location).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mMap != null)
{
if(locationButton != null)
locationButton.callOnClick();
}
}
});
If you want to retain the smoothness of the default My Location button but not the look, call the following lines:
//Make sure you have explicit permission/catch SecurityException
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
Now you can start & stop location updates while keeping the default behavior
This might be useful to someone who is looking for changing the location icon directly, instead of using any trick or workaround:
locationButton = (mapView.findViewById<View>(Integer.parseInt("1")).parent as View)
.findViewById<ImageView>(Integer.parseInt("2"))
locationButton.setImageResource(R.drawable.ic_location_current)`
Here findViewById<ImageView> is the right thing to be done.
I get the default ImageView of my location using tag instead of idfor preventingExpected resource of type id` lint warning -
ImageView imageView = ((ImageView)mapFragment.getView().findViewWithTag("GoogleMapMyLocationButton"));
Then you can manipulate the imageView then.
imageView.setImageResource(R.drawable.icon_custom_location);
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