我正在开发一个基于地图的应用程序,以查找用户当前位置附近的地方(如餐馆,医院等)。当我想点击餐馆时,它会在Google地图中正确加载用户当前位置附近的餐馆。但是当我在地图上放大2-3次或更多次以点击我所需的标记时,它会在一段时间内自动缩小。我只想控制缩小事件。我该怎么做才能避免在地图中自动缩小图片?
提前致谢!!
以下类在地图中显示标记和信息窗口。
public class PlacesDisplayTask extends AsyncTask<Object, Integer, List<HashMap<String, String>>> {
JSONObject googlePlacesJson;
GoogleMap googleMap;
private int grpId;
private String name;
Context mContext;
private Location location;
Map<LatLng, String> vicinities = new HashMap<LatLng, String>();
ImageView iv;
//Context getContext;
@Override
protected List<HashMap<String, String>> doInBackground(Object... inputObj) {
List<HashMap<String, String>> googlePlacesList = null;
Places placeJsonParser = new Places();
try {
googleMap = (GoogleMap) inputObj[0];
grpId = (Integer)inputObj[2];
mContext = (Context)inputObj[3];
location = (Location)inputObj[4];
name = (String)inputObj[5];
googlePlacesJson = new JSONObject((String) inputObj[1]);
googlePlacesList = placeJsonParser.parse(googlePlacesJson);
} catch (Exception e) {
Log.d("Exception", e.toString());
}
return googlePlacesList;
}
@Override
protected void onPostExecute(List<HashMap<String, String>> list) {
googleMap.clear();
if (list==null){
return;
}
for (int i = 0; i < list.size(); i++) {
final MarkerOptions markerOptions = new MarkerOptions();
final HashMap<String, String> googlePlace = list.get(i);
double lat = Double.parseDouble(googlePlace.get("lat"));
double lng = Double.parseDouble(googlePlace.get("lng"));
final LatLng latLng = new LatLng(lat, lng);
final BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(getIcon(grpId));
final String placeName = googlePlace.get("place_name");
String vicinity = googlePlace.get("vicinity");
vicinities.put(latLng, vicinity);
markerOptions.position(latLng)
.title(placeName)
.icon(icon);
googleMap.addMarker(markerOptions);
googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.custom_info_window, null);
TextView tv = (TextView)v.findViewById(R.id.tv1);
ImageView iv = (ImageView)v.findViewById(R.id.iv1);
tv.setText(marker.getTitle());
iv.setImageResource(R.drawable.rsz_info);
return v;
}
});
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
return false;
}
});
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Intent intent = new Intent(mContext, AddressGenerator.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("place_value", marker.getTitle());
intent.putExtra("vicinity_value", vicinities.get(marker.getPosition()));
intent.putExtra("lat_dest", String.valueOf(marker.getPosition().latitude));
intent.putExtra("lng_dest",String.valueOf( marker.getPosition().longitude));
intent.putExtra("lat_src", String.valueOf(location.getLatitude()));
intent.putExtra("lng_src", String.valueOf(location.getLongitude()));
intent.putExtra("name", name);
mContext.startActivity(intent);
}
});
}
}
private int getIcon(int groupId){
int icon = 0;
switch (groupId){
case 0:
icon = R.drawable.rsz_personal_care;
break;
case 1:
icon = R.drawable.rsz_atm;
break;
case 2:
icon = R.drawable.rsz_shopping;
break;
case 3:
icon = R.drawable.rsz_health;
break;
case 4:
icon = R.drawable.rsz_indicator;
break;
case 5:
icon = R.drawable.rsz_petrol;
break;
case 6:
icon = R.drawable.rsz_entertainment;
break;
case 7:
icon = R.drawable.rsz_bar;
break;
case 8:
icon = R.drawable.rsz_service_station;
break;
case 9:
icon = R.drawable.rsz_police;
break;
case 10:
icon = R.drawable.rsz_food;
break;
case 11:
icon = R.drawable.rsz_worship;
break;
}
return icon;
}
}
公共类MapsActivity扩展AppCompatActivity实现了LocationListener {
private GoogleMap googleMap;
private double latitude;
private double longitude;
private int PROXIMITY_RADIUS = 5000;
private String type;
private int grpId;
private String name;
private final static int REQUEST_CODE_FINE_LOCATION = 1;
private final static int REQUEST_CODE_COARSE_LOCATION = 2;
private static final int PERMISSION_REQUEST_CODE = 200;
private static final String GOOGLE_API_KEY = "AIzaSyBEZMt******Jhvct-OXni8mX*******w7GX4Q";
private View view;
LocationManager locationManager;
private Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
view = findViewById(R.id.map_id);
if(checkPermission()){
// Snackbar.make(view, "Permission already granted", Snackbar.LENGTH_LONG).show();
}
else
{
requestPermission();
}
if (!isGooglePlayServicesAvailable()) {
finish();
}
type = getIntent().getStringExtra("type");
grpId = Integer.parseInt(getIntent().getStringExtra("grpId"));
name = getIntent().getStringExtra("name");
googleMap = ((MapFragment) (getFragmentManager().findFragmentById(R.id.map))).getMap();
googleMap.setMyLocationEnabled(true);
LocationListener locationListener = new MyLocationListener();
GPSTracker gpsTracker = new GPSTracker(this);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
// getting GPS status
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled)
{
gpsTracker.showSettingsAlert();
}
else {
if (isGPSEnabled) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 20000, 0, locationListener);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 20000, 0, this);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.d("Network", "Network");
if (locationManager != null) {
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
if (googleMap != null) {
StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
googlePlacesUrl.append("location=" + latitude + "," + longitude);
googlePlacesUrl.append("&radius=" + PROXIMITY_RADIUS);
googlePlacesUrl.append("&types=" + type);
googlePlacesUrl.append("&sensor=true");
googlePlacesUrl.append("&key=" + GOOGLE_API_KEY);
GooglePlacesReadTask googlePlacesReadTask = new GooglePlacesReadTask();
Object[] toPass = new Object[6];
toPass[0] = googleMap;
toPass[1] = googlePlacesUrl.toString();
toPass[2] = grpId;
toPass[3] = getApplicationContext();
toPass[4] = location;
toPass[5] = name;
googlePlacesReadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, toPass);
}
}
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(12));
}
private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == status) {
return true;
} else
{
GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
return false;
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
private boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION);
int result1 = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION);
int result2 = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED
&& result2 == PackageManager.PERMISSION_GRANTED;
}
private void requestPermission() {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE);
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
private class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
}
答案 0 :(得分:0)
每次调用onLocationChanged
时,您都在移动(和设置动画)相机。
正如你所做的那样:
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(12));
第二行会覆盖第一行,效果是地图被缩放。
只需删除这些线条即可避免缩放效果。