johnsonjp34
johnsonjp34

Reputation: 3299

google maps api v2 flicker

My map code seems to cause my app to flicker/flash about every second. Anyone see anything that sticks out? It tracks where I have been with red circles. It doesn't seem to go to the zoom level I set either.

Thanks

 package com.direction.investor.farmsprayer;

import com.direction.investor.farmsprayer.R;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.LocationSource.OnLocationChangedListener;



import com.google.android.gms.maps.model.Circle;
 import com.google.android.gms.maps.model.CircleOptions;
 import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolygonOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.android.gms.maps.*;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.graphics.Color;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;

public class MainActivity extends Activity implements LocationListener{

Location myLocation;

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




// Get back the mutable Circle

Location myLocation;

LocationManager locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria cr = new Criteria();
String provider = locationmanager.getBestProvider(cr, true);
Location location = locationmanager.getLastKnownLocation(provider);

locationmanager.requestLocationUpdates(provider, 200, 0, (LocationListener) this);



}

@SuppressLint("NewApi")
@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
     GoogleMap mMap;
        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);


        mMap.setMyLocationEnabled(true);
        mMap.animateCamera(CameraUpdateFactory.zoomBy(17));
     mMap.moveCamera(CameraUpdateFactory.newLatLng((new LatLng(location.getLatitude(), location.getLongitude()))));


     CircleOptions circleOptions = new CircleOptions()
    .center(new LatLng(location.getLatitude(), location.getLongitude()));
    circleOptions.radius(3.048); // In meters
    circleOptions.fillColor(0xffff0000);
    circleOptions.strokeWidth(0);


     mMap.addCircle(circleOptions); 


}

Upvotes: 0

Views: 1228

Answers (1)

tony m
tony m

Reputation: 4779

In your current code, you are reinitialising the map fragment every time when your onlocationchanged method gets called.

Decalre your map GoogleMap mMap; as a global variable accessible to all methods in your class. Initialise your map fragment only one time whenever your app loads and so move the below code to your onResume() method so that its called only once. Also after setting mMap to your map fragment, do a null check ensure that the fragment is loaded, before doing other map initialisations.

     mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        if(mMap!=null) {
                mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                mMap.setMyLocationEnabled(true);
                mMap.animateCamera(CameraUpdateFactory.zoomBy(17));
}

Upvotes: 1

Related Questions