简体   繁体   English

Google-map-API V2中的当前位置

[英]Current location in Google-map-API V2

I am using Google API V2 in an Android application. 我在Android应用程序中使用Google API V2。 I want to get my current location and add a marker on it. 我想获取我的当前位置并在上面添加标记。 I am using my friend device. 我正在使用我的朋友设备。 when I run the code, it shows me my-friend's house location. 当我运行代码时,它显示了我朋友的房屋位置。 I am really shocked why. 我真的很震惊为什么。

could anyone please help me getting my current location.. 谁能帮我得到我的当前位置。

Thats because you're first loading the LastKnownLocation, because it's your friends phone it will show your friend last known location (his house). 那是因为您是第一次加载LastKnownLocation,因为它是您的朋友电话,它将显示您朋友的最后一个已知位置(他的房子)。

The code looks fine, I think you have to wait untill it finds the real location. 代码看起来不错,我认为您必须等到找到真正的位置。

(make sure you got all permissions, and gps is on) (确保您拥有所有权限,并且gps处于开启状态)

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

When you call locationManager.getLastKnownLocation(provider) your are retrieving the last location of the GPS that can be like you say when your friend was at home. 当您致电locationManager.getLastKnownLocation(provider)您正在检索GPS的最后一个位置,就像您的朋友在家时所说的那样。

If you want to have the location where you are at, you should call requestSingleUpdate for just one time update or requestLocationUpdates for a continuous update. 如果您想知道自己所在的位置,则应该只调用requestSingleUpdate一次更新,或者调用requestLocationUpdates进行连续更新。

An example for the one time update with a button listener: 一次使用按钮侦听器进行更新的示例:

btnupdate.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            locationManager.requestSingleUpdate(criteria, new LocationListener() {


                @Override
                public void onLocationChanged(Location location) {

                }
                @Override
                public void onProviderEnabled(String provider) {

                }
                @Override
                public void onProviderDisabled(String provider) {

                }
                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {

                }
            }, getMainLooper());
        }
    });

This implements a listener for the LocationManager, that gives you the posibility to do some actions depending of the Location status. 这实现了LocationManager的侦听器,使您可以根据Location状态执行某些操作。

In your case, you have implemented the listener on the activity, so just try calling: 对于您的情况,您已经在活动上实现了侦听器,因此只需尝试调用:

locationManager.requestSingleUpdate(criteria, this);

This request just single update to show your present location 此请求仅需一次更新即可显示您的当前位置

I am coming with an answer to my question. 我要回答我的问题。 I don't know if it will work with all people facing the same problem. 我不知道它是否对所有面临相同问题的人都有效。 I just tried to restart the mobile device. 我只是尝试重新启动移动设备。 After it restarts, it gets the correct current location. 重新启动后,它将获得正确的当前位置。 So it was a hardware problem though I thought it is something in the code.. 所以这是一个硬件问题,尽管我认为这是代码中的问题。

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

相关问题 片段中的当前用户位置Google Map API v2(无效) - Current user location Google Map API v2 in Fragment (NOT ACTIVITY) android + google map api v2 +当前位置 - android + google map api v2 + current location 显示Google Maps API V2中的当前位置,此地图位于一个滑动标签中 - showing current location in Google Maps API V2, this map in one of sliding tabs Android Google Map API(v2)显示当前位置获取遗憾应用程序已停止错误 - Android Google Map API(v2) Show Current Location getting Unfortunately application has stopped error 使用Android Google Map API(v2)获取当前位置,但不幸的是应用程序已停止错误 - Get Current Location with Android Google Map API(v2) but getting Unfortunately application has stopped error 在Android Google Map v2上显示当前位置,但应用程序崩溃 - Showing current location on android Google Map v2, But app crashes 在谷歌地图v2中更改自定义当前位置指示器 - Change custom current location indicator in google map v2 Google Map v2查找当前位置是否在圆圈中 - Google map v2 find if current location lies in circle Google Map API v2 - 获取从当前位置到已知位置的行车距离 - Google Map API v2 - Get Driving Distance from Current Location to Known Locations 使用Google Maps API v2以TEXT形式获取当前位置 - Get current location as TEXT with Google Maps API v2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM