0

I wanted to show the current location of the user as soon as the user opens the activity.

Here is what I have coded so far.

package com.android.carmanager;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class CarFinder extends MapActivity{
private MapView mapView;
private MapController mapController;
private LocationManager locationManager;
private LocationListener locationListener;

@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.carfinder);
    mapView = (MapView) findViewById(R.id.mapView);
      mapView.setStreetView(true);
      mapView.setBuiltInZoomControls(true);
      mapView.displayZoomControls(true);

      mapController = mapView.getController();
      mapController.setZoom(16);

      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  

      locationListener = new GPSLocationListener();

      locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 
                0, 
                0, 
                locationListener);
  }
private  class GPSLocationListener implements LocationListener 
{
  public void onLocationChanged(Location location) {
    if (location != null) {
      GeoPoint point = new GeoPoint(
          (int) (location.getLatitude() * 1E6), 
          (int) (location.getLongitude() * 1E6));

      Toast.makeText(getBaseContext(), 
          "Latitude: " + location.getLatitude() + 
          " Longitude: " + location.getLongitude(), 
          Toast.LENGTH_SHORT).show();

      mapController.animateTo(point);
      mapController.setZoom(16);
      mapView.invalidate();
    }
  }

public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}   
}

@Override
  protected boolean isRouteDisplayed(){
      //TODO Auto-generated method stub
      return false;

  }
  }

Attaching the manifest file here as well:

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

  <uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" ></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" ></uses-permission> 
<uses-permission android:name="android.permission.INTERNET" ></uses-permission>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">
        <uses-library android:name = "com.google.android.maps" />

    <activity android:name=".CarManagerActivity" android:label= "@string/app_name">
    <intent-filter> 
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
    <activity android:name=".MileageCalculator" android:label= "@string/app_name"></activity>
    <activity android:name=".About" android:label= "@string/app_name"></activity>
 <activity android:name=".CarFinder" android:label="@string/app_name"></activity>
 </application>

</manifest>

This is the error log cat file if required. I couldn't find any error as far as locating the location is concerned.

05-27 16:07:05.647: I/dalvikvm(706): threadid=3: reacting to signal 3
05-27 16:07:05.697: I/dalvikvm(706): Wrote stack traces to '/data/anr/traces.txt'
05-27 16:07:06.156: D/gralloc_goldfish(706): Emulator without GPU emulation detected.
05-27 16:07:06.167: I/dalvikvm(706): threadid=3: reacting to signal 3
05-27 16:07:06.197: I/dalvikvm(706): Wrote stack traces to '/data/anr/traces.txt'
05-27 16:07:25.666: I/dalvikvm(706): threadid=3: reacting to signal 3
05-27 16:07:25.808: I/dalvikvm(706): Wrote stack traces to '/data/anr/traces.txt'
05-27 16:07:25.868: D/dalvikvm(706): GC_CONCURRENT freed 161K, 3% free 9352K/9607K, paused 12ms+7ms
05-27 16:07:25.868: W/CursorWrapperInner(706): Cursor finalized without prior close()
05-27 16:07:25.868: W/CursorWrapperInner(706): Cursor finalized without prior close()
05-27 16:07:26.186: I/dalvikvm(706): threadid=3: reacting to signal 3
05-27 16:07:26.326: I/dalvikvm(706): Wrote stack traces to '/data/anr/traces.txt'
05-27 16:07:26.396: D/dalvikvm(706): GC_CONCURRENT freed 40K, 3% free 9764K/9991K, paused 12ms+21ms
05-27 16:07:26.436: I/Maps.MyLocationOverlay(706): Request updates from gps
05-27 16:07:26.656: E/ZoomButtonsController(706): Cannot make the zoom controller visible if the owner view is not attached to a window.
05-27 16:07:26.746: I/dalvikvm(706): threadid=3: reacting to signal 3
05-27 16:07:26.767: I/dalvikvm(706): Wrote stack traces to '/data/anr/traces.txt'
05-27 16:07:26.827: I/MapActivity(706): Handling network change notification:CONNECTED
05-27 16:07:26.827: E/MapActivity(706): Couldn't get connection factory client
05-27 16:07:27.137: D/dalvikvm(706): GC_CONCURRENT freed 40K, 3% free 10167K/10375K, paused 8ms+6ms
05-27 16:07:27.188: I/dalvikvm(706): threadid=3: reacting to signal 3
05-27 16:07:27.227: I/dalvikvm(706): Wrote stack traces to '/data/anr/traces.txt'
05-27 16:07:27.887: D/dalvikvm(706): GC_CONCURRENT freed 375K, 5% free 10237K/10759K, paused 12ms+6ms

But when I open the activity, the maps opens up and the location is nowhere to be seen.

It doesn't even display a longitude/latitude.

How do i tweak my code to display the current location?

Sanchit
  • 25
  • 9
  • Did you request the user's permission through the manifest file ? `` – Zakaria May 27 '12 at 11:12
  • @Zakaria Yes I did requested for the permission. Have added the manifest file code as well in the description above. Kindly take a look and correct me if i have placed them wrongly. Thanks in advance. – Sanchit May 27 '12 at 17:43
  • if you want to show the current location then better you go for MyLocationOverlay for MapActivity.Have a look over this http://stackoverflow.com/questions/5323918/mylocationoverlay-problem – Shabbir Panjesha May 28 '12 at 06:43

3 Answers3

1

Point 1: As per @Shabbir in the comments above, recommendation is to use MyLocationOverlay.


Point 2:

But when I open the activity, the maps opens up and the location is nowhere to be seen.

The MyLocationOverlay needs time to get a location fix, so it starts off by showing nothing. It only shows the location after that first fix is found.

If you already know your location before launching this activity, you can subclass MyLocationOverlay to accept a defaultLocation parameter in the constructor. Then override the draw() method like this:

public synchronized boolean draw(Canvas canvas, MapView mapView,
    boolean shadow, long when)
{
    //if no location found
    if(getLastFix()==null && defaultLocation!=null)
    {
        drawMyLocation(canvas, mapView, defaultLocation, DataUtilities.locationToGeoPoint(defaultLocation), when);
    }

    return super.draw(canvas, mapView, shadow, when);
}

Draw your default location if no fix has yet been found, else just let the base class do its thing.

With this method, you can show an initial location (defaultLocation) straight away, while waiting for your device to get a better fix.

Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
0

Have a look at this... http://android-er.blogspot.in/2009/11/mapview-to-center-on-current-location.html

Richard Rose
  • 109
  • 2
  • 9
0

Use this snippet

   List<Overlay> over_lay = mapView.getOverlays();
   MyLocationOverlay mylocationoverlay = new MyLocationOverlay(this, mapView);
   over_lay.add(mylocationoverlay);
john
  • 359
  • 1
  • 4
  • 15