2

我在一个基于谷歌地图的项目上工作了超过 6 个月。我使用的是 Google Maps API V1 及其开发人员 API 密钥。当我尝试发布应用程序时,我了解到 Google API V1 已被弃用!当我阅读 Google Maps API V2 时,我发现 MapView、GeoPoint、OverlayItem 通常被替换为 fragment、Marker、MapFragment、Google Map 等。

我的原始 XML 看起来像这样

<?xml version="1.0" encoding="utf-8"?>
 <com.google.android.maps.MapView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:apiKey="**************my old api key**************"/>

现在谷歌不提供这个键,我需要添加一个地图,并在上面显示我的叠加项目。我对这个新 API 并不感到困惑。

我的 Java 文件看起来一模一样

public class PlacesMapActivity extends MapActivity {
    // Nearest places
    PlacesList nearPlaces;

    // Map view
    MapView mapView;

    // Map overlay items
    List<Overlay> mapOverlays;

    AddItemizedOverlay itemizedOverlay;


    GeoPoint geoPoint;
    // Map controllers
    MapController mc;

    Drawable defaultMarker;

    double latitude;
    double longitude;
    OverlayItem overlayitem;

    String p_u_name;
    Place reference;





    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_places);

        // Getting intent data
        Intent i = getIntent();

        reference = (Place) i.getSerializableExtra("place_reference");

        // Users current geo location
        String user_latitude = i.getStringExtra("user_latitude");
        String user_longitude = i.getStringExtra("user_longitude");

        // Nearplaces list
        nearPlaces = (PlacesList) i.getSerializableExtra("near_places");

        mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);

        mapOverlays = mapView.getOverlays();

        // Geopoint to place on map
        geoPoint = new GeoPoint((int) (Double.parseDouble(user_latitude) * 1E6),(int) (Double.parseDouble(user_longitude) * 1E6));

        // Drawable marker icon
        Drawable drawable_user = this.getResources().getDrawable(R.drawable.mark_red);

        itemizedOverlay = new AddItemizedOverlay(drawable_user, this);

        // Map overlay item
        overlayitem = new OverlayItem(geoPoint, "Your Location","That is you!");

        itemizedOverlay.addOverlay(overlayitem);

        mapOverlays.add(itemizedOverlay);
        itemizedOverlay.populateNow();



        Drawable drawable = this.getResources().getDrawable(R.drawable.mark_blue);

            itemizedOverlay = new AddItemizedOverlay(drawable, this);

            mc = mapView.getController();

        // These values are used to get map boundary area
        // The area where you can see all the markers on screen
        int minLat = Integer.MAX_VALUE;
        int minLong = Integer.MAX_VALUE;
        int maxLat = Integer.MIN_VALUE;
        int maxLong = Integer.MIN_VALUE;

        // check for null in case it is null
        if (nearPlaces.results != null) {



            // loop through all the places
            for (Place place : nearPlaces.results) {

                latitude = place.geometry.location.lat; // latitude
                longitude = place.geometry.location.lng; // longitude

                // Geopoint to place on map
                geoPoint = new GeoPoint((int) (latitude * 1E6),
                        (int) (longitude * 1E6));

                // Map overlay item
                overlayitem = new OverlayItem(geoPoint,"0",place.reference);

                itemizedOverlay.addOverlay(overlayitem);

                // calculating map boundary area
                minLat  = (int) Math.min( geoPoint.getLatitudeE6(), minLat );
                minLong = (int) Math.min( geoPoint.getLongitudeE6(), minLong);
                maxLat  = (int) Math.max( geoPoint.getLatitudeE6(), maxLat );
                maxLong = (int) Math.max( geoPoint.getLongitudeE6(), maxLong );
            }
            mapOverlays.add(itemizedOverlay);

            // showing all overlay items
            itemizedOverlay.populateNow();
        }


        // Adjusting the zoom level so that you can see all the markers on map
        mapView.getController().zoomToSpan(Math.abs( minLat - maxLat ), Math.abs( minLong - maxLong ));
        MapController controller = mapView.getController();
        controller.setZoom(15);

        // Showing the center of the map
        mc.animateTo(new GeoPoint((maxLat + minLat)/2, (maxLong + minLong)/2 ));
        mapView.postInvalidate();

    }



    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

}

我想知道是否有任何方法可以使用我的旧代码来支持 Google Maps API V2?

4

1 回答 1

3

迁移到 v2 的最简单方法是删除您现在拥有的所有代码并从头开始编写。

Android API v1 和 v2 不映射 1 到 1。

谢谢你

于 2013-05-15T07:37:56.817 回答