How do I set default location and Zoom level for google map api v2?

后端 未结 5 1980
予麋鹿
予麋鹿 2020-12-02 07:13

When my map shows, it always start at a fixed location (near Africa).

Then, I use the following code to center the map to the location I want.

mMap.a         


        
相关标签:
5条回答
  • 2020-12-02 07:24

    I created a handle to SupportMapFragment and set its visibility to View.INVISIBLE using SupportMapFragment.getView().setVisibility(). Then, in the onLocationChanged callback that my class implements, I check if INVISIBLE and set to VISIBLE if true. This gets rid of the jumping you see on load while allowing you to dynamically initialize the starting position. In my case I am centering the camera around the user's location, so onLocationChanged is called immediately because of setMyLocationEnabled(true).

    Your requirements may be different, but either way you just need to set the visibility to INVISIBLE and then find a good place to set VISIBLE after your appropriate data has been loaded.

    0 讨论(0)
  • 2020-12-02 07:26

    you can use this to zoom directly without the animation :

    map.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(xxxx,xxxx) , 14.0f) );
    
    0 讨论(0)
  • 2020-12-02 07:31

    Take a look at the documentation here:

    https://developers.google.com/maps/documentation/android-api/map#configure_initial_state

    The way to do it is slightly different depending on whether you are adding the map via XML or programmatically. If you are using XML, you can do it as follows:

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:map="http://schemas.android.com/apk/res-auto"
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment"
      map:cameraBearing="112.5"
      map:cameraTargetLat="-33.796923"
      map:cameraTargetLng="150.922433"
      map:cameraTilt="30"
      map:cameraZoom="13"/>
    

    If you are doing it programmatically, you can do the following:

    CameraPosition cameraPosition = new CameraPosition.Builder()
        .target(new LatLng(-33, 150))
        .zoom(13)
        .build();
    MapFragment.newInstance(new GoogleMapOptions()
        .camera(camera));
    
    0 讨论(0)
  • 2020-12-02 07:38

    Just in case you want to load Google Map at any initial Location programmatically then you can use this piece of code,

    FragmentManager fm = getFragmentManager(); // getChildFragmentManager inside fragments.
    CameraPosition cp = new CameraPosition.Builder()
                        .target(initialLatLng) // your initial co-ordinates here. like, LatLng initialLatLng
                        .zoom(zoom_level)
                        .build();
    SupportMapFragment mapFragment = SupportMapFragment.newInstance(new GoogleMapOptions().camera(cp));
    fm.beginTransaction().replace(R.id.rl_map, mapFragment).commit();
    

    Add this piece of code for layout

    <RelativeLayout
           android:id="@+id/rl_map"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent" />
    

    This will load GoogleMap at particular Location directly i.e, initialLatLng.

    0 讨论(0)
  • 2020-12-02 07:40

    you can use directy CameraUpdate using static latlong

     LatLong latlong = new LatLong(lat, long);
     CameraUpdate cameraPosition = CameraUpdateFactory.newLatLngZoom(latLong, 15);
                    mGoogleMap.moveCamera(cameraPosition);
                    mGoogleMap.animateCamera(cameraPosition);
    
    0 讨论(0)
提交回复
热议问题