伙计我在我的Android应用程序中实现了Google地图,并且创建了一个标记,我已经在地图中间放置了一个标记图像。现在我希望每当用户拖动地图时,我会得到地图中心的位置(我将图像看作标记)。
我的地图活动是:
public class MapActivity extends FragmentActivity implements LocationListener {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
TextView Title;
FrameLayout goback;
Location myLocation;
LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
setUpMapIfNeeded();
SupportMapFragment supportMapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = supportMapFragment.getMap();
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
//try
Title=(TextView)findViewById(R.id.map_title);
Title.setText(getIntent().getExtras().getString("Header"));
goback=(FrameLayout)findViewById(R.id.frame_layout);
setUpMapIfNeeded();
// mMap.setMyLocationEnabled(true);
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
}
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.marker);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(5));
CameraPosition ll=mMap.getCameraPosition();
Toast.makeText(getApplicationContext(),""+ll,Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
请帮助我这样做,谢谢:)
答案 0 :(得分:4)
首先,您可以获得地图容器的referance,并通过除以2宽度和高度来计算中心点。
View containerView=findViewById(R.id.mapContainer);
LatLng centerPoint= this.map.getProjection().fromScreenLocation(new Point(((int)containerView.getWidth/2),((int)(containerView.getHeight/2)));
答案 1 :(得分:2)
你可以通过这种方式获得中心:
mMap.getCameraPosition().target
其中mMap是您活动中的GoogleMap实例。这将返回一个LatLng对象,该对象基本上代表地图的中心。请注意,GeoPoint类不再可用。
根据http://developer.android.com/reference/com/google/android/gms/maps/model/CameraPosition.html
目标是“相机指向的位置”。 (用示例代码测试它,它对我来说没问题)
如果这有助于你,请告诉我。
干杯!
答案 2 :(得分:1)
您可以使用此方法
MapView.getProjection().fromPixels(x, y)
其中x是地图宽度的一半,y是高度的一半。这应该会返回一个坐标对象,该坐标对象将为您提供地图中心的经度和纬度
可以看到有关它的更多信息here