2016-06-15 2 views
1

Google지도를 사용하여 이클립스에서 Android 용 애플리케이션을 개발했습니다. 문제는 내 현재 위치를 나타내는 파란색 점이 도시 외부의 도로에서 운전중인 도로와 항상 평행하게 나타나는 것입니다. 그러나 만일 당신이 도시의 내부에 있으면, 길의 꼭대기에 벌써있다.내 위치가 길 밖으로 나옵니다. Google map 이클립스

내가 응용 프로그램의 시작에 내 위치를 얻기 위해 이것을 사용하고 있습니다 :

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을 사용하십시오. 이전 오픈 소스 위치 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)); 

} 
} 
관련 문제