請幫助我的代碼,setMyLocationEnabled(true)
提供了一個錯誤。使用setMyLocationEnabled時出錯
程序運行時關閉,我不能找到解決辦法:(
MainActivity.java文件:
public class MainActivity extends Activity {
Button btnDireccion;
TextView lblDireccion;
public static double lat = 0;
public static double lon = 0;
public static boolean checked = false; //Se ha chequeado el mapa.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
btnDireccion = (Button)findViewById(R.id.btnDireccion);
lblDireccion = (TextView)findViewById(R.id.lblDireccion);
}
public void mostrarMapa(View view){
Intent pantalla = new Intent(this, SeleccionarDireccion.class);
startActivity(pantalla);
}}
SeleccionarDireccion.java
public class SeleccionarDireccion extends AppCompatActivity {
private LatLng latlong = new LatLng (-6.760644,-79.863413);
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seleccionar_direccion);
MapFragment fMap = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
map.setMyLocationEnabled(true);
map = fMap.getMap();
map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
public void onMapLongClick(LatLng LL) {
MainActivity.lat = LL.latitude;
MainActivity.lon = LL.longitude;
regresar();
}
});
MainActivity.checked = true;
if (map!=null){
iniciarGPS();
}
}
private void iniciarGPS(){
map.addMarker(new MarkerOptions().position(latlong)
.title("Ubicacion actual"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(latlong,15));
}
public void regresar() {
Intent intent = new Intent(this, MainActivity.class);
intent .setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
this.finish();
} }
activity_seleccionar_direccion.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="com.androidmorefast.moden.appcapturardireccion.SeleccionarDireccion"
android:name="com.google.android.gms.maps.MapFragment" />
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.androidmorefast.moden.appcapturardireccion.MainActivity">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical" >
<Button
android:id="@+id/btnDireccion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dirección"
android:onClick="mostrarMapa" />
<TextView
android:id="@+id/lblDireccion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
的可能的複製[?我如何使用谷歌地圖和的LocationManager顯示在Android棉花糖當前位置(http://stackoverflow.com/questions/34582370/how-can-i -use-google-maps-and-locationmanager-to-show-current-location-on-androi) –
'setMyLocationEnabled(true)給出一個錯誤'你得到哪個錯誤? –