简体   繁体   中英

Android Google Maps V2 did not update location

I am developing an application with Android and Google Maps V2, i tried many tutorials, and the application is working fine with no error, and it showed up my location, but when i tried to open the application in another location, the application did not update the location at all, i need to know how to update location, i am testing on Samsung Galaxy S Duos GT-S7562 with ICS software

So my questions on: How to update my current location?. There is a button in top right got with the map, how can i put an event in it so i can get my current location?.

Here is the code i am using.

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>

Edit:

GoogleMap.setOnMyLocationChangeListener is said to be deprecated as of 3.1.36 version of the library.

Original:

Use GoogleMap.setOnMyLocationChangeListener instead of interacting with LocationManager directly.

Memory leak and battery drain warning : You should be unregistering somewhere when you use requestLocationUpdates . Your users won't like your app if it will request updated even after Activity has been closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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