Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.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_Eclipse_Google Maps - Fatal编程技术网

Android 我的位置点出现在谷歌地图月食的街道外

Android 我的位置点出现在谷歌地图月食的街道外,android,eclipse,google-maps,Android,Eclipse,Google Maps,我用谷歌地图在Eclipse中为Android开发了一个应用程序。问题是,在城市以外的道路上,指示我当前位置的蓝点始终与我驾驶的道路平行。但是,如果你在城市里,那么这个点已经在路的顶端了 我正在使用此功能在应用程序开始时获取我的位置: locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Crite

我用谷歌地图在Eclipse中为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();
                    }
                }
            }
        }
    }
我的问题是,为什么蓝色圆点看起来与街道平行,而不是位于街道上方?

使用。建议不要使用较旧的开源位置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));

}
}
使用。建议不要使用较旧的开源位置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));

}
}