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!