Skip to content Skip to sidebar Skip to footer

Google Map In Android,display In Small Size Mapview

I want to display location in map view & map size should be 200X200. I tried loading map in mapview but its not displaying properly after tapping on direction pointer 2 to 3 ti

Solution 1:

I tested the code below and there is no problems with the map to the size 200dp x 200dp

Layout:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#FFFFFF"android:orientation="vertical"><fragmentandroid:id="@+id/map"class="com.google.android.gms.maps.SupportMapFragment"android:layout_width="200dp"android:layout_height="200dp" /></LinearLayout>

Fragment:

publicclassLocationFragmentextendsFragment
{
    GoogleMap googleMap;

    @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        Viewv= inflater.inflate(R.layout.fragment_location, container, false);

        googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        MapsInitializer.initialize(getActivity().getApplicationContext());

        googleMap.addMarker(newMarkerOptions()
                .position(newLatLng(38.7222524, -9.139336599999979))
                .title("MyLocation")
                .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.ic_mobileedge_navpoint)));

        // Move the camera instantly  with a zoom of 15.
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom( newLatLng( 38.7222524, -9.139336599999979), 15));

        // Zoom in, animating the camera.
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(12), 1000, null);

        return v;
    }
}

Solution 2:

Update - 11 August 2020

MapView.getMap() has been replaced with MapView.getMapAsync()


I see that you are using MapView instead of MapFragment. In Google Maps V2, the old MapView class com.google.android.maps.MapView is obsolete. So make sure you use the correct MapView both in your Java code and XML layout which should be

import com.google.android.gms.maps.MapView;

and

<com.google.android.gms.maps.MapView
    android:id="@+id/map_view"
    android:layout_width="your_width"
    android:layout_height="your_height" />

Also make sure you have made all necessary changes in AndroidManifest.xml, which includes defining the API_KEY for Maps V2 as described here.

Apart from this, there is a major thing you have to do if you are using MapView in place of a MapFragment, you have to call the MapView's overridden functions explicitly inside the corresponding overrides of residing Activity just like,

publicclassYourMapActivityextendsActivity {

    MapView mMapView;
    GoogleMap mMap;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /* Your OnCreate with Map Initialization */

        mMapView = (MapView) findViewById(R.id.map_view);
        mMapView.onCreate(Bundle.EMPTY);

        mMap = mMapView.getMap();
        MapsInitializer.initialize(this);
    }

    @OverrideprotectedvoidonResume() {
        mMapView.onResume();
        super.onResume();
    }

    @OverrideprotectedvoidonPause() {
        mMapView.onPause();
        super.onPause();
    }

    @OverrideprotectedvoidonDestroy() {
        mMapView.onDestroy();
        super.onDestroy();
    }

    @OverridepublicvoidonLowMemory() {
        mMapView.onLowMemory();
        super.onLowMemory();
    }
}

This should be enough for loading a custom sized MapView in your Acitivity. You may also use the following functions to make it suit your requirement.

mMapView.setClickable(false);

mMap.getUiSettings().setMyLocationButtonnabled(false);
mMap.getUiSettings().setZoomControlsEnabled(false);
mMap.getUiSettings().setCompassEnabled(false);
mMap.getUiSettings().setZoomGesturesEnabled(false);
mMap.getUiSettings().setScrollGesturesEnabled(false);
mMap.setMyLocationEnabled(true);

Post a Comment for "Google Map In Android,display In Small Size Mapview"