2011-12-14 61 views
0

我想製作GPS應用程序,並從此代碼開始,這是從另一個教程複製粘貼。但是當我測試應用程序時,空白區域和地圖不會出現!任何人都可以幫忙嗎?Android GPS地圖

下面是代碼

package com.manita.mapuse; 

import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.widget.Toast; 

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 MapuseActivity extends MapActivity implements LocationListener { 
    private MapView mapView = null; 
    private LocationManager lm = null; 
    private double lat = 0; 
    private double lng = 0; 
    private MapController mc = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    mapView = (MapView) this.findViewById(R.id.mapView); 
    mapView.setBuiltInZoomControls(true); 

    lm = (LocationManager) this.getSystemService(LOCATION_SERVICE); 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, this); 
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, this); 

    mc = mapView.getController(); 
    mc.setZoom(12); 
    } 

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

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    { 
    if (keyCode == KeyEvent.KEYCODE_S) 
    { 
    mapView.setSatellite(!mapView.isSatellite()); 
    return true; 
    } 
    return super.onKeyDown(keyCode, event); 
    } 

    @Override 
    public void onLocationChanged(Location location) 
    { 
    lat = location.getLatitude(); 
    lng = location.getLongitude(); 
    Toast.makeText(getBaseContext(), 
    "Location change to : Latitude = " + lat + " Longitude = " + lng, 
    Toast.LENGTH_SHORT).show(); 
    GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6)); 
    mc.animateTo(p); 
    mc.setCenter(p); 
    } 

    @Override 
    public void onProviderDisabled(String provider) 
    { 
    // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String provider) 
    { 
    // TODO Auto-generated method stub 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) 
    { 
    // TODO Auto-generated method stub 

    } 
} 

下面是manifest.xml中

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

    <uses-sdk android:minSdkVersion="7" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <uses-library android:name="com.google.android.maps" /> 
     <activity 
      android:label="@string/app_name" 
      android:name=".MapuseActivity" > 
      <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.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_GPS" /> 
<uses-permission android:name="android.permission.LOCATION" /> 
</manifest> 

,這是我MainActivity.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainlayout" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <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="@string/mapKey" 
/> 
    <LinearLayout android:id="@+id/zoom" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     /> 

</RelativeLayout> 

謝謝。

+2

請檢查您的Google MAP API密鑰。 – Arslan 2011-12-14 11:00:21

+0

我有我的地圖API密鑰,我從這裏[http://code.google.com/intl/fr/android/maps-api-signup.html]! – 2011-12-14 11:19:11

+0

它是從調試鍵生成的關鍵?如果沒有地圖上的模擬器將不會顯示任何數據,但瓦... – Prasham 2011-12-14 11:24:25

回答

0

如果你的應用程序是白色的一個可能的解決方案是爲谷歌地圖創建一個新的API密鑰。我有同樣的確切問題。創建完成後,將其放入清單中,然後您就可以繼續使用