2016-03-23 3 views
1

私は私が私の場所にカメラをしたいが、カメラは他の場所にあり、私はこの起こる 誰もがこれはこの状況が私の場所にGoogleマップカメラがないのはなぜですか?

が起こる理由を見つける私を助けることができる理由を知らない、フラグメントアンドロイドでGoogleマップのコードを持っています私の断片コード:

public class FragmentMap extends Fragment implements LocationListener { 

MapView mapView; 
GoogleMap map; 
double latitude; 
double longitude; 
public String bestProvider; 
public Criteria criteria; 


@Override 
public void onLocationChanged(Location location) { 

    // Getting latitude of the current location 
    latitude = location.getLatitude(); 

    // Getting longitude of the current location 
    longitude = location.getLongitude(); 

    // Creating a LatLng object for the current location 
    LatLng latLng = new LatLng(latitude, longitude); 

    // Showing the current location in Google Map 
    map.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

    // Zoom in the Google Map 
    map.animateCamera(CameraUpdateFactory.zoomTo(15)); 

} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 

} 

@Override 
public void onProviderEnabled(String provider) { 

} 

@Override 
public void onProviderDisabled(String provider) { 

} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.map_fragment, container, false); 

    // Gets the MapView from the XML layout and creates it 
    mapView = (MapView) v.findViewById(R.id.mapview); 
    mapView.onCreate(savedInstanceState); 

    // Gets to GoogleMap from the MapView and does initialization stuff 
    map = mapView.getMap(); 
    map.getUiSettings().setMyLocationButtonEnabled(false); 
    map.setMyLocationEnabled(true); 


    // Needs to call MapsInitializer before doing any CameraUpdateFactory calls 
    MapsInitializer.initialize(this.getActivity()); 

    // Updates the location and zoom of the MapView 
    map.setMyLocationEnabled(true); 

    criteria = new Criteria(); 
    bestProvider = String.valueOf(MainActivity.locationManager.getBestProvider(criteria, true)).toString(); 
    Location location = MainActivity.locationManager.getLastKnownLocation(bestProvider); 

    if (location != null) { 
     onLocationChanged(location); 
    } 

    CameraUpdate cameraUpdate = 
      CameraUpdateFactory 
        .newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16); 
    map.animateCamera(cameraUpdate); 

    return v; 
} 

@Override 
public void onResume() { 
    mapView.onResume(); 
    super.onResume(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    mapView.onDestroy(); 
} 

@Override 
public void onLowMemory() { 
    super.onLowMemory(); 
    mapView.onLowMemory(); 
} 

}

これは私の場所の宣言である:

public static LocationManager locationManager; 
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
+0

マップは現在どこにあり、map.invalidate()を呼び出しましたか? –

+0

gpsの位置は正しいですが、カメラが間違っています –

+0

ベストチュートリアルhttp://www.adavis.info/2014/09/android-location-updates-with.html – saeed

答えて

1

あなたonlocationchangeはする必要があります -

@Override 
public void onLocationChanged(Location location) { 

CameraPosition cameraPosition = new CameraPosition.Builder() 
       .target(new LatLng(location.getLatitude(), location.getLongitude())) 
       .zoom(14) 
       .build(); 

if (map != null) { 
    map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
} 

} 

これはあなたの現在の場所にカメラを移動します。

私はそれがあなたのために働くかどうか教えてください。

+0

まだカメラの位置を逃していない:私は長いと緯度をデバッグしようとしました –

+0

onlocationchangeは呼ばれていますか? –

+0

私は長いと緯度が正しい経度と緯度を取得する方法が間違っていることを知っていますか? –

関連する問題