1
我正在使用谷歌地圖V2獲取我當前的位置,以及行程 我的手機連接到互聯網和GPS工作,但結果令人失望 有些時候我有:「不可用」和同一時候,它gaves我在哪裏,我是連接不是當前 這裏laaast點的位置是我的代碼地理位置谷歌地圖v2
public class ItineraryActivity extends Activity {
private static final LatLng Event_place = new LatLng(33.59648, -7.664723);
private static final LatLng MOUNTAIN_VIEW = new LatLng(37.4, -122.1);
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
private GoogleMap map;
private LocationManager service;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mapevent);
findViewById(R.id.direction).setVisibility(View.INVISIBLE);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
Marker hamburg = map.addMarker(new MarkerOptions()
.position(Event_place).title("Our event")
.snippet("We are waiting for you .."));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(Event_place, 15));
/** Geo */
service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// Activer GPS
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
Log.e("eeee", "law 3lem");
} else {
Log.e("eeee", "law ");
}
// **
Criteria criteria = new Criteria();
String provider = service.getBestProvider(criteria, false);
Location location = service.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
new Routing(this, map, Color.BLACK).execute(
new LatLng(location.getLatitude(), location.getLongitude()),
Event_place);
} else {
Log.e("", "not available");
}
}
public void onLocationChanged(Location location) {
double lat = (double) (location.getLatitude());
double lng = (double) (location.getLongitude());
Log.e("lat + long", String.valueOf(lat) + "/" + String.valueOf(lng));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng),
14));
map.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("Vous êtes là")
.snippet("Nous comptons sur votre présence ...")
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
map.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);
}
}