Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 谷歌地图的地图不跟踪运动中的当前位置_Android_Google Maps_Google Maps Android Api 2 - Fatal编程技术网

Android 谷歌地图的地图不跟踪运动中的当前位置

Android 谷歌地图的地图不跟踪运动中的当前位置,android,google-maps,google-maps-android-api-2,Android,Google Maps,Google Maps Android Api 2,下午好,我正在使用Google Maps api v2创建一个应用程序,该应用程序已基本就绪,但地图的摄像头没有跟随我当前的位置。例如,我绘制了一条从当前位置到另一个坐标的路线,当沿着这条路线走时,代表我位置的蓝点在地图上行走,但地图静止不动。遵循代码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R

下午好,我正在使用Google Maps 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);
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mapa);
V2GetroutedDirection=新的GMAPv2etroutedDirection();
getWindow().addFlags(WindowManager.LayoutParams.FLAG\u保持屏幕打开);
map=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
//Habilita minha localizaão无谷歌地图
map.setMyLocationEnabled(true);
//恢复o objeto位置管理器do系统服务位置\ U服务
LocationManager LocationManager=(LocationManager)getSystemService(LOCATION\u服务);
locationManager.isProviderEnabled(locationManager.GPS\U提供程序);
//验证是否有能力评估是否符合配置要求
如果(!locationManager.isProviderEnabled(locationManager.GPS\U提供程序)){
showGPSDisabledAlertToUser();
}
//Cria um objeto标准准复发医生
标准=新标准();
标准.设定准确度(标准.准确度/精细度);
//Pega o nome do melhor provedor
字符串提供程序=locationManager.getBestProvider(条件为true);
Location Location=locationManager.getLastKnownLocation(提供者);
如果(位置!=null){
onLocationChanged(位置);
}
}
//--------------------------- - - ------------------------------ -- -----------------------------
@凌驾
已更改位置上的公共无效(位置){
//正在获取当前位置的纬度
双纬度=location.getLatitude();
//正在获取当前位置的经度
double longitude=location.getLongitude();
//为当前位置创建LatLng对象
LatLng LatLng=新LatLng(纬度、经度);
CameraPosition CameraPosition=新建CameraPosition.Builder().target(latLng.zoom)(17.build();
CameraUpdate update=CameraUpdateFactory.newCameraPosition(cameraPosition);
mMap.animateCamera(更新);
//Habilita minha localizaão无谷歌地图
map.setMyLocationEnabled(true);
map.getUiSettings().setCompassEnabled(true);
map.getUiSettings().setAllGesturesEnabled(true);
map.setTrafficEnabled(true);
markerOptions=新的markerOptions();
fromPosition=新车床(纬度、经度);
//--------------------------- - - ------------------------------ -- -----------------------------
double latitude1=location.getLatitude();//返回一个目的地纬度(Ecoponto Antonio Eufrasio de Toledo)
double longitude1=location.getLongitude();//返回目的地的经度(Ecoponto Antonio Eufrasio de Toledo)
纬度1=-22.332009;
纵向1=-49.08 2014;
位置=新板条(横向1,纵向1);
//托莱多安东尼奥·尤弗雷西奥·德托莱多经济区
双θ=经度-经度1;
双区=数学sin(deg2rad(纬度))*数学sin(deg2rad(纬度))+数学cos(deg2rad(纬度))*数学cos(deg2rad(纬度))*数学cos(deg2rad(θ));
dist=数学acos(dist);
dist=rad2deg(dist);
dist=dist*60*1.1515;
dist=dist*1.609344;
//--------------------------- - - ------------------------------ -- -----------------------------
double-latitude2=location.getLatitude();//返回一个目的地纬度(Ecoponto Mary Dota)
double longitude2=location.getLongitude();//返回一个目的地经度(Ecoponto Mary Dota)
纬度2=-22.291164;
纵向2=-49.053223;
位置=新板条(纬度2,纬度2);
//圣玛丽·多塔学院
双θ1=经度-经度2;
双区1=Math.sin(deg2rad(纬度))*Math.sin(deg2rad(纬度))+Math.cos(deg2rad(纬度))*Math.cos(deg2rad(纬度))*Math.cos(deg2rad(θ1));
dist1=Math.acos(dist1);
dist1=rad2deg(dist1);
dist1=dist1*60*1.1515;
dist1=dist1*1.609344;
//--------------------------- - - ------------------------------ -- -----------------------------
double-latitude3=location.getLatitude();//返回一个目的地纬度(Ecoponto Jardim Redentor/Geisel)
double longitude3=location.getLongitude();//重复使用指定的经度(Ecoponto Jardim Redentor/Geisel)
纬度3=-22.320793;
纵向3=-49.033812;
位置=新板条(纵向3,纵向3);
//加尔各兰多市(Calculando a dit–ncia da posição atual e do Ecoponto Jardim Redentor/Geisel)
双θ2=经度-经度3;
双区2=Math.sin(deg2rad(纬度))*Math.sin(deg2rad(纬度3))+Math.cos(deg2rad(纬度))*Math.cos(deg2rad(纬度3))*Math.cos(deg2rad(θ2));
dist2=数学acos(dist2);
dist2=rad2deg(dist2);
dist2=dist2*60*1.1515;
dist2=dist2*1.609344;
//--------------------------- - - ------------------------------ -- -----------------------------
double-latitude4=location.getLatitude();//返回一个目的地纬度(Ecoponto Pousada I)
double longitude4=location.getLongitude();//重复使用一个目的地经度(Ecoponto Pousada I)
纬度4=-22.270191;
纵向4=-49.069544;
位置=新板条(横向4,纵向4);
//加尔各答和波萨达一世的经济地位
双θ3=经度-经度4;
双区3=Math.sin(deg2rad(纬度))*Math.sin(deg2rad(纬度))+Math.cos(deg2rad(纬度))*Math.cos(deg2rad(纬度))*Math.cos(deg2rad(θ3));
区3