如何在谷歌地图上标记我的当前位置?
我正在使用谷歌地方 API。我必须从我当前的位置显示所有附近的地方。所有地点都显示在谷歌地图上,但如何显示我当前的位置?代码如下:
public class PoliceStationMapActivity extends FragmentActivity implements LocationListener {
private ArrayList<Place> mArrayListPoliceStations;
private GoogleMap mMap;
private LocationManager locManager;
private double latitude;
private double longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_police_station);
locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
else
Log.i("Test", "network provider unavailable");
Location lastKnownLocation = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
latitude = lastKnownLocation.getLatitude();
longitude = lastKnownLocation.getLongitude();
if (lastKnownLocation != null) {
Log.i("Test", lastKnownLocation.getLatitude() + ", " + lastKnownLocation.getLongitude());
locManager.removeUpdates(this);
}
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap != null) {
new GetAllPoliceStationsTask().execute("" + latitude, "" + longitude);
}
}
private class GetAllPoliceStationsTask extends AsyncTask<String, Void, ArrayList<Place>> {
@Override
protected ArrayList<Place> doInBackground(String... param) {
ArrayList<Place> policeStationsList = RequestHandler.getInstance(PoliceStationMapActivity.this).getAllPlaces(param[0], param[1]);
return policeStationsList;
}
@Override
protected void onPostExecute(java.util.ArrayList<Place> result) {
if (result != null) {
mArrayListPoliceStations = result;
placeAllPoliceStationMarkersOnMap(mArrayListPoliceStations);
}
}
}
private void placeAllPoliceStationMarkersOnMap(ArrayList<Place> policeStationList) {
for (Place place : policeStationList) {
addPlaceMarkerOnMap(place);
}
};
private void addPlaceMarkerOnMap(Place place) {
LatLng latLng = new LatLng(place.getLatitude(), place.getLongitude());
Marker poiMarker = mMap.addMarker(new MarkerOptions().position(latLng).title(place.getName()).snippet(place.getVicinity()));
Marker currentMarker = mMap.addMarker(new MarkerOptions().position());
}
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}`