문제

I used google map v2. There is no error in logcat.

method for initializing map;

public class GoogleMapFragment extends Activity implements GooglePlayServicesClient.ConnectionCallbacks,
                                                       GooglePlayServicesClient.OnConnectionFailedListener,
                                                       LocationListener{

private GoogleMap googleMap;
private Location location;
private double latitude;
private double longitude;
private String provider;
private LocationManager locationManager;
private LocationClient mLocationClient;
private MapFragment mMapFragment;
private LatLng latLang;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.google_map);

    mLocationClient = new LocationClient(this, this, this);

     mMapFragment = MapFragment.newInstance();
     FragmentTransaction fragmentTransaction =
             getFragmentManager().beginTransaction();
     fragmentTransaction.add(R.id.map, mMapFragment);
     fragmentTransaction.commit();

    try {

        initializeLocation();

        initializeMap();

    } catch (Exception e) {
        e.printStackTrace();
    }

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.configuration, menu);
    return true;
}

private void initializeLocation(){

    // Get the location manager
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the locatioin provider -> use
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    location = locationManager.getLastKnownLocation(provider);

    latitude = location.getLatitude();
    longitude = location.getLongitude();


    if (location != null) {
        System.out.println("Provider " + provider + " has been selected.");
        onLocationChanged(location); 
    }
}

private void initializeMap() {
    if (googleMap == null) {

        googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);         

        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 14f)); 

        googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).icon(BitmapDescriptorFactory.defaultMarker()));

        googleMap.setMyLocationEnabled(true);
        googleMap.getUiSettings().setRotateGesturesEnabled(true);


        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
        }
    }
}

@Override
protected void onResume() {
    super.onResume();
    initializeLocation();
    initializeMap();
}

@Override
protected void onStart() {
    super.onStart();
    // Connect the client.
    mLocationClient.connect();
}

@Override
protected void onStop() {
    // If the client is connected
    if (mLocationClient.isConnected()) {
        mLocationClient.removeLocationUpdates(this);
    }
    mLocationClient.disconnect();
    super.onStop();
}

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

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    latitude = location.getLatitude();
    longitude = location.getLongitude();

    String msg = "Updated Location: " + "\n" +
            Double.toString(latitude) + "\n" +
            Double.toString(longitude);

    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

}

When I debug, lat and long values are correct. but even I change map type, I always get this map. Movecamera, addMarker, setMapType is not working.. And no error found in logcat.

enter image description here

도움이 되었습니까?

해결책

write the code of change location and move map camera in location changed method

@Override
    public void onLocationChanged(Location location) {

         latitude = location.getLatitude();
         longitude = location.getLongitude();       
         LatLng latLng = new LatLng(latitude, longitude);// Creating a LatLng object for the current location
         googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); // Showing the current location in Google Map
         googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top