【发布时间】:2014-11-13 13:13:06
【问题描述】:
我正在使用谷歌地图。并且在 displayMap() 方法中 system.out.println 中显示当前位置,但经纬度显示为 0.0。我的问题是为什么不显示当前的经纬度?
我把我的代码发给你。请检查我的代码并建议我如何获取当前纬度和当前经度的值? 按钮 btnSubmit = null;
/** Use for Google Map */
private GoogleMap googleMap = null;
private SupportMapFragment supportMapFragment = null;
public double latitude=0, longitude=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(this);
try {
// Loading map
initializeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
/** Default Map is Initialize */
@SuppressLint("NewApi")
private void initializeMap() {
if (googleMap == null) {
// googleMap = ((MapFragment) getFragmentManager().findFragmentById(
// R.id.map)).getMap();
supportMapFragment = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map));
googleMap = supportMapFragment.getMap();
if (googleMap != null) {
displayMap();
}
}
}
/** Display The Current Location on Map */
private void displayMap() {
// Enable MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
// Get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Create a criteria Object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of best provider
String provider = locationManager.getBestProvider(criteria, true);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
// set Map Type
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// Get latitude of Current Location
latitude = myLocation.getLatitude();
System.out.println("latitude====="+latitude);
// Get longitude of Current Location
longitude = myLocation.getLongitude();
System.out.println("longitude====="+longitude);
// Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Show the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in Google map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
googleMap.addMarker(new MarkerOptions().position(
new LatLng(latitude, longitude)).title("You R Here..."));
}
@Override
protected void onResume() {
super.onResume();
initializeMap();
}
@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;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnSubmit:
Intent intent = new Intent(getBaseContext(), RegisterActivity.class);
intent.putExtra("latitude", latitude);
intent.putExtra("longitude", longitude);
startActivity(intent);
break;
default:
break;
}
}
}
我的xml文件是,
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
【问题讨论】:
-
你能做一个独立的小例子吗? stackoverflow.com/help/mcve
-
@Haresh Chhelana 它再次显示同样的问题。纬度和经度值为0。但地图当前显示在单元格中。
标签: android google-maps