0

我的地圖代碼似乎導致我的應用程序每秒閃爍/閃爍。任何人看到任何突出的東西?它用紅圈跟蹤我去過的地方。它似乎沒有去我設置的縮放級別。谷歌地圖api v2閃爍

感謝

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


} 

回答

1

在你現在的代碼,你每次當你onlocationchanged方法被調用時重新初始化地圖片段。

Decalre your map GoogleMap mMap; as a global variable accessible to all classes in your class。每當您的應用程序加載時,只初始化您的地圖片段一次,然後將以下代碼移至onResume()方法,以便僅調用一次。在將mMap設置爲您的地圖片段之後,在進行其他地圖初始化之前,執行空檢查以確保片段已加載。

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