1

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>
Siddharth
  • 9,349
  • 16
  • 86
  • 148
Ahmed Basiouny
  • 67
  • 1
  • 4
  • 8
  • I would recommend to use FusedLocationApi to get location updates. You can find an example here https://stackoverflow.com/a/44908276/1808829 – Ayaz Alifov Jul 04 '17 at 14:33

1 Answers1

3

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.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
  • Thanks it works now, i also discovered that, sometimes i have to restart my device so i can see the effect of my new code. – Ahmed Basiouny May 04 '13 at 18:04
  • Did you also discover that `onProviderDisabled` and `onProviderEnabled` and `onStatusChanged` and imports and comments need not have been part of your question ? You have never upvoted an answer (although you have accepted it). You have never put little code you have put ALL your code in ALL your questions. Again, read the FAQ. From your badges its evident that you have never read the FAQ. @MaciejGorski upvote for you. – Siddharth May 27 '13 at 06:25