1

I am developing an Android application using google maps api v2. My problem is that setMyLocationEnabled(true) does not display my accurate position. My app shows a wrong location whereas the official gmaps app displays my correct location. How is that possible? It's wrong by approx 600 meters. I would post screenshots but as I'm new on stackoverflow I'm not allowed to.

Here is my MainActivity.java

package com.shanghai.powerplugs;

public class MainActivity extends FragmentActivity implements
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener,
    com.google.android.gms.location.LocationListener {

        LocationManager myLocationManager = null;
        OnLocationChangedListener myLocationListener = null;
        Criteria myCriteria;

        GoogleMap map;
        MarkerOptions userMarker;
        LatLng userLocation;
        CheckBox mapSatellite;
        SupportMapFragment fm;
        Vibrator vib;
        String provider;
        LocationManager locationManager;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GoogleMap map = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map)).getMap();

    map.setMyLocationEnabled(true);
    //map.setTrafficEnabled(true);
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    provider = locationManager.getBestProvider(criteria, true);

    Location location = locationManager.getLastKnownLocation(provider);
    if(location!=null){

    }else{
        provider = LocationManager.NETWORK_PROVIDER;
        location = locationManager.getLastKnownLocation(provider);
    }

    userMarker = new MarkerOptions();
    userLocation = new LatLng(location.getLatitude(), location.getLongitude());
    userMarker.position(userLocation);
    map.addMarker(userMarker);

    map.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
    map.animateCamera(CameraUpdateFactory.zoomTo(15));



}

@Override
public void onConnectionFailed(ConnectionResult arg0) {
    // TODO Auto-generated method stub

}


@Override
public void onConnected(Bundle arg0) {

}

@Override
public void onDisconnected() {
    // TODO Auto-generated method stub

}

@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub

}

}

FYI I just found out that by changhing map.setMapType(GoogleMap.MAP_TYPE_NORMAL); to map.setMapType(GoogleMap.MAP_TYPE_SATELLITE); the location is finally right.. Very interesting!

2 Answers2

0

Chack GPS enabled in device. More information in

How can I enable or disable the GPS programmatically on Android?

private void turnGPSOn(){ String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

if(!provider.contains("gps")){ //if gps is disabled
    final Intent poke = new Intent();
    poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
    poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
    poke.setData(Uri.parse("3")); 
    sendBroadcast(poke);
}

}

private void turnGPSOff(){ String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

if(provider.contains("gps")){ //if gps is enabled
    final Intent poke = new Intent();
    poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
    poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
    poke.setData(Uri.parse("3")); 
    sendBroadcast(poke);
}

}

Community
  • 1
  • 1
0

I can see that you are implementing GoolgePlayServicesClient. Why are you not using it. Create a LocationClient object and get location through it.

 LocationClient mLocationClient = new LocationClient(this, this, this);
 Location mCurrentLocation = mLocationClient.getLastLocation();
 double clientLat = mCurrentLocation.getLatitude();
 double clientLng = mCurrentLocation.getLongitude();

Also in onLocationChanged method you can access Location. It is always better to get last location from this method.