我正在尝试获取地图中心的经度和纬度(google maps api v2),以便在按下按钮的文本视图中显示。但它一直在崩溃。我尝试了几件事,这对我来说是最好的,但是一旦按下按钮就崩溃了:
我认为它崩溃了这一行:MapView mapView =(MapView)findViewById(R.id.map); (因为我试图删除onClick中的所有其他内容,它仍然会出现问题)
爪哇:
public class Main extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
final GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
map.setMyLocationEnabled(true);
final TextView latitudeTV = (TextView) findViewById(R.id.latitude);
final TextView longitudeTV = (TextView) findViewById(R.id.longitude);
Button buttonCoor = (Button) findViewById(R.id.getCoor);
buttonCoor.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MapView mapView = (MapView)findViewById(R.id.map);
GeoPoint mapCenter = mapView.getMapCenter();
int latInt = mapCenter.getLatitudeE6();
int lonInt = mapCenter.getLongitudeE6();
latitudeTV.setText(latInt);
longitudeTV.setText(lonInt);
}
});
}
我的代码出了什么问题?
由于api密钥,我无法在模拟器上运行它,因此无法获取任何logcat信息。
谢谢
更新:
我也尝试了这个,但结果相同(按下按钮时崩溃)
MapView mapView = (MapView)findViewById(R.id.map);
GeoPoint mapCenter = mapView.getProjection().fromPixels(
mapView.getWidth()/2,
mapView.getHeight()/2);
final int lat = mapCenter.getLatitudeE6();
final int lon = mapCenter.getLongitudeE6();
答案 0 :(得分:0)
final GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
...
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Point point = new Point(width / 2, height / 2);
LatLng latLng = map.getProjection().fromScreenLocation(point);
latitudeTV.setText(String.valueOf(latLng));
感谢这个家伙的教程:
http://patesush.blogspot.dk/2012/12/how-to-create-app-for-google-map-v2-in.html