下午好,我正在使用谷歌地图创建一个应用程序api v2已经准备就绪,但是地图的相机没有关注我当前的位置。例如,我绘制了一条从当前位置到另一条路线的路线,当沿着这条路线行驶时,蓝点代表我的位置,在地图上行走但地图静止不动。遵循代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapa);
v2GetRouteDirection = new GMapV2GetRouteDirection();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// Habilita minha localização no Google Maps
map.setMyLocationEnabled(true);
// Recupera o objeto LocationManager do System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//Verifica se o GPS está habilitado e se não estiver manda o usuário para as configurações
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
showGPSDisabledAlertToUser();
}
// Cria um objeto criteria para recuperar o provedor
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
// Pega o nome do melhor provedor
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
onLocationChanged(location);
}
}
//--------------------------- - - ------------------------------ -- -----------------------------
@Override
public void onLocationChanged(Location location) {
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng).zoom(17).build();
CameraUpdate update = CameraUpdateFactory.newCameraPosition(cameraPosition);
mMap.animateCamera(update);
// Habilita minha localização no Google Maps
map.setMyLocationEnabled(true);
map.getUiSettings().setCompassEnabled(true);
map.getUiSettings().setAllGesturesEnabled(true);
map.setTrafficEnabled(true);
markerOptions = new MarkerOptions();
fromPosition = new LatLng(latitude, longitude);
//--------------------------- - - ------------------------------ -- -----------------------------
double latitude1 = location.getLatitude();// Recuperando a latitude do destino(Ecoponto Antonio Eufrasio de Toledo)
double longitude1 = location.getLongitude();// Recuperando a longitude do destino(Ecoponto Antonio Eufrasio de Toledo)
latitude1 = -22.332009;
longitude1 = -49.082014;
toPosition = new LatLng(latitude1, longitude1);
//Calculando a distância da posição atual e do Ecoponto Antonio Eufrasio de Toledo
double theta = longitude - longitude1;
double dist = Math.sin(deg2rad(latitude)) * Math.sin(deg2rad(latitude1)) + Math.cos(deg2rad(latitude)) * Math.cos(deg2rad(latitude1)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
dist = dist * 1.609344;
//--------------------------- - - ------------------------------ -- -----------------------------
double latitude2 = location.getLatitude();// Recuperando a latitude do destino(Ecoponto Mary Dota)
double longitude2 = location.getLongitude();// Recuperando a longitude do destino(Ecoponto Mary Dota)
latitude2 = -22.291164;
longitude2 = -49.053223;
toPosition = new LatLng(latitude2, latitude2);
//Calculando a ditância da posição atual e do Ecoponto Mary Dota
double theta1 = longitude - longitude2;
double dist1 = Math.sin(deg2rad(latitude)) * Math.sin(deg2rad(latitude2)) + Math.cos(deg2rad(latitude)) * Math.cos(deg2rad(latitude2)) * Math.cos(deg2rad(theta1));
dist1 = Math.acos(dist1);
dist1 = rad2deg(dist1);
dist1 = dist1 * 60 * 1.1515;
dist1 = dist1 * 1.609344;
//--------------------------- - - ------------------------------ -- -----------------------------
double latitude3 = location.getLatitude();// Recuperando a latitude do destino(Ecoponto Jardim Redentor/Geisel)
double longitude3 = location.getLongitude();// Recuperando a longitude do destino(Ecoponto Jardim Redentor/Geisel)
latitude3 = -22.320793;
longitude3 = -49.033812;
toPosition = new LatLng(longitude3, longitude3);
//Calculando a ditância da posição atual e do Ecoponto Jardim Redentor/Geisel
double theta2 = longitude - longitude3;
double dist2 = Math.sin(deg2rad(latitude)) * Math.sin(deg2rad(latitude3)) + Math.cos(deg2rad(latitude)) * Math.cos(deg2rad(latitude3)) * Math.cos(deg2rad(theta2));
dist2 = Math.acos(dist2);
dist2 = rad2deg(dist2);
dist2 = dist2 * 60 * 1.1515;
dist2 = dist2 * 1.609344;
//--------------------------- - - ------------------------------ -- -----------------------------
double latitude4 = location.getLatitude();// Recuperando a latitude do destino(Ecoponto Pousada I)
double longitude4 = location.getLongitude();// Recuperando a longitude do destino(Ecoponto Pousada I)
latitude4 = -22.270191;
longitude4 = -49.069544;
toPosition = new LatLng(latitude4, longitude4);
//Calculando a ditância da posição atual e do Ecoponto Pousada I
double theta3 = longitude - longitude4;
double dist3 = Math.sin(deg2rad(latitude)) * Math.sin(deg2rad(latitude4)) + Math.cos(deg2rad(latitude)) * Math.cos(deg2rad(latitude4)) * Math.cos(deg2rad(theta3));
dist3 = Math.acos(dist3);
dist3 = rad2deg(dist3);
dist3 = dist3 * 60 * 1.1515;
dist3 = dist3 * 1.609344;
//--------------------------- - - ------------------------------ -- -----------------------------
double latitude5 = location.getLatitude();// Recuperando a latitude do destino(Ecoponto Edson Francisco da Silva)
double longitude5 = location.getLongitude();// Recuperando a longitude do destino(Ecoponto Edson Francisco da Silva)
latitude5 = -22.313669;
longitude5 = -49.121405;
toPosition = new LatLng(latitude5, longitude5);
//Calculando a ditância da posição atual e do Ecoponto Edson Francisco da Silva
double theta4 = longitude - longitude5;
double dist4 = Math.sin(deg2rad(latitude)) * Math.sin(deg2rad(latitude5)) + Math.cos(deg2rad(latitude)) * Math.cos(deg2rad(latitude5)) * Math.cos(deg2rad(theta4));
dist4 = Math.acos(dist4);
dist4 = rad2deg(dist4);
dist4 = dist4 * 60 * 1.1515;
dist4 = dist4 * 1.609344;
//--------------------------- - - ------------------------------ -- -----------------------------
double latitude6 = location.getLatitude();// Recuperando a latitude do destino(Ecoponto Parque Viaduto)
double longitude6 = location.getLongitude();// Recuperando a longitude do destino(Ecoponto Parque Viaduto)
latitude6 = -22.339716;
longitude6 = -49.108404;
toPosition = new LatLng(latitude6, longitude6);
//Calculando a ditância da posição atual e do Ecoponto Parque Viaduto
double theta5 = longitude - longitude6;
double dist5 = Math.sin(deg2rad(latitude)) * Math.sin(deg2rad(latitude6)) + Math.cos(deg2rad(latitude)) * Math.cos(deg2rad(latitude6)) * Math.cos(deg2rad(theta5));
dist5 = Math.acos(dist5);
dist5 = rad2deg(dist5);
dist5 = dist5 * 60 * 1.1515;
dist5 = dist5 * 1.609344;
//--------------------------- - - ------------------------------ -- -----------------------------
double latitude7 = location.getLatitude();// Recuperando a latitude do destino(Ecoponto Parque Bauru)
double longitude7 = location.getLongitude();// Recuperando a longitude do destino(Ecoponto Parque Bauru)
latitude7 = -22.317173;
longitude7 = -49.017617;
toPosition = new LatLng(latitude7, longitude7);
//Calculando a ditância da posição atual e do Ecoponto Parque Bauru
double theta6 = longitude - longitude7;
double dist6 = Math.sin(deg2rad(latitude)) * Math.sin(deg2rad(latitude7)) + Math.cos(deg2rad(latitude)) * Math.cos(deg2rad(latitude7)) * Math.cos(deg2rad(theta6));
dist6 = Math.acos(dist6);
dist6 = rad2deg(dist6);
dist6 = dist6 * 60 * 1.1515;
dist6 = dist6 * 1.609344;
//--------------------------- - - ------------------------------ -- -----------------------------
//Condição para saber qual Ecoponto é o mais próximo
if ((dist < dist1) & (dist < dist2) & (dist < dist3) & (dist < dist4) & (dist < dist5) & (dist < dist6)){
toPositiondist = new LatLng(-22.332009,-49.082014);
GetRouteTask getRoute = new GetRouteTask();
getRoute.execute();
}else if ((dist1 < dist) & (dist1 < dist2) & (dist1 < dist3) & (dist1 < dist4) & (dist1 < dist5) && (dist1 < dist6)) {
toPositiondist = new LatLng(-22.291164,-49.053223);
GetRouteTask getRoute = new GetRouteTask();
getRoute.execute();
}else if ((dist2 < dist) & (dist2 < dist1) & (dist2 < dist3) & (dist2 < dist4) & (dist2 < dist5) & (dist2 < dist6)) {
toPositiondist = new LatLng(-22.320793,-49.033812);
GetRouteTask getRoute = new GetRouteTask();
getRoute.execute();
}else if ((dist3 < dist) & (dist3 < dist1) & (dist3 < dist2) & (dist3 < dist4) & (dist3 < dist5) & (dist3 < dist6)) {
toPositiondist = new LatLng(-22.270191,-49.069544);
GetRouteTask getRoute = new GetRouteTask();
getRoute.execute();
}else if ((dist4 < dist) & (dist4 < dist1) & (dist4 < dist2) & (dist4 < dist3) & (dist4 < dist5) & (dist4 < dist6)) {
toPositiondist = new LatLng(-22.313669,-49.121405);
GetRouteTask getRoute = new GetRouteTask();
getRoute.execute();
}else if ((dist5 < dist) & (dist5 < dist1) & (dist5 < dist2) & (dist5 < dist3) & (dist5 < dist4) & (dist5 < dist6)) {
toPositiondist = new LatLng(-22.339716,-49.108404);
GetRouteTask getRoute = new GetRouteTask();
getRoute.execute();
}else if ((dist6 < dist) & (dist6 < dist1) & (dist6 < dist2) & (dist6 < dist3) & (dist6 < dist4) & (dist6 < dist5)) {
toPositiondist = new LatLng(-22.317173,-49.017617);
GetRouteTask getRoute = new GetRouteTask();
getRoute.execute();
}
}
@Override
protected void onResume() {
super.onResume();
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(provider, 35000, 1, this);
}
@Override
protected void onPause() {
super.onPause();
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.removeUpdates(this);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
private class GetRouteTask extends AsyncTask<String, Void, String> {
private ProgressDialog Dialog;
String response = "";
@Override
protected void onPreExecute() {
Dialog = new ProgressDialog(MapActivity.this);
Dialog.setMessage("Carregando rota...");
if(!isFinishing())
{
Dialog.show();
}
}
@Override
protected String doInBackground(String... urls) {
document = v2GetRouteDirection.getDocument(fromPosition, toPositiondist, GMapV2GetRouteDirection.MODE_DRIVING);
response = "Success";
return response;
}
@Override
protected void onPostExecute(String result) {
map.clear();
if(response.equalsIgnoreCase("Success")){
//Adiciona um ponto no mapa no local pré determinado
map.addMarker(new MarkerOptions().position(frameworkSystemLocation1).title("Ecoponto Antonio Eufrasio de Toledo").icon(BitmapDescriptorFactory.fromResource(R.drawable.ecoponto)));
map.addMarker(new MarkerOptions().position(frameworkSystemLocation2).title("Ecoponto Mary Dota").icon(BitmapDescriptorFactory.fromResource(R.drawable.ecoponto)));
map.addMarker(new MarkerOptions().position(frameworkSystemLocation3).title("Ecoponto Jardim Redentor/Geisel").icon(BitmapDescriptorFactory.fromResource(R.drawable.ecoponto)));
map.addMarker(new MarkerOptions().position(frameworkSystemLocation4).title("Ecoponto Pousada I").icon(BitmapDescriptorFactory.fromResource(R.drawable.ecoponto)));
map.addMarker(new MarkerOptions().position(frameworkSystemLocation5).title("Ecoponto Edson Francisco da Silva").icon(BitmapDescriptorFactory.fromResource(R.drawable.ecoponto)));
map.addMarker(new MarkerOptions().position(frameworkSystemLocation6).title("Ecoponto Parque Viaduto").icon(BitmapDescriptorFactory.fromResource(R.drawable.ecoponto)));
map.addMarker(new MarkerOptions().position(frameworkSystemLocation7).title("Ecoponto Parque Bauru").icon(BitmapDescriptorFactory.fromResource(R.drawable.ecoponto)));
ArrayList<LatLng> directionPoint = v2GetRouteDirection.getDirection(document);
PolylineOptions rectLine = new PolylineOptions().width(5).color(Color.GREEN);
for (int i = 0; i < directionPoint.size(); i++) {
rectLine.add(directionPoint.get(i));
}
//Adiciona uma rota
polyline = map.addPolyline(rectLine);
final Button btnClear = (Button) findViewById(R.id.button1);
btnClear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Recupera o objeto LocationManager do System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Cria um objeto criteria para recuperar o provedor
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
// Pega o nome do melhor provedor
String provider = locationManager.getBestProvider(criteria, true);
// Obtendo última localização
Location location = locationManager.getLastKnownLocation(provider);
if (polyline != null) {
polyline.remove();
map.clear();
// Habilita minha localização no Google Maps
map.setMyLocationEnabled(true);
onLocationChanged(location);
}
}
});
}
Dialog.dismiss();
}
}
@Override
protected void onStop() {
super.onStop();
finish();
}
private void showGPSDisabledAlertToUser(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("O GPS está desativado em seu dispositivo. Gostaria de ativá-lo?")
.setCancelable(false)
.setPositiveButton("Ativar GPS", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
}
});
alertDialogBuilder.setNegativeButton("Cancelar",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
}
答案 0 :(得分:0)
我猜你错过了收听改变位置的听众。请参阅以下代码段。
希望有所帮助。
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}