如何在android中获取地图的中心点?

时间:2014-11-20 13:54:35

标签: android google-maps android-camera

我正在构建一个Android应用程序,用户通过长时间触摸Google地图获取latLng。

以下是代码:

    public class MainActivitytut extends FragmentActivity{

GoogleMap googleMap;
MarkerOptions markerOptions;
LatLng latLng;
private TextView myAddress;
String lat;
TextView txtmapinfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maintut);



    //onBackPressed();
    SupportMapFragment supportMapFragment = (SupportMapFragment) 
            getSupportFragmentManager().findFragmentById(R.id.map);

    // Getting a reference to the map
        googleMap = supportMapFragment.getMap();

        // Setting a click event handler for the map
        googleMap.setOnMapClickListener(new OnMapClickListener() {

            @Override
            public void onMapClick(LatLng arg0) {               

                // Getting the Latitude and Longitude of the touched location
                latLng = arg0;
                lat = latLng.toString();
            //  Toast.makeText(getApplicationContext(), lat,
        //             Toast.LENGTH_LONG).show();
            //  getMyLocationAddress();
                // Clears the previously touched position
                googleMap.clear();

                // Animating to the touched position                                
                googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));             

                // Creating a marker
                markerOptions = new MarkerOptions();

                // Setting the position for the marker
                markerOptions.position(latLng);                     

                // Placing a marker on the touched position
                googleMap.addMarker(markerOptions);

                // Adding Marker on the touched location with address
                new ReverseGeocodingTask(getBaseContext()).execute(latLng);             

            }
        });      

  }


protected void putExtra(String string, CharSequence text) {
    // TODO Auto-generated method stub

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is
    // present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;

}
}

我曾使用过lat = map.getCameraPosition().target;,当我为lat的值烘烤时,它正在烘烤0.0

我需要的是,在刷地图时,latlng会改变和吐司。

1 个答案:

答案 0 :(得分:1)

等到地图已加载,然后调用getCameraPosition:

mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
    @Override
    public void onMapLoaded() {
        LatLng pos = mMap.getCameraPosition().target;
        Log.e("", "Pos: " + pos.toString());
        mMap.addMarker(new MarkerOptions().position(pos));
    }
});