Android中的谷歌地图,以小尺寸mapview显示

时间:2014-04-11 10:29:23

标签: android google-maps google-maps-api-3 android-mapview

我想在地图视图中显示位置&地图大小应为200X200。 我尝试在mapview中加载地图但是在点击方向指针2到3次显示正确的地图但没有移动之后它没有正确显示。

map

但是,当我使用完整尺寸的屏幕显示地图而不更改任何代码时,它正确加载。如何在小视图中显示相同的内容?

地图代码 -

static GoogleMap map;
private MapView mapView;
MapsInitializer.initialize(getApplicationContext());

        switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()) )
        {
        case ConnectionResult.SUCCESS:
            //Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show();
            mapView = (MapView)findViewById(R.id.map);
            mapView.onCreate(savedInstanceState);
            // Gets to GoogleMap from the MapView and does initialization stuff
            if(mapView!=null)
            {
                map = mapView.getMap();
                map.getUiSettings().setMyLocationButtonEnabled(false);
                map.setMyLocationEnabled(true);
                CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(location, 10);
                map.animateCamera(cameraUpdate);
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            }
            break;
        case ConnectionResult.SERVICE_MISSING: 
            //Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
            break;
        case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED: 
            //Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
            break;
        default: Toast.makeText(getApplicationContext(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()), Toast.LENGTH_SHORT).show();
        }

        CameraPosition cameraPosition = new CameraPosition.Builder()
        .target(location).zoom(10).bearing(0) // Sets the orientation of the camera to east
        .tilt(70)    // Sets the tilt of the camera to 30 degrees
        .build();    // Creates a CameraPosition from the builder
        map.setBuildingsEnabled(true);
        map.setMyLocationEnabled(true);
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

解决方案 - 使用片段完成 -

map_screen.xml

<?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/mapfragment"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:layout_alignParentTop="true"
        android:layout_gravity="center_horizontal"
        class="com.google.android.gms.maps.SupportMapFragment" />

    <TextView
        android:id="@+id/reg_office_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mapfragment"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:text="@string/tata_auto"
        android:textColor="@color/tatablue"
        android:textSize="18sp" />

</RelativeLayout>

java代码 -

public class MapFragment extends Fragment {

    private View view;

    public  GoogleMap map; 
    private Location location;
    private LatLng mAddress1;
    public static boolean inMap=false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        view= inflater.inflate(R.layout.contact_map, container, false);
        setMapView();
        return view;
    }

    private void setMapView(){
        CurrentLocation mCurrentLoc=new CurrentLocation(getActivity());
        location = CurrentLocation.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) { 
            mCurrentLoc.onLocationChanged(location);
        } else {
            mCurrentLoc.onLocationChanged(location);
        }

        //class="com.google.android.gms.maps.SupportMapFragment"

        map = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.mapfragment)).getMap();


        CameraPosition cameraPosition = new CameraPosition.Builder()
        .target(mAddress1).zoom(8).bearing(0) // Sets the orientation of the camera to east
        .tilt(30)    // Sets the tilt of the camera to 30 degrees
        .build();    // Creates a CameraPosition from the builder
        map.setBuildingsEnabled(true);
        map.setMyLocationEnabled(true);
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));


    }
}

mAddress1是您所在位置的纬度和经度。

2 个答案:

答案 0 :(得分:6)

我测试了下面的代码,地图尺寸为200dp x 200dp没有问题

布局:

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

片段:

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;
    }
}

答案 1 :(得分:4)

我看到你使用的是MapView而不是MapFragment。在Google Maps V2中,旧的MapView类com.google.android.maps.MapView已过时。因此,请确保在Java代码和XML布局中使用正确的MapView,这应该是

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

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

另外,请确保您已在AndroidManifest.xml中进行了所有必要的更改,其中包括为Maps V2 as described here定义API_KEY。

除此之外,如果使用MapView代替MapFragment,还有一件事要做,你必须在驻留Activity的相应覆盖内明确调用MapView的重写函数,就像这样,

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();
    }
}

这足以在您的Acitivity中加载自定义大小的MapView。您还可以使用以下功能使其符合您的要求。

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);