将android google maps v1转换为v2

时间:2013-08-31 15:02:19

标签: android google-maps maps google-maps-android-api-2 google-maps-android-api-1

我开始通知我关于谷歌地图v2 und所以我想在我的实际应用程序中得到它!

在manifest和xml文件中的

我做了一切(这个工作),但我的问题是在我的地图活动中!当我点击我的应用程序应该启动的按钮时,只有一个黑屏,我的项目崩溃:(这里代码:

清单:

   <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="at.android.kioe"
    android:versionCode="1"
    android:versionName="1.1" 
    >

    <uses-sdk android:minSdkVersion="17"
         android:targetSdkVersion="17"/>

    <uses-feature
                android:glEsVersion="0x00020000"
                android:required="true"/>
    <meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my api debug key (v2) " />

    <permission android:name="at.android.kioe.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="at.android.kioe.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />



    <application
        android:icon="@drawable/label_kulinarik"
        android:label="@string/app_name" >

    <uses-library android:name="com.google.android.maps"> </uses-library>


 <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

 <activity
        android:name=".DataBaseHelper"
           android:theme="@android:style/Theme.NoTitleBar"
             />
        <activity android:name=".hashmap"
           android:theme="@android:style/Theme.NoTitleBar"
            />
        <activity android:name=".LocationDemo2"
           android:theme="@android:style/Theme.NoTitleBar"
           />

            />
        <activity android:name=".Notepadv1"
            android:theme="@android:style/Theme.NoTitleBar"
            />
   </application>

</manifest>

main.xml(mapslayout):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="bottom"
    android:orientation="vertical"
    android:padding="6dp" >

    <fragment
        android:name="com.google.android.gms.maps.SupportMapFragment"

        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />


    <FrameLayout
        android:id="@+id/frameLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/frameLayout1"
        android:layout_alignLeft="@+id/frameLayout1"
        android:layout_alignRight="@+id/frameLayout1" >

    </FrameLayout>

    <FrameLayout
        android:id="@+id/frameLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" >

        <LinearLayout
            android:id="@+id/navi"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="bottom|center" >

            <Button
                android:id="@+id/button1"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:layout_marginTop="3dp"
                android:background="@color/black"
                android:drawableTop="@drawable/idee"
                android:onClick="StartseiteKlick"
                android:text="Idee"
                android:textColor="@color/white"
                android:textSize="5pt" />

            <Button
                android:id="@+id/button2"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:layout_marginTop="3dp"
                android:background="@color/black"
                android:drawableTop="@drawable/betriebe"
                android:onClick="BezirkeKlick"
                android:text="Betriebe"
                android:textColor="@color/white"
                android:textSize="5pt" />

            <Button
                android:id="@+id/button3"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:layout_marginTop="3dp"
                android:background="@color/black"
                android:drawableTop="@drawable/maps_open"
                android:onClick="GooglemapsKlick"
                android:text="Karte"
                android:textColor="@color/white"
                android:textSize="5pt" />

            <Button
                android:id="@+id/button4"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:layout_marginTop="3dp"
                android:background="@color/black"
                android:drawableTop="@drawable/suche"
                android:onClick="SucheKlick"
                android:text="Suche"
                android:textColor="@color/white"
                android:textSize="5pt" />
        </LinearLayout>
    </FrameLayout>

</RelativeLayout>

locationdemo2.java:

    package at.android.kioe;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

@SuppressLint("Override")
public class LocationDemo2 extends FragmentActivity {

  private static final String TAG = "**** "+LocationDemo2.class.getSimpleName();

  private MapController mapController;
  MapView mapView;
  GeoPoint geoPoint;
  boolean art;
  int pFeld[];
  int districtID;
  double zielLat,zielLng;  //geographische Breite,Länge als Parameter des Ziels
  private boolean DEBUG = false;







  //Buttons:
  public void StartseiteKlick (View vies) {
    setContentView(R.layout.startseite);
    }
  public void GooglemapsKlick (View vies) {
      setContentView(R.layout.googlemaps);
          } 
  public void SucheKlick (View vies) {
        setContentView(R.layout.suche);
    }
  public void BezirkeKlick (View vies) {
        setContentView(R.layout.bezirke);
  }

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

    Intent in = getIntent();
    art = in.getBooleanExtra("karte", false);
    if (art) { //Karte mit allen Betrieben des Bezirks
      districtID=in.getIntExtra("districtID",0);
      pFeld = in.getIntArrayExtra("pFeld"); //Koordinaten der Betriebe
    } else { //Karte mit dem aus der Liste gewählten Betrieb im Mittelpunkt
      zielLat = in.getFloatExtra("lat",0);
      zielLng = in.getFloatExtra("lng",0);
      geoPoint = new GeoPoint((int)(zielLat*1E6), (int)(zielLng*1E6));
    }
    // Zoom aktivieren

    // MapController ermitteln
    mapController = mapView.getController();
  } //Ende onCreate

  @Override
  protected void onStart() {
    super.onStart();
    if (!art) { //Karte nur für aus der Liste ausgewählten Betrieb
      //fügt der MapView neues Overlay (zum zeichnen der 'current location') hinzu
      List<Overlay> overlays = mapView.getOverlays();
//    overlays.clear();
      overlays.add(new MyOverlay());
    } else //alle Betriebe in Karte zeichnen
      multipleItems();
  }

  private void multipleItems() {
//      mapView.setSatellite(true);

    Drawable markerDefault = this.getResources().getDrawable(R.drawable.mm_20_green);
    MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(markerDefault);

    //für alle Betriebe ein Item aus den Koordinaten bilden und die
    //where-Klausel mit der _id des Betriebes hinzufügen für onTap
    for (int i=0; i<pFeld.length; i+=3) {
      if (pFeld[i+1] > 0)
      itemizedOverlay.addOverlayItem(pFeld[i+1],pFeld[i+2],"_id="+pFeld[i]);
    }
    //alle Items zur MapView hinzufügen
    mapView.getOverlays().add(itemizedOverlay);
    //den Mittelpunkt für den ausgewählten District heraussuchen
    int lat=0,lng=0;
    for (int i=0; i < Notepadv1.mpFeld.length; i+=3) {
      if (Notepadv1.mpFeld[i] == districtID) {
        lat = Notepadv1.mpFeld[i+1];
        lng = Notepadv1.mpFeld[i+2];
        //Darstellung Mittelpunkt
   //       itemizedOverlay.addOverlayItem(lat,lng,"",this.getResources().getDrawable(R.drawable.marker_default));
        mapController.setCenter(new GeoPoint(lat,lng));
        break;
      }
    }
    if (DEBUG) {
      String message="Anzahl Koord.:"+pFeld.length/3+", Mitte:"+lat+","+lng;
      Log.i(TAG,message);
      Toast.makeText(LocationDemo2.this, message, Toast.LENGTH_LONG).show();
    }
    mapController.zoomToSpan(itemizedOverlay.getLatSpanE6(), itemizedOverlay.getLonSpanE6());
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
  }

  protected boolean isRouteDisplayed() {
    return false;
  }

  /* a private inner class which extends Overlay and override the draw method
   * http://androidcookbook.com/Recipe.seam?recipeId=1541
   * zeichnet als 'overlay' den 'geoPoint' in die Karte
   */
  private class MyOverlay extends com.google.android.maps.Overlay {
    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
      super.draw(canvas, mapView, shadow);
      if (!shadow && geoPoint != null) {
          mapController.setCenter(geoPoint);
          Point point = new Point(); //wird Koordinate des GeoPoint
          mapView.getProjection().toPixels(geoPoint, point);
          Bitmap bmp = BitmapFactory.decodeResource(getResources(),//R.drawable.marker_default);
                  R.drawable.mm_20_green);
          // Shift it left so the center of the image is aligned with the x-coordinate of the geo point
          int x = point.x - bmp.getWidth() / 2;
          // Shift it upward so the bottom of the image is aligned with the y-coordinate of the geo point
          int y = point.y - bmp.getHeight();
          canvas.drawBitmap(bmp, x, y, null);
      }
    }
  }

  private class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
    private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    public MyItemizedOverlay(Drawable defaultMarker) {
      super(boundCenterBottom(defaultMarker));
    }
    public void addOverlayItem(int lat, int lon, String title, Drawable altMarker) {
      GeoPoint point = new GeoPoint(lat, lon);
      OverlayItem overlayItem = new OverlayItem(point, title, null);
      addOverlayItem(overlayItem, altMarker);
    }
    public void addOverlayItem(int lat, int lon, String title) {
      GeoPoint point = new GeoPoint(lat, lon);
      OverlayItem overlayItem = new OverlayItem(point, title, null);
      addOverlayItem(overlayItem);
    }
    public void addOverlayItem(OverlayItem overlayItem) {
      mOverlays.add(overlayItem);
      populate();
    }
    public void addOverlayItem(OverlayItem overlayItem, Drawable altMarker) {
      overlayItem.setMarker(boundCenterBottom(altMarker));
      addOverlayItem(overlayItem);
    }
    @Override
    protected OverlayItem createItem(int i) {
      return mOverlays.get(i);
    }
    @Override
    public int size() {
      return mOverlays.size();
    }
    @Override
    protected boolean onTap(int index) {
      if (DEBUG)
        Toast.makeText(LocationDemo2.this,
          getItem(index).getTitle()+", Index:"+index, Toast.LENGTH_LONG).show();
      Intent in = new Intent(getApplicationContext(),Notepadv1.class);
      in.putExtra("art", "where");
      //_id des gewählten Listenpunktes
      in.putExtra("where", getItem(index).getTitle());

      //Info-Daten zum gewählten Betrieb anzeigen
      String infotext = new hashmap().texte.get(index + 1);
      new AlertDialogManager().showAlertDialog(LocationDemo2.this, "info", infotext, in);
      return true;


    }



    public class AlertDialogManager 
    {
        public void showAlertDialog(Context context, String title, String infoText, final Intent in) 
        {
            final AlertDialog alertDialog = new AlertDialog.Builder(context).create();

            // Setting Dialog Title
            alertDialog.setTitle(title);

            // Setting Dialog Message
            alertDialog.setMessage(infoText);

            alertDialog.setButton("Info", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) 
                {
                    startActivity(in);
                }
            });

            alertDialog.setButton2("zurück", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) 
                {
                    alertDialog.cancel();
                }
            });

            // Showing Alert Message
            alertDialog.show();
        }
    }

  }
  public void finish() {}
  public void finishAffinity() {}
}

logcat的:

    08-31 17:05:08.981: E/AndroidRuntime(22481): FATAL EXCEPTION: main
08-31 17:05:08.981: E/AndroidRuntime(22481): java.lang.RuntimeException: Unable to start activity ComponentInfo{at.android.kioe/at.android.kioe.LocationDemo2}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.app.ActivityThread.access$700(ActivityThread.java:152)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.os.Looper.loop(Looper.java:137)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.app.ActivityThread.main(ActivityThread.java:5328)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at java.lang.reflect.Method.invokeNative(Native Method)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at java.lang.reflect.Method.invoke(Method.java:511)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at dalvik.system.NativeStart.main(Native Method)
08-31 17:05:08.981: E/AndroidRuntime(22481): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.app.Activity.setContentView(Activity.java:1928)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at at.android.kioe.LocationDemo2.onCreate(LocationDemo2.java:67)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.app.Activity.performCreate(Activity.java:5250)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-31 17:05:08.981: E/AndroidRuntime(22481):    ... 11 more
08-31 17:05:08.981: E/AndroidRuntime(22481): Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
08-31 17:05:08.981: E/AndroidRuntime(22481):    at maps.ag.bb.a(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at maps.ag.bb.a(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at maps.ag.an.a(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at maps.ag.bi.a(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at maps.ag.bh.a(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at blf.onTransact(SourceFile:107)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.os.Binder.transact(Binder.java:310)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at com.google.android.gms.internal.bh$4.b(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at com.google.android.gms.internal.bh.a(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at com.google.android.gms.internal.bh.onCreateView(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
08-31 17:05:08.981: E/AndroidRuntime(22481):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682)

我希望有人可以帮助我......我已经在寻找好几个小时......

3 个答案:

答案 0 :(得分:0)

如果没有错误日志,很难解决问题 这是一个例子。 Getting Started with Google MapV2 for Android Apps

注意:以上内容来自我自己的博客。

答案 1 :(得分:0)

您的元标记必须是应用标记的子标记。

您需要扩展标准Activity而不是FragmentActivity,因为您的最低分数不是11或更低

您应该使用MapFrament代替SupportMapFragment

从清单

中删除<uses-library android:name="com.google.android.maps"> </uses-library>

确保按照

的所有步骤操作

https://developers.google.com/maps/documentation/android/start

答案 2 :(得分:0)

你没有读过堆栈跟踪吗?它清楚地说:

  

找不到API密钥。检查它是否在AndroidManifest.xml的元素中

您在Manifest文件中使用的API密钥(“我的api调试密钥(v2)”)无效。您必须在API控制台中创建一个并使用该控制台。