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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical">

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="200dp"
        android:layout_height="200dp" />

</LinearLayout>

Fragment:

public class LocationFragment extends Fragment
{
    GoogleMap googleMap;

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

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

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

        googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(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( new LatLng( 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,

public class YourMapActivity extends Activity {

    MapView mMapView;
    GoogleMap mMap;

    @Override
    protected void onCreate(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);
    }

    @Override
    protected void onResume() {
        mMapView.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        mMapView.onDestroy();
        super.onDestroy();
    }

    @Override
    public void onLowMemory() {
        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"