Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

android - How to show entire world and put markers on it?

Background

I want to show the popular map of the entire world, and put markers on it.

The problem

I tried to use Google Maps for it, but it seems (here) it doesn't support zooming out enough to show the entire world.

What I've found

Looking on Wikipedia (here), the center of the earth is on 30°00′N 31°00′E , but this seems to be in Egypt, and it doesn't look like the center of the world map, even on the Wikipedia page itself:

enter image description here

I've also found a similar question here, so I've converted it to Android code:

    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapFragment);
    mapFragment.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(final GoogleMap googleMap) {
            LatLngBounds allowedBounds = new LatLngBounds(
                    new LatLng(-85, 180),    // bottom right corner
                    new LatLng(85, -180)    // top left corner of map
            );
            double k = 5.0f;
            double n = allowedBounds.northeast.latitude - k;
            double e = allowedBounds.northeast.longitude - k;
            double s = allowedBounds.southwest.latitude + k;
            double w = allowedBounds.southwest.longitude + k;
            LatLng neNew = new LatLng(n, e);
            LatLng swNew = new LatLng(s, w);
            LatLngBounds boundsNew = new LatLngBounds(swNew, neNew);
            googleMap.setLatLngBoundsForCameraTarget(boundsNew);
        }
    });

But it does't show entire world at all:

enter image description here

I've also tried to play with the zoom (looking here), but it never zoomed out enough to show as much as continents:

googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(30, 31), 1));

enter image description here

I chose "1", because the docs say that "1" is the value for "world":

The following list shows the approximate level of detail you can expect to see at each zoom level:

1: World

The question

How can I show the entire world on Google Maps ?

Or, is there any good alternative to just show the world map (static image is good too), and be able to put markers on it? Maybe even using a VectorDrawable?

Is there an image I can show, and somehow put markers on it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Its possible to show entire World and put markers on it with MapView (or MapFragment) Lite Mode:

... If you need to show the entire world in the viewport, it may be better to use Lite Mode.

Markers is in "Partly" Supported Features:

You can add a marker and respond to a click event. You can also add custom marker icons. It's not possible to make a marker draggable. Markers on a lite mode map are flat, and they cannot be rotated.

Also, you can use other features like lines and polygon drawing, etc.

So, with MainActivity like:

public class MainActivity extends AppCompatActivity {

    private static final String MAP_VIEW_BUNDLE_KEY = "MapViewBundleKey";

    private MapView mMapView;
    private GoogleMap mGoogleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bundle mapViewBundle = null;
        if (savedInstanceState != null) {
            mapViewBundle = savedInstanceState.getBundle(MAP_VIEW_BUNDLE_KEY);
        }

        mMapView = (MapView) findViewById(R.id.mapview);
        mMapView.onCreate(mapViewBundle);

        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                mGoogleMap = googleMap;
                mGoogleMap.addMarker(new MarkerOptions()
                        .position(new LatLng(31.755983, 35.212879))
                        .title("Jerusalem"));
            }
        });
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        Bundle mapViewBundle = outState.getBundle(MAP_VIEW_BUNDLE_KEY);
        if (mapViewBundle == null) {
            mapViewBundle = new Bundle();
            outState.putBundle(MAP_VIEW_BUNDLE_KEY, mapViewBundle);
        }
    }
}

and activity_main.xml (also its possible to set Lite Mode for MapView programmatically via GoogleMapOptions.liteMode(true)):

<com.google.android.gms.maps.MapView android:id="@+id/mapview"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    map:cameraZoom="1"
    map:mapType="normal"
    map:liteMode="true"
    />

you got something like that:

Google Maps Whole World


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...