- 最後登錄
- 2023-3-18
- 在線時間
- 0 小時
- 註冊時間
- 2012-5-6
- 閱讀權限
- 20
- 精華
- 0
- UID
- 11305296
- 帖子
- 237
- 積分
- 198 點
- 潛水值
- 26390 米
| 本帖最後由 sank910021 於 2016-7-9 02:21 PM 編輯
如題,我希望按下一個button後在同個頁面顯示google map,但是我不管怎麼按我手機都會閃退...
有使用到ViewStub、Google Map API,這兩個以前都沒用過,試著照網路上的教學弄出來也不確定對不對....
還請各位幫我解答,謝謝!
map.xml
- <?xml version="1.0" encoding="utf-8"?>
- <fragment android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:name="com.google.android.gms.maps.MapFragment"
- android:id="@+id/map_fragment"
- xmlns:android="http://schemas.android.com/apk/res/android" />
複製代碼
activity_main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:orientation="vertical"
- 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.example.lincy.googlemaptest.MainActivity">
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <ViewStub
- android:id="@+id/vs_map"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout="@layout/map"/>
- </LinearLayout>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/map_button"
- android:id="@+id/map_button" />
複製代碼 MainActivity.java:
- import android.content.pm.PackageManager;
- import android.support.v4.app.ActivityCompat;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v4.app.FragmentActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewStub;
- import android.widget.Button;
- import android.app.Activity;
- import com.google.android.gms.maps.CameraUpdateFactory;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.MapFragment;
- import com.google.android.gms.maps.OnMapReadyCallback;
- import com.google.android.gms.maps.SupportMapFragment;
- import com.google.android.gms.maps.model.LatLng;
- import com.google.android.gms.maps.model.MarkerOptions;
- public class MainActivity extends Activity implements OnMapReadyCallback {
- GoogleMap map;
- Button map_button;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- this.map_button = (Button) findViewById(R.id.map_button);
- this.map_button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- ViewStub vs = (ViewStub) findViewById(R.id.vs_map);
- vs.inflate();
- }
- });
- }
- @Override
- public void onMapReady(GoogleMap googleMap) {
- LatLng sydney = new LatLng(-33.867, 151.206);
- if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- // TODO: Consider calling
- // ActivityCompat#requestPermissions
- // here to request the missing permissions, and then overriding
- // public void onRequestPermissionsResult(int requestCode, String[] permissions,
- // int[] grantResults)
- // to handle the case where the user grants the permission. See the documentation
- // for ActivityCompat#requestPermissions for more details.
- return;
- }
- map.setMyLocationEnabled(true);
- map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
- map.addMarker(new MarkerOptions()
- .title("Sydney")
- .snippet("The most populous city in Australia.")
- .position(sydney));
- }
- }
複製代碼
... |
|