繁体   English   中英

如何在Android应用中获取经度

[英]how to get longitude latitude in android app

我正在创建一个应用程序,其中必须获取用户的经度和纬度并标记他的位置。 我的应用程序显示错误消息“很遗憾,地图已停止”。 我搜索了很多,但是找不到解决方案。 这是我的代码和我的日志猫。

  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"));

日志猫:

03-29 01:37:37.167:D / ActivityThread(2521):installProvider:context = android.app.ContextImpl @ 41e4f100holder = 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:context = android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ ContentProviderHolder @ 41fc0078noisy = truenoReleaseNeeded = falsestable = false 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:context=android.app.ContextImpl@41e4f100holder=android.app.IActivityManager$ ContentProviderHolder @ 41f7ffd8noisy = truenoReleaseNeeded = falsestable = false 03-29 01:37:37.833:D / ActivityThread(2521):installProvider:context=android.app.ContextImpl@41 e4f100holder=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 = ednoRelease falsestable = false 03-29 01:37:38.658:D / ActivityThread(2521):installProvider:context = android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ ContentProviderHolder @ 41ee72b0noisy = truenoReleaseNeeded = falsestable = false 03-29 01 :37:38.668:I / Icing(2521):内部初始化完成:存储状态0 03-29 01:37:38.725:D / ActivityThread(2521):installProvider:context=android.app.ContextImpl@41e4f100holder=android.app .IActivityManager $ ContentProviderHolder @ 41f7a920noisy = truenoR eleaseNeeded = falsestable = false 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:在0x00e0处替换操作码0x6e 03-29 01:37:40.903:I / Icing(2521):从com.google.android.googlequicksearchbox 03-29索引38E683E5680B78374039C38CC697D19B079F6A63:D / 29 ActivityThread(2521):installProvider:context = android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ ContentProviderHolder @ 420511f0noisy = truenoReleaseNeeded = falsestable = false 03-29 01:37:41.505:D / ActivityThread(2521):installProvider: context = android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ ContentProviderHolder @ 420542a0noisy = truenoReleaseNeeded = falsestable = false 03-29 01:37:41.520:D / ActivityThread(2521):installProvider:context = android.app.ContextImpl @ 41e4f100holder = android.app.IActivityManager $ ContentProviderHolder @ 42055cb8noisy = truenoReleaseNeeded = falsestable = false 03-29 01:37:41.534:D / ActivityThread(2521):installProvider:context = android.app.ContextImpl @ 41e4f100holder = 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:41.554:I / Icing(2521):Indexing done 38E683E5680B78374039C38CC697D19B079 37:44.889:D / IPCThreadState(2521):[DN#5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x41263ee8

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

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

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

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

只要把线

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

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

也可以看看

ACCESS_FINE_LOCATION AndroidManifest权限未授予

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM