我已经成功实现了代码,以便Google地图在我的应用程序中有效运行。我现在正在尝试实现locationManager并输出当前位置的纬度和经度值,但它正在崩溃。
MainActivity
package com.example.androidgooglemap;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.location.Location;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
public class MainActivity extends FragmentActivity {
public GoogleMap mMap;
LocationManager location;
private String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
location = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//mMap.setMyLocationEnabled(true);
//mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
@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;
}
/* Request updates at startup */
@Override
protected void onResume() {
super.onResume();
location.getLastKnownLocation(provider);
}
/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
}
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
System.out.println(String.valueOf(lat));
System.out.println(String.valueOf(lng));
}
我可以得到一些指导,因为我显然是业余爱好者!
答案 0 :(得分:0)
确切地知道您接收的异常将有所帮助。但我的猜测是你错过了
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
在你的清单中。