2016-06-15 5 views
1

私はGoogleマップを使ってEclipseでAndroid用アプリケーションを開発しました。問題は、私の現在の位置を示す青い点が、都市の外の道路で、運転している道路と常に平行に現れることです。しかし、あなたが街の中にいる場合は、すでに道路の上にあるポイント。私の位置情報は通りの外に表示されます。

私はアプリの起動時に自分の位置を取得するために、これを使用しています:

googleMap.setMyLocationEnabled(true); 

をして、私は待機を開始:

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

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

が、私はマップに青い点を追加場所の変更:

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(); 
        } 
       } 
      } 
     } 
    } 

私の質問は、なぜ青い点がその上にではなく通りに平行に見えるのですか?

答えて

0

FusedLocationProviderAPIを使用してください。古いオープンソースのLocation APIを使用することをお勧めします。特にGoogleマップを既に使用しているため、既にGoogle Playサービスを使用しているためです。

位置リスナを設定し、各onLocationChanged()コールバックで現在の位置マーカーを更新するだけです。 1つの場所の更新のみが必要な場合は、最初のコールバックが返された後にコールバックの登録を解除します。

@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)); 

} 
} 
関連する問題