如何在Android应用程序中获取经度纬度

时间:2014-03-28 20:46:44

标签: java android google-maps

我正在创建一个应用程序,我必须获取用户的经度和纬度并标记其位置。我的应用程序发出错误“不幸地图已停止”。我google了很多但无法找到解决方案。这是我的代码和我的日志猫。

  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:

  

03-29 01:37:37.167:D / ActivityThread(2521):installProvider:   上下文= android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ @ ContentProviderHolder = 41fb9070noisy = truenoReleaseNeeded = falsestable假   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:   上下文= android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ @ ContentProviderHolder = 41fc0078noisy = truenoReleaseNeeded = falsestable假   03-29 01:37:37.637:E / linker(2521):load_library(linker.cpp:757):   图书馆   “/data/data/com.google.android.gms/files/libAppDataSearchExt_armeabi_v7a.so”   未找到03-29 01:37:37.642:D / ActivityThread(2521):installProvider:   上下文= android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ @ ContentProviderHolder = 41f7ffd8noisy = truenoReleaseNeeded = falsestable假   03-29 01:37:37.833:D / ActivityThread(2521):installProvider:   上下文= android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ @ ContentProviderHolder = 41ea8d40noisy = truenoReleaseNeeded = falsestable假   03-29 01:37:38.324:D / ActivityThread(2521):installProvider:   上下文= android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ @ ContentProviderHolder = 41f19948noisy = truenoReleaseNeeded = falsestable假   03-29 01:37:38.645:D / ActivityThread(2521):installProvider:   上下文= android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ @ ContentProviderHolder = 41fa1000noisy = truenoReleaseNeeded = falsestable假   03-29 01:37:38.658:D / ActivityThread(2521):installProvider:   上下文= android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ @ ContentProviderHolder = 41ee72b0noisy = truenoReleaseNeeded = falsestable假   03-29 01:37:38.668:I / Icing(2521):内部初始化完成:存储状态0   03-29 01:37:38.725:D / ActivityThread(2521):installProvider:   上下文= android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ @ ContentProviderHolder = 41f7a920noisy = truenoReleaseNeeded = falsestable假   03-29 01:37:38.755:I / Icing(2521):初始化完成03-29 01:37:38.879:   W / asset(2521):AssetManager - > addDefaultAssets CIP路径不存在!   03-29 01: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:   替换操作码0x6e在0x00e0 03-29 01:37:40.903:I / Icing(2521):   索引38E683E5680B78374039C38CC697D19B079F6A63来自   com.google.android.googlequicksearchbox 03-29 01:37:41.108:   D / ActivityThread(2521):installProvider:   上下文= android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ @ ContentProviderHolder = 420511f0noisy = truenoReleaseNeeded = falsestable假   03-29 01:37:41.505:D / ActivityThread(2521):installProvider:   上下文= android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ @ ContentProviderHolder = 420542a0noisy = truenoReleaseNeeded = falsestable假   03-29 01:37:41.520:D / ActivityThread(2521):installProvider:   上下文= android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ @ ContentProviderHolder = 42055cb8noisy = truenoReleaseNeeded = falsestable假   03-29 01:37:41.534:D / ActivityThread(2521):installProvider:   上下文= android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ @ ContentProviderHolder = 42058ce8noisy = truenoReleaseNeeded = falsestable假   03-29 01:37:41.546:D / ActivityThread(2521):installProvider:   上下文= android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ @ ContentProviderHolder = 4205a700noisy = truenoReleaseNeeded = falsestable假   03-29 01:37:41.554:I / Icing(2521):完成索引   38E683E5680B78374039C38CC697D19B079F6A63 03-29 01:37:44.889:   D / IPCThreadState(2521):[DN#5] BR_CLEAR_DEATH_NOTIFICATION_DONE   cookie 0x41263ee8

2 个答案:

答案 0 :(得分:0)

您是否已将活动添加到Android清单中?

如果我忘记将其添加到清单中,我也会收到此错误。

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

答案 1 :(得分:0)

您还需要授权您的应用访问GPS。

只需放行

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

进入你的清单,一切都会好起来的。

另见

ACCESS_FINE_LOCATION AndroidManifest Permissions Not Being Granted