Android Google Maps API OnLocationChanged 仅调用一次

2024-05-02

每当我的位置发生变化时,我都会尝试更新我的相机。然而,onLocationChanged 只被调用一次。当我随后在模拟器中发送新位置时,不会调用 onLocationChanged。

我已经尝试了几个小时了,但似乎无法修复它。

public class RouteActivity extends FragmentActivity implements OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {

private GoogleMap mMap;
ArrayList<LatLng> MarkerPoints;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker;
LocationRequest mLocationRequest;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.route);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        checkLocationPermission();
    }
    // Initializing
    MarkerPoints = new ArrayList<LatLng>();

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    //Initialize Google Play Services
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            buildGoogleApiClient();
            mMap.setMyLocationEnabled(true);
        }
    }
    else {
        buildGoogleApiClient();
        mMap.setMyLocationEnabled(true);
    }

    // Setting onclick event listener for the map
    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

        @Override
        public void onMapClick(LatLng point) {

            // Already two locations
            if (MarkerPoints.size() > 1) {
                MarkerPoints.clear();
                mMap.clear();
            }

            // Adding new item to the ArrayList
            MarkerPoints.add(point);

            // Creating MarkerOptions
            MarkerOptions options = new MarkerOptions();

            // Setting the position of the marker
            options.position(point);

            /**
             * For the start location, the color of marker is GREEN and
             * for the end location, the color of marker is RED.
             */
            if (MarkerPoints.size() == 1) {
                options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
            } else if (MarkerPoints.size() == 2) {
                options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
            }


            // Add new marker to the Google Map Android API V2
            mMap.addMarker(options);

            // Checks, whether start and end locations are captured
            if (MarkerPoints.size() >= 2) {
                LatLng origin = MarkerPoints.get(0);
                LatLng dest = MarkerPoints.get(1);

                // Getting URL to the Google Directions API
                String url = getUrl(origin, dest);
                Log.d("onMapClick", url.toString());
                FetchUrl FetchUrl = new FetchUrl();

                // Start downloading json data from Google Directions API
                FetchUrl.execute(url);
                //move map camera
                mMap.moveCamera(CameraUpdateFactory.newLatLng(origin));
                mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
            }

        }
    });

}

private String getUrl(LatLng origin, LatLng dest) {

    // Origin of route
    String str_origin = "origin=" + origin.latitude + "," + origin.longitude;

    // Destination of route
    String str_dest = "destination=" + dest.latitude + "," + dest.longitude;

    // Sensor enabled
    String sensor = "sensor=false";

    // Building the parameters to the web service
    String parameters = str_origin + "&" + str_dest + "&" + sensor;

    // Output format
    String output = "json";

    // Building the url to the web service
    String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters;


    return url;
}

/**
 * A method to download json data from url
 */
private String downloadUrl(String strUrl) throws IOException {
    String data = "";
    InputStream iStream = null;
    HttpURLConnection urlConnection = null;
    try {
        URL url = new URL(strUrl);

        // Creating an http connection to communicate with url
        urlConnection = (HttpURLConnection) url.openConnection();

        // Connecting to url
        urlConnection.connect();

        // Reading data from url
        iStream = urlConnection.getInputStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

        StringBuffer sb = new StringBuffer();

        String line = "";
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

        data = sb.toString();
        Log.d("downloadUrl", data.toString());
        br.close();

    } catch (Exception e) {
        Log.d("Exception", e.toString());
    } finally {
        iStream.close();
        urlConnection.disconnect();
    }
    return data;
}

// Fetches data from url passed
private class FetchUrl extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... url) {

        // For storing data from web service
        String data = "";

        try {
            // Fetching the data from web service
            data = downloadUrl(url[0]);
            Log.d("Background Task data", data.toString());
        } catch (Exception e) {
            Log.d("Background Task", e.toString());
        }
        return data;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        ParserTask parserTask = new ParserTask();

        // Invokes the thread for parsing the JSON data
        parserTask.execute(result);

    }
}

/**
 * A class to parse the Google Places in JSON format
 */
private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {

    // Parsing the data in non-ui thread
    @Override
    protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) {

        JSONObject jObject;
        List<List<HashMap<String, String>>> routes = null;

        try {
            jObject = new JSONObject(jsonData[0]);
            Log.d("ParserTask",jsonData[0].toString());
            DataParser parser = new DataParser();
            Log.d("ParserTask", parser.toString());

            // Starts parsing data
            routes = parser.parse(jObject);
            Log.d("ParserTask","Executing routes");
            Log.d("ParserTask",routes.toString());

        } catch (Exception e) {
            Log.d("ParserTask",e.toString());
            e.printStackTrace();
        }
        return routes;
    }

    // Executes in UI thread, after the parsing process
    @Override
    protected void onPostExecute(List<List<HashMap<String, String>>> result) {
        ArrayList<LatLng> points;
        PolylineOptions lineOptions = null;

        // Traversing through all the routes
        for (int i = 0; i < result.size(); i++) {
            points = new ArrayList<LatLng>();
            lineOptions = new PolylineOptions();

            // Fetching i-th route
            List<HashMap<String, String>> path = result.get(i);

            // Fetching all the points in i-th route
            for (int j = 0; j < path.size(); j++) {
                HashMap<String, String> point = path.get(j);

                double lat = Double.parseDouble(point.get("lat"));
                double lng = Double.parseDouble(point.get("lng"));
                LatLng position = new LatLng(lat, lng);

                points.add(position);
            }

            // Adding all the points in the route to LineOptions
            lineOptions.addAll(points);
            lineOptions.width(10);
            lineOptions.color(Color.BLUE);

            Log.d("onPostExecute","onPostExecute lineoptions decoded");

        }

        // Drawing polyline in the Google Map for the i-th route
        if(lineOptions != null) {
            mMap.addPolyline(lineOptions);
        }
        else {
            Log.d("onPostExecute","without Polylines drawn");
        }
    }
}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();
}

@Override
public void onConnected(Bundle bundle) {

    Log.i("LOCATION", "Connected!");

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        Log.i("LOCATION", "Request location updates are running");
    } else {
        Log.e("LOCATION", "Request location updates are NOT running!!! - No permissions gained");
    }
}

@Override
public void onConnectionSuspended(int i) {
    Log.i("LOCATION", "Connection suspended!");
}

@Override
public void onLocationChanged(Location location) {
    Log.i("LOCATION", "Location changed");

    mLastLocation = location;
    if (mCurrLocationMarker != null) {
        mCurrLocationMarker.remove();
    }

    //Place current location marker
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

    //move map camera
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

    //stop location updates
    if (mGoogleApiClient != null) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    }

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.i("LOCATION", "Connection failed");
}

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
public boolean checkLocationPermission(){
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        // Asking user if explanation is needed
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

            //Prompt the user once explanation has been shown
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);


        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);
        }
        return false;
    } else {
        return true;
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted. Do the
                // contacts-related task you need to do.
                if (ContextCompat.checkSelfPermission(this,
                        Manifest.permission.ACCESS_FINE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED) {

                    if (mGoogleApiClient == null) {
                        buildGoogleApiClient();
                    }
                    mMap.setMyLocationEnabled(true);
                }

            } else {

                // Permission denied, Disable the functionality that depends on this permission.
                Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
            }
            return;
        }

        // other 'case' lines to check for other permissions this app might request.
        // You can add here other case statements according to your requirement.
    }
}}

Debug output: Logs


我认为您需要使用位置管理器。有一个很好的教程here http://techlovejump.com/android-gps-location-manager-tutorial/.

网站上说的是您需要添加以下内容:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,
                2000,   
                10, this);

第一个参数是您正在使用的提供商(在本例中为 GPS 提供商)。第二个参数 (2000) 是每次更新之间的最短时间(以毫秒为单位)。第三个参数(10)是最小距离。最后一个参数是您的 LocationListener (this)。

实施也是一个好主意onProviderDisabled如果用户关闭了 GPS。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android Google Maps API OnLocationChanged 仅调用一次 的相关文章

随机推荐

  • Scrapy文件下载如何使用自定义文件名

    For my scrapy http doc scrapy org index html我目前正在使用的项目文件管道 https doc scrapy org en latest topics media pipeline html scr
  • Rails Beta 请求注册并提供社交媒体分享奖励

    我想构建一个简单的测试版请求注册页面 当用户尽可能多地共享应用程序的链接时 用户将获得更早的奖励 这样的解决方案可以在 特伦维网站 用户输入电子邮件 用户通过其独特的代码获得独特的链接 用户在每次注册时分享此链接 这对他来说是 1 管理方法
  • 对于没有固定/相对/绝对位置的元素,是否有 z-index 替代方案?

    我需要在更高的位置显示一个元素 z level 问题是 该元素位于带有 a 的 div 内 display flex and justify content space around 正常的z index属性不起作用 我认为这是因为该元素没
  • 为什么我在模拟器中看不到视频?

    我见过几个与此类似的问题 但我想确定一下 我无法在模拟器上运行视频 是否一致 有人在模拟器上成功运行视频吗 以下是我使用的代码 import android app Activity import android net Uri impor
  • 使用相同的 SqlConnection 对 SqlCommand.BeginExecuteNonQuery 进行多个并发调用

    我有一些可用的 C 代码 它使用 SqlConnection 创建临时表 例如 Foo 调用存储过程来填充这些临时表并将结果返回到 C 客户端 使用 C 对这些结果执行复杂的计算 并使用计算结果更新之前创建的临时表之一 由于整个过程中都会使
  • 防止IndexedDB请求错误取消事务

    我的意图 循环localStorage并将数据放入IndexedDB 如果发生某些已知错误 例如当键已存在时出现 ConstraintError 我想忽略这些特定错误 以便事务不会中止 当请求触发错误时 中止事务是默认行为 问题 我以为使用
  • 为什么 JavaScript 在不同浏览器中不一致?

    在花了无数个小时修复 JS 以使其跨浏览器兼容 主要是 IE 之后 我一直在思考以下问题 Why不是 JavaScript持续的跨浏览器 我的意思是 为什么 JS 不能像 Java 和 Flash 那样好呢 相反 我们必须求助于 jQuer
  • 避免调用成员变量的构造函数

    我有以下 C 类 Header File class A public A private B m B C m C cpp File A A m B 1 m B doSomething m B doMore m C C m B getSom
  • SQL CE Compact 3.5 表的标识列

    是否有一个查询可以针对 INFORMATION SCHEMA 或针对系统表编写 以确定某个列是否是 SQL CE 版本 3 5 中的标识列 Use 列属性 供您参考 a 列属性 Transact SQL http technet micro
  • 遍历内存编辑每个字节

    我正在编写汇编代码 提示用户输入一串小写字符 然后输出包含所有大写字符的相同字符串 我的想法是迭代从特定地址开始的字节 并从每个字节中减去 20H 将小写变为大写 直到到达具有特定值的字节 我对 Assembly 相当缺乏经验 所以我不确定
  • 尝试输入字符串时出现名称错误[重复]

    这个问题在这里已经有答案了 import pickle import os import time class Person def init self number address self number number self addr
  • 使用 cmake 和 opencv 对符号“gzclose”的未定义引用[关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 我尝试构建该项目 doppia https bitbucket org rodrigob doppia 但发生链接错误 我想这是一
  • java.lang.NullPointerException(无错误消息)APK构建

    Top level build file where you can add configuration options common to all sub projects modules buildscript repositories
  • 如何在 Mulesoft 中将睡眠设置为流程而不丢失消息负载

    我想插入脚本来延迟 Mulesoft 中的处理流程 我尝试在 groovy 中插入脚本 但丢失了消息有效负载 因此当我必须获取消息有效负载时 收到了空指针 我怎样才能不丢失消息有效负载 Thanks 如果您正在使用Groovy流程中的组件
  • 帮助我在 Python 中实现反向传播

    EDIT2 新的训练集 Inputs 0 0 0 0 0 0 1 0 0 0 2 0 0 0 3 0 0 0 4 0 1 0 0 0 1 0 1 0 1 0 2 0 1 0 3 0 1 0 4 0 2 0 0 0 2 0 1 0 2 0 2
  • 如何以编程方式移动 OpenLayers Vector?

    API 文档为OpenLayers Feature Vector http dev openlayers org apidocs files OpenLayers Feature Vector js html说 Vector 本身根本没有方
  • C# 数据库包装设计

    我正在为 C 设计一个数据库包装器 以下是我有两个选择 选项A class DBWrapper IDisposable private SqlConnection sqlConn public DBWrapper sqlConn new S
  • 将自定义字段添加到 Django 中的 auth_user 表

    目前我创建了另一个类 表名为MyAppUser我的自定义列 例如地址和电话号码 具有 Django 身份验证的外键User 像这样的东西 from django db import models from django contrib au
  • 使用 SvelteKit 托管 Firebase

    我已经创建了一个 svelte 应用程序并使用 SvelteKit 构建应用程序 一切正常 我尝试在 firebase 托管中部署此应用程序 但失败了 Sveltekit 生成生产版本 svelte kit文件夹 我试图改变public对象
  • Android Google Maps API OnLocationChanged 仅调用一次

    每当我的位置发生变化时 我都会尝试更新我的相机 然而 onLocationChanged 只被调用一次 当我随后在模拟器中发送新位置时 不会调用 onLocationChanged 我已经尝试了几个小时了 但似乎无法修复它 public c