我只是想知道我们是否可以专注于Android应用程序中添加的标记。如果有,怎么样?或者是否有其他方法可以完成这项任务。
假设我使用以下代码添加了一个标记:
map.addMarker(new MarkerOptions()
.title(title)
.snippet(snippet)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
.position(pos)
);
如何使用最大变焦对焦此标记。如果我要再添加一个标记,它应该以这样的方式调整变焦(最大可能的变焦),以便两个标记一次显示。
这就是我想要做的事情,但它在第map.moveCamera(cu);
行失败了。
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.CancelableCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class LocateUserInMap extends FragmentActivity {
private GoogleMap map;
private List<Marker> markers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_locate_user_in_map);
markers = new ArrayList<Marker>();
Intent intent = getIntent();
double frndLatVal = intent.getDoubleExtra("frndLatVal", 0);
double frndLonVal = intent.getDoubleExtra("frndLonVal", 0);
LatLng myloc = new LatLng(19.115486500000000000, 72.905544299999970000);
LatLng friendloc = new LatLng(frndLatVal, frndLonVal);
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.google_map)).getMap();
addToMap(myloc, "My location", "From here 0M", "blue");
addToMap(friendloc, "Friend location", "From here 100M", "red");
showMarkersAtOnce();
}
private void showMarkersAtOnce() {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker m : markers) {
builder.include(m.getPosition());
}
LatLngBounds bounds = builder.build();
int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
map.moveCamera(cu);
/* map.getUiSettings().setScrollGesturesEnabled(false);
map.animateCamera(cu,
//CameraUpdateFactory.newLatLng(new LatLng(lat, lng))
new CancelableCallback()
{
@Override
public void onFinish()
{
map.getUiSettings().setScrollGesturesEnabled(true);
}
@Override
public void onCancel()
{
map.getUiSettings().setAllGesturesEnabled(true);
}
});*/
// map.animateCamera(CameraUpdateFactory.zoomBy(13));
}
private void addToMap(LatLng pos, String title, String snippet,
String markercolor) {
Marker localmarker = null;
if (markercolor == "blue")
{
localmarker = map.addMarker(new MarkerOptions()
.title(title)
.snippet(snippet)
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
.position(pos));
} else if (markercolor == "red")
{
localmarker = map.addMarker(new MarkerOptions()
.title(title)
.snippet(snippet)
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED))
.position(pos));
}
markers.add(localmarker);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.locate_user_in_map, menu);
return true;
}
}
和Logcat
08-04 13:23:32.975: W/EGL_emulation(764): eglSurfaceAttrib not implemented
08-04 13:23:42.787: D/dalvikvm(764): GC_CONCURRENT freed 169K, 3% free 11335K/11591K, paused 130ms+40ms, total 336ms
08-04 13:23:49.812: D/dalvikvm(764): GC_CONCURRENT freed 273K, 3% free 11497K/11847K, paused 121ms+124ms, total 480ms
08-04 13:23:49.972: E/ActivityThread(764): Failed to find provider info for com.google.settings
08-04 13:23:50.259: E/ActivityThread(764): Failed to find provider info for com.google.settings
08-04 13:23:54.223: D/dalvikvm(764): GC_CONCURRENT freed 262K, 3% free 11686K/12039K, paused 115ms+110ms, total 396ms
答案 0 :(得分:12)
您必须计算所有标记。这样做
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for each (Marker m : markers) {
builder.include(m.getPosition());
}
LatLngBounds bounds = builder.build();
现在获取CameraUpdateFactory
:
int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
最后将相机移动到这样的标记组上:
googleMap.moveCamera(cu);
或者如果你想要动画:
googleMap.animateCamera(cu);
答案 1 :(得分:1)
你可以这样做。你需要做的是迭代你的标记并找到最外面的坐标。
LatLngBounds bounds = new LatLngBounds(southWest, northEast);
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, MAP_PADDING));