Android谷歌地图V2没有更新位置

Android谷歌地图V2没有更新位置,android,google-maps,Android,Google Maps,我正在用Android和Google Maps V2开发一个应用程序,我尝试了很多教程,该应用程序运行良好,没有错误,它显示了我的位置,但当我尝试在另一个位置打开该应用程序时,该应用程序根本没有更新位置,我需要知道如何更新位置,我正在用ICS软件测试三星Galaxy S Duos GT-S7562 我的问题是: 如何更新我的当前位置?。 在地图的右上角有一个按钮,我怎样才能把一个事件放进去,这样我才能得到我的当前位置 这是我正在使用的代码 ShowMap.java: package com.ma

我正在用Android和Google Maps V2开发一个应用程序,我尝试了很多教程,该应用程序运行良好,没有错误,它显示了我的位置,但当我尝试在另一个位置打开该应用程序时,该应用程序根本没有更新位置,我需要知道如何更新位置,我正在用ICS软件测试三星Galaxy S Duos GT-S7562

我的问题是: 如何更新我的当前位置?。 在地图的右上角有一个按钮,我怎样才能把一个事件放进去,这样我才能得到我的当前位置

这是我正在使用的代码

ShowMap.java:

package com.maps00;

public class ShowMap extends Activity implements LocationListener{

     GoogleMap googleMap;
     LatLng myposition;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_map);
         MapFragment fm=(MapFragment) getFragmentManager().findFragmentById(R.id.map);
    googleMap=fm.getMap();
    googleMap.setMyLocationEnabled(true);
    LocationManager locationManager=(LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria=new Criteria(); // object to retrieve provider
    String provider = locationManager.getBestProvider(criteria, true);
    Location location=locationManager.getLastKnownLocation(provider);
    if(location!=null){
        onLocationChanged(location);
    }
    locationManager.requestLocationUpdates(provider, 20000, 0, this);
    }

    @Override
    public void onLocationChanged(Location location) {
                double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        LatLng latLng = new LatLng(latitude, longitude);
        myposition = new LatLng(latitude, longitude);
        googleMap.addMarker(new MarkerOptions().position(myposition).title("Start"));
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(17));
        TextView t=(TextView) findViewById(R.id.tv_location);
        t.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude );
    }
}
Manifest.xml

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

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />

    <permission
        android:name="com.maps00.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <uses-permission android:name="com.maps00.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <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" />
    <uses-permission android:name="android.permission.com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-feature
        android:name="android.hardware.location"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.location.network"
        android:required="false" />
    <uses-feature android:name="android.hardware.location.gps" />
    <uses-feature
        android:name="android.hardware.wifi"
        android:required="false" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.maps00.ShowMap"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="MY KEY" />


    </application>
</manifest> 

Layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ShowMap" >
<fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />
     <TextView
        android:id="@+id/tv_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/map"
        android:layout_alignTop="@+id/map"
        android:layout_marginLeft="83dp"
        android:text="TextView" />
</RelativeLayout>

编辑:

据说从库的3.1.36版本起已弃用

原件:

使用而不是直接与
LocationManager
交互


内存泄漏和电池耗尽警告:当您使用
requestLocationUpdates
时,应该在某个地方注销。你的用户不会喜欢你的应用程序,如果它在
活动
关闭后仍要求更新。

谢谢,它现在可以工作了,我还发现,有时我必须重新启动设备才能看到新代码的效果。您是否还发现
onProviderDisabled
onProviderEnabled
onStatusChanged
以及导入和注释不必是您问题的一部分?你从未对答案投过赞成票(尽管你已经接受了)。你从来没有把小代码你把你所有的代码都放在你所有的问题上。再次阅读常见问题解答。从您的徽章可以看出,您从未阅读过常见问题解答@MaciejGorski upvote支持你。我建议使用FusedLocationApi获取位置更新。你可以在这里找到一个例子