Android:Google Maps API v1到v2

时间:2013-07-21 09:24:31

标签: android api google-maps

我有使用谷歌地图api v1的android应用程序(源代码)。不幸的是我无法使用它,因为我无法为v1 api生成地图密钥。因为地图没有显示。

是否有一种简单的方法可以将源代码更改为与v2 google maps api兼容?我试过这个教程:http://www.vogella.com/articles/AndroidGoogleMaps/article.html但没有取得更大的成功(我是android开发的新手)

public class MapviewGeolocation extends MapActivity implements LocationListener {

    private MapView mapView;
    private MapController mc;
    public static float currentLatitude = Resources.lat;
    public static float currentLongitude = Resources.lon;
    // private MyLocationOverlay myLocation;
    private List<Event> items;

    SharedPreferences sp;
    LocationManager locationManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // STRICTMODE
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);

        sp = this.getPreferences(Context.MODE_PRIVATE);

        items = new ArrayList<Event>();

        final Location myCurrentLocation = this.getLastBestLocation();

        if (myCurrentLocation != null) {
            if (Resources.isByGps) {
                currentLatitude = (float) myCurrentLocation.getLatitude();
                currentLongitude = (float) myCurrentLocation.getLongitude();
            }

        } else {

            currentLatitude = Resources.lat;
            currentLongitude = Resources.lon;

        }

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

        mc = mapView.getController();
        mc.setZoom(13);
        GeoPoint geo = new GeoPoint((int) (currentLatitude * 1e6),
                (int) (currentLongitude * 1e6));
        mc.animateTo(geo);

        locationManager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);

        MyLocationOverlay mylocationOverlay = new MyLocationOverlay(this,
                mapView);
        mylocationOverlay.enableMyLocation();
        mapView.getOverlays().add(mylocationOverlay);

        InitMapTask init_map_task = new InitMapTask();
        init_map_task.execute();
    }

    @Override
    protected void onResume() {
        super.onResume();

        String adres = sp.getString("adres", "");

        if (adres.length() < 1) {
            Resources.isByGps = true;
        } else {
            Resources.isByGps = false;
        }

        if (!Resources.isByGps) {

            currentLatitude = sp.getFloat("lat", Resources.lat);
            currentLongitude = sp.getFloat("lon", Resources.lon);
        } else {
            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 5000, 200, this);
        }

        mapView.refreshDrawableState();
        mapView.invalidate();

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();

        if (Resources.isByGps) {
            locationManager.removeUpdates(this);
        }
    }

    private void addOverlays() {

        items = Resources.events;

    }

    public Drawable getDrawable(int population) {

        Drawable drawable = null;

        if (population < 300)
            drawable = this.getResources().getDrawable(R.drawable.pins_rose);
        else if ((300 <= population) && (500 > population))
            drawable = this.getResources().getDrawable(R.drawable.pins_bleu);
        else if ((500 <= population) && (800 > population))
            drawable = this.getResources().getDrawable(R.drawable.pins_vert);
        else if ((800 <= population) && (1000 > population))
            drawable = this.getResources().getDrawable(R.drawable.pins_jaune);
        else
            drawable = this.getResources().getDrawable(R.drawable.pins_blanc);

        return drawable;
    }

    private void addOverlay(MapItemizedOverlay itemizedOverlay) {
        Event ev = itemizedOverlay.getLocation();
        GeoPoint location = new GeoPoint((int) (ev.getLat() * 1E6),
                (int) (ev.getLon() * 1E6));
        OverlayItem overlayitem = new OverlayItem(location, ev.getTitle(),
                ev.getCity());
        itemizedOverlay.addOverlay(overlayitem);
        mapView.getOverlays().add(itemizedOverlay);
    }

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

    @Override
    public void onLocationChanged(Location location) {

        if (Resources.isTesting) {
            Toast.makeText(
                    getBaseContext(),
                    "Localization changed - current: Latitude = "
                            + currentLatitude + " Longitude = "
                            + currentLongitude, Toast.LENGTH_LONG).show();
        }

        // if (Resources.isByGps) {
        if (location != null) {
            currentLatitude = (float) location.getLatitude();
            currentLongitude = (float) location.getLongitude();
            GeoPoint geo = new GeoPoint((int) (currentLatitude * 1e6),
                    (int) (currentLongitude * 1e6));
            mc.animateTo(geo);
        }
        // }

        mapView.invalidate();
    }

    @Override
    public void onProviderDisabled(String provider) {
        if (Resources.isTesting)
            Toast.makeText(this, "GPS is off...", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onProviderEnabled(String provider) {
        if (Resources.isTesting)
            Toast.makeText(this, "GPS is on...", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        if (Resources.isTesting)
            Toast.makeText(this, "GPS status changed...", Toast.LENGTH_LONG)
                    .show();
    }

    public class InitMapTask extends AsyncTask<Void, Void, Void> {

        private ProgressDialog progress;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progress = new ProgressDialog(MapviewGeolocation.this);
            progress.setMessage("Loading...");
            progress.show();

        }

        @Override
        protected Void doInBackground(Void... params) {
            addOverlays();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            for (int i = 0; i < items.size(); i++) {
                int color = 0;
                Drawable drawable = getDrawable(400);

                MapItemizedOverlay itemizedOverlay = new MapItemizedOverlay(
                        drawable, mapView, MapviewGeolocation.this, color,
                        items.get(i), currentLatitude, currentLongitude);

                addOverlay(itemizedOverlay);
            }

            mapView.invalidate();
            progress.dismiss();

        }

    }

    /**
     * @return the last know best location
     */
    private Location getLastBestLocation() {
        LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location locationGPS = mLocationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        Location locationNet = mLocationManager
                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        long GPSLocationTime = 0;
        if (null != locationGPS) {
            GPSLocationTime = locationGPS.getTime();
        }

        long NetLocationTime = 0;

        if (null != locationNet) {
            NetLocationTime = locationNet.getTime();
        }

        if (0 < GPSLocationTime - NetLocationTime) {
            return locationGPS;
        } else {
            return locationNet;
        }

    }

    // action for bottom menu
    public void actionLista(View v) {
        Intent i = new Intent(this, ListviewActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(i);
    }

    public void actionMapa(View v) {
        Intent i = new Intent(this, MapviewGeolocation.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(i);
    }

    public void actionSettings(View v) {
        Intent i = new Intent(this, Settings.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(i);
    }

}

1 个答案:

答案 0 :(得分:4)

完成以下教程。 http://mobisys.in/blog/tag/zoom-controls-in-map-api-v2/。它对我帮助很大。