2013-06-24 29 views
0

在這段代碼中我有一些問題,在這裏我無法訪問谷歌地圖,方框發生在地圖的地方。任何人都可以幫我解決這個問題。無法訪問Android程序中的谷歌地圖

GPSApp.java類

package com.android.gpsapp;

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.LinearLayout; 
import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 

public class GPSApp extends MapActivity 
{ 
    MapView mapView; 
    MapController mc; 
    GeoPoint p; 

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

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

    LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom); 
    View zoomView = mapView.getZoomControls(); 

    zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(MapView.LayoutParams.WRAP_CONTENT, MapView.LayoutParams.WRAP_CONTENT)); 

    mapView.displayZoomControls(true); 

    mc = mapView.getController(); 

    String coordinates[] = {"26.456435","80.330606"}; 
    double lat = Double.parseDouble(coordinates[0]); 
    double lng = Double.parseDouble(coordinates[1]); 

    p = new GeoPoint(
     (int) (lat * 1E6), 
     (int) (lng * 1E6)); 

    mc.animateTo(p); 
    mc.setZoom(17); 
    mapView.invalidate(); 

    mapView.setSatellite(true); 
    mapView.setStreetView(true); 
    mapView.setTraffic(true); 
} 

    @Override 
    protected boolean isRouteDisplayed() { 
    // TODO Auto-generated method stub 
     return false; 
    } 
} 

的main.xml和API_KEY我已經給

<?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" 
> 

    <LinearLayout android:id="@+id/zoom" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    /> 

    <com.google.android.maps.MapView 
    android:id="@+id/mapView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:enabled="true" 
    android:clickable="true" 
    android:apiKey="API_Key" 
    /> 

</RelativeLayout> 

AndroidManifes.xml

<?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.android.gpsapp" 
    android:versionCode="1" 
    android:versionName="1.0"> 
    <application android:label="@string/app_name" > 

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

    <activity android:name="GPSApp" android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     </activity> 
    </application> 
    <uses-permission android:name="android.permission.INTERNET" /> 
</manifest> 
+0

你在模擬器或設備上運行? –

回答

0

儘量延長FragmentActivity

public class Maps extends FragmentActivity { 
    GoogleMap map; 
    double lat; 
    double lan; 
    boolean flag = false; 

    // private LocationManager lm; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mapptry); 

     map = ((SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map)).getMap(); 
      } 
     } 
+1

OP的代碼適用於Maps V1;您的答案適用於Maps V2。 – CommonsWare

+0

我想擴展FragmentActivity,但它給出錯誤,你可以解釋這一點。 – esha

+0

導入這個**導入android.support.v4.app.FragmentActivity; ** –

0

你可以嘗試移動<uses-permission>是前<application>,因爲Android過去一直很關心這類事情。

如果這沒有幫助,請嘗試在模擬器或設備上運行地圖應用程序。如果它出現同樣的問題,那麼問題在於您有能力從Google下載地圖信息。

如果地圖應用程序有效,而您的API不可用,那麼您的API密鑰不正確,例如嘗試在地圖V1應用上使用Google地圖V2 API密鑰。您的代碼已針對Maps V1進行設置,並且您無法再獲取Google Maps V1 API密鑰。您應考慮切換爲使用Maps V2,因爲Maps V1已正式棄用。

+0

感謝您的回答,但這不起作用 – esha

0

我在這裏可以想到的主要問題是,您正試圖實施Google Maps API V1,而目前的Google Maps API版本爲2.因此,無論您是試圖在Google Maps API V1中使用Google Maps API V2密鑰如果你所描述的問題會發生(沒有地圖,只有正方形與變焦控制)

如果你確實產生了一個API V1鍵(我真的懷疑......),那麼問題可能在某處你在Google API控制檯中定義你的應用程序的方式。

以任何方式建議您轉移到Google API v2,因爲API V1已被棄用。

這裏是一個博客帖子我就如何落實在應用程序谷歌地圖API V2寫道:

Google Maps API V2