【问题标题】:my location point appears out of the street google maps eclipse我的位置点出现在街外谷歌地图日食
【发布时间】:2016-10-17 02:47:55
【问题描述】:

我在 Eclipse 中使用 Google 地图开发了一个适用于 Android 的应用程序。问题是指示我当前位置的蓝点总是与我正在驾驶的道路平行,在城市以外的道路上。但是,如果您在城市内,则该点已经在路的顶部。

我正在使用它在应用程序启动时获取我的位置:

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

        Criteria criteria = new Criteria();
        Location myLocation = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));

然后我将蓝点添加到地图上:

googleMap.setMyLocationEnabled(true);

然后我开始监听位置变化:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, time, 1, this);

我的位置变更功能:

@Override
    public void onLocationChanged(Location location) {
        if (location != null) {

            Lat1 = location.getLatitude();
            Long1 = location.getLongitude();

            if (Lat1 != Lat || Long1 != Long) {
                Lat = location.getLatitude();
                Long = location.getLongitude();
                if (startNav == true) {
                    googleMap.animateCamera(CameraUpdateFactory
                            .newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 17));
                    b = new LatLng(Lat, Long);
                    if (a != null) {
                        String urlTopass = makeURL(b.latitude, b.longitude, a.latitude, a.longitude);
                        new connectAsyncTask(urlTopass).execute();
                    }
                }
            }
        }
    }

我的问题是为什么蓝点看起来与街道平行,而不是在街道上方?

【问题讨论】:

    标签: android eclipse google-maps


    【解决方案1】:

    使用FusedLocationProviderAPI。建议不要使用较旧的开源位置 API,尤其是因为您已经在使用 Google 地图,因此您已经在使用 Google Play 服务。

    只需设置一个位置监听器,并在每个 onLocationChanged() 回调中更新您当前的位置标记。如果您只想更新一个位置,只需在第一个回调返回后取消注册回调即可。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    @Override
    protected void onResume() {
        super.onResume();
    
        if (mGoogleApiClient == null || !mGoogleApiClient.isConnected()){
            buildGoogleApiClient();
            mGoogleApiClient.connect();
        }
    
        if (map == null) {
            MapFragment mapFragment = (MapFragment) getFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
        }
    }
    
    @Override
    public void onMapReady(GoogleMap retMap) {
        map = retMap;
        setUpMap();
    }
    
    public void setUpMap(){
        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        map.setMyLocationEnabled(true);
    }
    
    @Override
    protected void onPause(){
        super.onPause();
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }
    }
    
    protected synchronized void buildGoogleApiClient() {
        Toast.makeText(this, "buildGoogleApiClient", Toast.LENGTH_SHORT).show();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }
    
    @Override
    public void onConnected(Bundle bundle) {
        Toast.makeText(this,"onConnected", Toast.LENGTH_SHORT).show();
    
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        //mLocationRequest.setSmallestDisplacement(0.1F);
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }
    
    @Override
    public void onConnectionSuspended(int i) {
    }
    
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
    }
    
    @Override
    public void onLocationChanged(Location location) {
        mLastLocation = location;
    
        //remove previous current location Marker
        if (marker != null){
            marker.remove();
        }
    
        double dLatitude = mLastLocation.getLatitude();
        double dLongitude = mLastLocation.getLongitude();
        marker = map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude))
                .title("My Location").icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));
    
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 2014-03-21
      • 1970-01-01
      • 2015-10-09
      相关资源
      最近更新 更多