android - 如何在 map 中心的谷歌地图中获取位置(经纬度/名称)

标签 android google-maps

伙计们,我在我的 Android 应用程序中实现了谷歌地图,并开始创建一个标记,我在 map 中间放置了一个标记图像。现在,我希望每当用户拖动 map 时,我都能得到 map 中心的位置(我放置图像的位置看起来像一个标记)。

我的 map Activity 是:

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

}

请帮助我这样做,谢谢:)

最佳答案

首先您可以获取您的 map 容器的引用,并通过除以 2 的宽度和高度来计算中心点。

View containerView=findViewById(R.id.mapContainer);

LatLng centerPoint=   this.map.getProjection().fromScreenLocation(new Point(((int)containerView.getWidth/2),((int)(containerView.getHeight/2)));

关于android - 如何在 map 中心的谷歌地图中获取位置(经纬度/名称),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32759018/

相关文章:

android - 录像同时回调onPreviewFrame

android - 我可以在 Android Studio 中运行没有 HAXM 的模拟器吗?

android - GPS 卫星设置在我的 android 设备中不可用。 HTC ONE 4.4 API 19

javascript - 获取 Google map XML 文件的最后一个节点

javascript - 如何控制谷歌地图标记的不透明度

java - Android Studio:从Github导入Project并重新构建/清理/与Gradle文件同步后的 “cannot resolve symbol R”

java - fragment 上的传感器管理器(如果可见则添加;如果不可见则删除)

android - 旋转手机时隐藏键盘

javascript - 自定义弹出谷歌地图

javascript - 带有可点击路线/信息窗口的 Google map v3 行车路线