2013-07-21 2 views
1

У меня есть приложение Android (источник), которое использует карты google api v1. К сожалению, я не могу использовать его, потому что я не могу создать ключ карты для v1 api. И из-за этого карты не отображаются.Android: API Карт Google v1 - v2

Есть ли простой способ изменить исходный код для совместимости с v2 google maps api? Я попробовал этот учебник: http://www.vogella.com/articles/AndroidGoogleMaps/article.html но без большего успеха (я новичок в андроиде развития)

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); 
    } 

} 
+0

Почему вы не следуете документам @ https://developers.google.com/maps/documentation/android/start – Raghunandan

ответ

Смежные вопросы