我想在Android谷歌地圖v2中獲取當前位置。這是我的代碼:如何在android google maps api V2中獲取當前位置?
package android.arin;
import java.util.List;
import location.Location;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import fish.Species;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MapScreen extends FragmentActivity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {
private Species selectedfish = null;
private GoogleMap map = null;
private LocationClient locationClient = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_screen);
setUpScreen();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
}
return super.onOptionsItemSelected(item);
}
private void setUpScreen() {
selectedfish = (Species) NavigationScreen.FishWhosOptionsClicked;
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
List<Location> locations = selectedfish.getLocations();
for(int i=0; i<locations.size(); i+=1) {
Location location = locations.get(i);
LatLng latlong = new LatLng(location.getLatitude(), location.getLongitude());
map.addMarker(new MarkerOptions()
.title(location.getAddress())
.snippet(location.getComment())
.position(latlong));
}
locationClient = new LocationClient(this, this, this);
}
@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
}
@Override
public void onConnected(Bundle connectionHint) {
android.location.Location location = locationClient.getLastLocation();
LatLng latlong = new LatLng(location.getLatitude(), location.getLongitude());
map.moveCamera(CameraUpdateFactory.newLatLngZoom(latlong, 10));
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
}
}
但它似乎沒有工作。它不會崩潰,但它不會移動到我所在的地方...
有誰知道如何解決這個問題?
謝謝。
是否啓用GPS? – Simas 2014-08-28 18:09:09
它未啓用。 – omega 2014-08-28 18:10:14
當我啓用它時,它仍然不起作用,但是當我點擊右上角的圖標時,它會在地圖上顯示一個藍色圓點。 – omega 2014-08-28 18:13:39