0

我的地图代码似乎导致我的应用每秒闪烁/闪烁。有人看到有什么突出的吗?它用红色圆圈跟踪我去过的地方。它似乎也没有达到我设置的缩放级别。

谢谢

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


}
4

1 回答 1

1

在您当前的代码中,每次onlocationchanged调用您的方法时,您都会重新初始化地图片段。

将您的地图标记GoogleMap mMap;为类中所有方法都可以访问的全局变量。每当您的应用程序加载时仅初始化一次地图片段,因此将以下代码移动到您的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));
}
于 2013-07-23T06:03:19.973 回答