Android Maps API version 1 to version 2 migrations

我只是一个虾纸丫 提交于 2019-12-05 03:27:34
Emil Adz

The sad answer is that most of the objects of Google Map API V1 are replaced with new Objects in Google Map API V2.

For example instead of using GeoPoints you are going to use Latlng points. Overlays are gone as well and replaced with Polylines and Polygones.

Take a look at this answer I gave here:

Convert Android App that uses maps API V1 to Maps Android API V2

And have a look at this blog post I wrote that will get you started with Google Map API V2 implementation in your app:

Google Map API V2

So as you can understand most of the code should be rewritten to fit Google Map API V2.

When moving to v2 of the Maps API for Android, you’ll immediately notice that this is a completely new API with very little regard to backwards compatibility .

You’ll see a lot of compile errors as many of the existing classes will now be missing like MapActivity, GeoPoint, RecticleDrawMode and Overlay.

The new objects are all included in the com.google.android.gms.maps.* package.

  • Users location

Android Maps v1

In Maps v1, the MyLocationOverlay was used to detect the users location

List overlaysoverlays = mapView.getOverlays();
MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
myLocationOverlay.enableMyLocation();
overlays.add(myLocationOverlay);

Android Maps v2

You can activate show a My Location button on the map very easily like this:

googleMap.setMyLocationEnabled(true);

The My Location button appears in the top right corner of the screen only when the My Location layer is enabled. When a user clicks the button, the camera animates to focus on the user’s current location if the user’s location is currently known.

  • Highlighting markers

Highlighting markers has become very simple. Changing the marker icon has become a one-liner.

Android Maps v1

private void highlightMarker(int index) {
 Drawable d = getResources().getDrawable(R.drawable.pin_green);
 Rect copyBounds = overlay.getMarker(0).copyBounds();
 d.setBounds(copyBounds);
 overlay.setMarker(d);
 SitesOverlay sitesOverlay =  (SitesOverlay) map.getOverlays().get(0);
 sitesOverlay.refresh();
}

Android Maps v2

private void highLightMarker(int index) {
 Marker marker = markers.get(index))
 marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
}

For more steps check this tutorial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!