Android Maps API version 1 to version 2 migrations

只谈情不闲聊 提交于 2019-12-06 23:02:54

问题


Background:

I've inherited someone else's problem and am trying to get an app up and running. Multiple developers have ditched the project and now its my turn. I'm an iOS developer by trade but I have a few simple android project completed.

The problem app is a native android originally built with API level 7 and is heavily built around google maps api version 1. I don't have access to the original keys so I appear to be forced into upgrading to google maps version 2.

I have already set up a new Activity and am beyond the basic tutorials of getting a functioning map. No problems there, but I'm blocked on items like GeoPoint and don't seem to be able to find much information on its replacement.

My question is this:

Is there a migration path to move from API version 1 to API version 2 or do I have to re-code the whole map component? GeoPoints, custom pins, and overlays included?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/16431464/android-maps-api-version-1-to-version-2-migrations

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