Im having problems with my current code, i don't see what i'm doing wrong. i keep getting the error "no suitable method found for requestLocationUpdates(string,int,in,MapsActivity)
public class MapsActivity extends AppCompatActivity implements LocationListener, OnMapReadyCallback, View.OnClickListener, DirectionCallback, OnMapClickListener, GoogleApiClient.OnConnectionFailedListener {
private LocationManager locationManager;
private Button btnRequestDirection;
GPSTracker gps;
private GoogleMap googleMap;
private String serverKey = "xxx";
private LatLng camera = new LatLng(54.1111, -1.1111);
private LatLng origin = new LatLng(54.1111, -1.1111);
private LatLng destination = new LatLng(54.11, -1.11);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
gps = new GPSTracker(MapsActivity.this);
long i = 1000;
long x = 10;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,i,x, this);
if (gps.canGetLocation()) {
camera = new LatLng(gps.getLatitude(), gps.getLongitude());
origin = camera;
btnRequestDirection = (Button) findViewById(R.id.btn_request_direction);
btnRequestDirection.setOnClickListener(this);
((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
} else {
gps.showSettingsAlert();
}
}
Everything i do the event "onLocationChanged()" never triggers, =/
You need to have a locationListener to listen for a location changes. If you are using 'requestLocationUpdates', you must use 'import android.location.LocationListener'. The standard Android location stuffs is 'android.location'. The com.google.android.gms.location stuff is for use with the FusedLocationProviderApi which is part of Google play services.
Related
I have a small app that receives your current GPS coordinates in latitude and longitude when a "Save Location" button is pressed. Another activity is started where the map is shown and your current position is marked. On this map activity, there is a button you can press to go to another activity (let's call this the memo activity), in which there is a button to bring you back to the map activity. However, the app crashes upon pressing this button that brings you back to the map activity.
Here is the code for the main activity, map activity and the memo activity.
Main Activity
public class HomeScreen extends AppCompatActivity {
private Button button;
private TextView textView;
private LocationManager locationManager;
private LocationListener locationListener;
private double longitude;
private double latitude;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
button = (Button) findViewById(R.id.button);
}
public void saveAndGo(View view) {
GPStracker g = new GPStracker(getApplicationContext());
Location l = g.getLocation();
if (l != null)
{
longitude = l.getLongitude();
latitude = l.getLatitude();
}
Intent go = new Intent(this, MapsActivity.class);
go.putExtra("longitude", longitude);
go.putExtra("latitude", latitude);
startActivity(go);
finish();
}
Map Activity
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private Double latitude;
private Double longitude;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
latitude = getIntent().getExtras().getDouble("latitude");
longitude = getIntent().getExtras().getDouble("longitude");
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng pos = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(pos).title("Your Location"));
mMap.setMinZoomPreference(16.0f);
mMap.setMaxZoomPreference(20.0f);
mMap.setMyLocationEnabled(true);
mMap.moveCamera(CameraUpdateFactory.newLatLng(pos));
}
public void goMemos (View view) {
Intent go = new Intent(this, MemoScreen.class);
startActivity(go);
}
Memo Activity
public class MemoScreen extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.memoscreen);
}
public void goMap (View view){
Intent go = new Intent(this, MapsActivity.class);
startActivity(go);
}
}
I assume it's crashing because when I try to get data from the main activity when switching back to the map activity from the memo activity, the data is null. The app runs just fine if I remove the getExtras() calls from the map activity.
How do I get around this?
Note: I would like to avoid using finish() in the memo activity since I plan to add more screens to this app, thus using that may end up in undesired behavior (i.e., returning back to the wrong activity).
you have to put simple condition that is
if(getIntent().getExtras() != null){
latitude = getIntent().getExtras().getDouble("latitude");
longitude = getIntent().getExtras().getDouble("longitude");
}
just use this in your mapsactivity
and not forgot to initialize the value of
private Double latitude=123.45;
private Double longitude=123.45;
I new in coding google map,
my question is how i control the user gestur drag , zoom in and zoom out.
because my code always back to the current location of user when i zoomin/out, nad when i drag/ scroll up, down, left, right. always back to the current possition .
its my code for current loc user
private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
mMarker = mMap.addMarker(new MarkerOptions().position(loc));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16));
}
};
You can use a boolean to move the camera only the first time:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMyLocationChangeListener {
private GoogleMap mMap;
private Marker mMarker;
private boolean firstTime = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(this);
}
#Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
mMarker = mMap.addMarker(new MarkerOptions().position(loc));
if (firstTime) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16));
firstTime = false;
}
}
}
NOTE: Take into account that this example uses GoogleMap.OnMyLocationChangeListener only because that is the method that you are using in your question, but it's deprecated and you must use the FusedLocationProviderApi according to the documentation:
public final void setOnMyLocationChangeListener
(GoogleMap.OnMyLocationChangeListener listener)
This method was deprecated. use
com.google.android.gms.location.FusedLocationProviderApi instead.
FusedLocationProviderApi provides improved location finding and power
usage and is used by the "My Location" blue dot. See the
MyLocationDemoActivity in the sample applications folder for example
example code, or the Location Developer Guide.
i have a problem with my code, in a fragment i have this code:
public class Logo extends Fragment implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {
LocationManager lm;
Location mLastLocation;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private Location mCurrentLocation;
private TextView Lat;
private TextView Long;
String provider;
public Logo() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static Logo newInstance() {
Logo fragment = new Logo();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
lm = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
View view = inflater.inflate(R.layout.fragment_main, container, false);
Lat = (TextView) view.findViewById(R.id.Latitude);
Long = (TextView) view.findViewById(R.id.Longitude);
TextView Morad = (TextView) view.findViewById(R.id.Morada);
Criteria c=new Criteria();
provider=lm.getBestProvider(c, false);
mLastLocation=lm.getLastKnownLocation(provider);
Lat.setText("A obter");
Long.setText(" dados");
Morad.setText("Aguarde...");
if(mLastLocation!=null)
{
Lat.setText(String.valueOf(mLastLocation.getLatitude()));
Long.setText(String.valueOf(mLastLocation.getLongitude()));
}
else
{
Lat.setText("No connection");
Long.setText(" wait");
}
return view;
}
#Override
public void onConnected(Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
Lat.setText(String.valueOf(mLastLocation.getLatitude()));
Long.setText(String.valueOf(mLastLocation.getLongitude()));
}
}
But never get the Lat and Longitude values, what i missed up.
I only want to return the Latitude and Longitude values and put in the 2 filds.
you need to decide if you are going to use the built in LocationManager or google play services Location APi because you are trying to use both and that will not work.
if you are trying to use the built in one then you never get a location because you dont have a last location and you never request location updates.
if you are trying to use the google play services location API well you need to do more work because you didnt really even implement it. I guess really in both cases you still have more work because you really didnt implement either correctly
Google finally added a callback for location changes in the Android API v2! However, I cannot intuitively get it to work, and Google does not have much documentation for it. Has anyone gotten it to work? What more do I need?
public class ... extends SupportMapFragment implements GoogleMap.OnMyLocationChangeListener {
GoogleMap map;
LocationManager locationManager;
String provider;
#Override
public void onActivityCreated(android.os.Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
map = getMap();
if (map != null) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
locationManager =(LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(criteria, false);
}
}
#Override
public void onResume() {
super.onResume();
while(map == null) {
map = getMap();
map.setMyLocationEnabled(true);
map.setOnMyLocationChangeListener(this);
}
}
#Override
public void onMyLocationChange(Location loc) {
//implementation
}
}
This is how I do to navigate to the center of the map when we get the first location-update.
my class header:
public class FragActivity extends SherlockFragmentActivity implements OnMyLocationChangeListener
private GoogleMap mMap;
my mMap-setup:
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = customMapFragment.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
setUpMap();
}
setUpMap-method:
private void setUpMap() {
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(this);
}
and my onlocationchange:
#Override
public void onMyLocationChange(Location lastKnownLocation) {
CameraUpdate myLoc = CameraUpdateFactory.newCameraPosition(
new CameraPosition.Builder().target(new LatLng(lastKnownLocation.getLatitude(),
lastKnownLocation.getLongitude())).zoom(6).build());
mMap.moveCamera(myLoc);
mMap.setOnMyLocationChangeListener(null);
}
Works like a charm
I am trying to get the "blue dot" my location coordinates and set it as the center of my initial mapview load. But I am getting a null pointer exception.
What am I missing here ?
public class GetLocationActivity extends MapActivity implements OnClickListener {
private MapView location;
private LocationMarker locationMarker;
private MyLocationOverlay locationOverlay;
private GeoPoint destination;
private GeoPoint source;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location);
location = (MapView) this.findViewById(R.id.location);
location.setBuiltInZoomControls(true);
location.setClickable(true);
location.setLongClickable(true);
location.getController().setZoom(12);
locationMarker = new LocationMarker(this, location, marker,
(ImageView) findViewById(R.id.drag));
location.getOverlays().add(locationMarker);
locationOverlay = new MyLocationOverlay(this, location);
location.getOverlays().add(locationOverlay);
source = locationOverlay.getMyLocation();
location.getController().setCenter(
new GeoPoint(source.getLatitudeE6(), source.getLongitudeE6()));
}
Getting a nullPointer at source.getLatitudeE6().
Forgetting my basics here :). I need to get my location using LocationManager.
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
(LocationListener) this);