2014-03-28 74 views
0

我正在創建一個應用程序,我必須獲取用戶的經度和緯度並標記他的位置。我的應用出現錯誤「不幸地圖已停止」。我GOOGLE了很多,但無法找到解決方案。這裏是我的代碼和我的日誌貓。如何獲取Android應用程序的經度緯度

public class MapView extends Activity { 

    private LocationManager locationManager; 
    private String best; 
    double myLocationLatitude; 
    double myLocationLongitude; 

    Location loc = locationManager.getLastKnownLocation(best); 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.map_layout); 

     locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

     // additionally (you have to implement LocationListener) 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0.0f, new android.location.LocationListener() 
     { 

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

      } 

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

      } 

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

      } 

      @Override 
      public void onLocationChanged(Location location) { 
       // TODO Auto-generated method stub 
       Log.d("a","onLocationChanged: lat=" +location.getLatitude()); 
       Log.d("a","onLocationChanged: lat=" +location.getLongitude()); 
      } 
     }); 

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


    GoogleMap mMap = null; 
    mMap.addMarker(new MarkerOptions().draggable(true) 
             .position(new LatLng(loc.getLatitude(),loc.getLongitude())) 
             .title("YOUR LOCATION")); 

LOG CAT:

1月3日至29日:37:37.167:d/ActivityThread(2521):installProvider: context=[email protected]=android.app.IActivityManager $ ContentProviderHolder @ 41fb9070noisy = truenoReleaseNeeded = falsestable = false 03-29 01:37:37.364:D/dalvikvm(2521):GC_FOR_ALLOC已釋放870K,13% 免費6526K/7424K,暫停110ms,共110ms 03-29 01:37 :37.365: D/ActivityThread(2521):installProvider: [email protected]=andr [email protected]=truenoReleaseNeeded=falsestable=false 03-29 01:37:37.637:E/linker(2521):load_library(linker.cpp:757): library 「/ data/data/「com.google.android.gms/files/libAppDataSearchExt_armeabi_v7a.so」 not found 03-29 01:37:37.642:D/ActivityThread(2521):installProvider: [email protected]=android.app。 IActivityManager $ ContentProviderHolder @ 41f7ffd8noisy = truenoReleaseNeeded = falsestable = false 03-29 01:37:37.833:D/ActivityThread(2521):installProvider: context = android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ ContentProviderHolder @ 41ea8d40noisy = truenoReleaseNeeded = falsestable = false 03-29 01:37:38.324:D/ActivityThread(2521):installProvider: context = android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ ContentProviderHolder @ 41f19948noisy = truenoReleaseNeeded = falsestable = false 03-29 01:37:38.645:D/ActivityThread(2521):installProvider: context = android。 app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ ContentProviderHolder @ 41fa1000noisy = truenoReleaseNeeded = falsestable = false 03-29 01:37:38.658:D/ActivityThread(2521):installProvider: [email protected] [email protected]y=truenoReleaseNeeded=falsestable=false 03-29 01:37:38.668:I/Icing(2521):內部初始化完成:存儲狀態0 03-29 01:37:38.725: D/ActivityThread(2521):installProvider: [email protected]我的/結冰(2521):初始化完成03-29 01:37:38.879: W/asset(2521):AssetManager - > addDefaultAssets CIP路徑不存在! 一月3日至29日:37:40.148:I/dalvikvm(2521):找不到方法 android.app.admin.DevicePolicyManager.isDeviceOwnerApp,從方法 com.google.android.gms.init.InitializeIntentService.onHandleIntent引用 03-29 01:37:40.148:W/dalvikvm(2521):VFY:無法解析虛擬 方法1697:Landroid/app/admin/DevicePolicyManager; .isDeviceOwnerApp (Ljava/lang/String;)Z 03-29 01:37:40.148:d/dalvikvm(2521):VFY:從 com.google.android.googlequicksearchbox 03 索引38E683E5680B78374039C38CC697D19B079F6A63: 更換操作碼0x6e在0x00e0 1月3日至29日:37:40.903:I /冰(2521) -29 01:37:41.108: D/ActivityThread(2521):installProvider: 上下文= android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ ContentProviderHolder @ 420511f0noisy = truenoReleaseNeeded = falsestable =假 一月3日至29日:37:41.505:d/ActivityThread(2521):installProvider: 上下文=機器人。應用程序。ContextImpl @ 41e4f100holder = android.app.IActivityManager $ ContentProviderHolder @ 420542a0noisy = truenoReleaseNeeded = falsestable = false 03-29 01:37:41.520:D/ActivityThread(2521):installProvider: [email protected]=android .app.IActivityManager $ ContentProviderHolder @ 42055cb8noisy = truenoReleaseNeeded = falsestable =假 1月3日至29日:37:41.534:d/ActivityThread(2521):installProvider: [email protected]=android.app.IActivityManager$ ContentProviderHolder @ 42058ce8noisy = truenoReleaseNeeded = falsestable = false 03-29 01:37:41.546:D/ActivityThread(2521):installProvider: context = android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ ContentProviderHolder @ 4205a700noisy = truenoReleaseNeeded = falsestable = false 03-29 01:37:4 1.554:I /冰(2521):索引完成 38E683E5680B78374039C38CC697D19B079F6A63一月3日至29日:37:44.889: d/IPCThreadState(2521):DN#5] BR_CLEAR_DEATH_NOTIFICATION_DONE 餅乾0x41263ee8

+0

需要初始化的LocationManager。 – dharms

+0

我試過了,但錯誤沒有解決。 –

+0

請用你試過的東西更新你的問題。在初始化之前確保你沒有使用'locationManager'。 ('Location loc = locationManager.getLastKnownLocation(best);'是一個很好的例子) – dharms

回答

0

你有你的加入活動到你的Android清單?

我得到這個錯誤太多,如果我忘了將其添加到清單。

http://developer.android.com/guide/topics/manifest/manifest-intro.html

+0

我已經添加了我的活動,但它仍然給出錯誤。 –

+0

好吧...你把谷歌地圖的開發者代碼添加到你的代碼中了嗎?通常我只是簡單地得到沒有內容的地圖,如果沒有添加代碼。第二個想法:你有谷歌地圖mMap = null,然後你嘗試添加一些東西。這在Java編碼中不起作用 – JonathanSchmied

+0

如果您在xml中包含地圖,請檢查編碼,如下所示:GoogleMap map =((MapFragment)getFragmentManager() .findFragmentById(R.id.map))。getMap(); – JonathanSchmied

0

你也需要給你的應用訪問GPS的權限。

只要把線

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

到你的清單和一切都會好的。

又見

ACCESS_FINE_LOCATION AndroidManifest Permissions Not Being Granted

+0

我已經添加了權限 –

+0

那麼它可能是什麼dcharms提到。由於您在方法體外調用該變量,因此它必須在方法體外部初始化並在使用之前進行初始化。否則,您正在引用一個空變量 –

相關問題