问题
I am trying to create an app that will obtain the users location via gps, display the users location in google maps, and then update after a certain amount of time/movement.
I currently have an app that will obtain the users location via gps and update itself every 10meters/10,000 miliseconds, but currently all it does is display what the coordinates are. I have it set up to connect to google maps, but at the minute it is simply setting the map to some coordinates that I have manually entered myself.
How do I get it so the map will display the location based on the coordinates obtained via gps?
Any help would be greatly appreciated, I am very new to all this!
EDIT: heres my code so far!
package com.android.basicmap;
import com.google.android.maps.MapActivity;
import android.os.Bundle;
import com.google.android.maps.MapView;
import com.google.android.maps.MapController;
import com.google.android.maps.GeoPoint;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
public class BasicMapActivity extends MapActivity {
private MapView mapView;
private MapController mapController;
private LocationManager locationManager;
private LocationListener locationListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
locationListener);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setStreetView(true);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(16);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
private class GPSLocationListener implements LocationListener
{
@Override
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();
}
}
}
}
回答1:
Here's a full-blown tutorial on that: http://www.codeproject.com/Articles/112044/GPSLocator-App-to-Find-Current-Nearest-Location-us
But come on, I found that by entering "android how to show gps location on google map", please, google more thoroughly and go through tutorials before posting such simple questions :)
回答2:
Assuming that you are using MapView
, the simplest thing to do is add an instance of MyLocationOverlay
to the map, as Android will handle displaying the user's location for you.
map=(MapView)findViewById(R.id.whatever_your_mapview_id_is);
map.getOverlays().add(new MyLocationOverlay(this, map));
If for some reason you want to do that yourself rather than use the built-in facility, you can create your own ItemizedOverlay
to display the point, and add an instance of your own custom overlay to the MapView
.
回答3:
It's the best to use MyLocationOverlay if you want to display user's current location. However you have alternative of:
map.animateTo(GeoPoint);
回答4:
Assuming that u've defined your mapview and mapcontroller
for that, first you neeed suscribe to the location update in your OnCreate method like that: the code belows requests update every 1000ms or 10m
LocationManager lm;
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);
below you is my way to update my position on the map
@Override
GeoPoint p,P2, p3;
double lat=53, double lng=4;
public void onLocationChanged(Location location) {
Log.v(tag, "Location Changed");
lat= (int) (location.getLatitude());
lng= (int) (location.getLongitude());
sb = new StringBuilder(512);
noOfFixes++;
p = new GeoPoint(
(int) (location.getLatitude()*1E6),
(int) (location.getLongitude()*1E6));
mapController.animateTo(p);
mapController.setCenter(p);
Toast.makeText(this, "Lat= " + lat + " Long= " + lng, Toast.LENGTH_SHORT).show();
}
来源:https://stackoverflow.com/questions/9198773/android-eclipse-display-google-maps-using-coordinates-obtained-using-gps