2015-08-16 54 views
-1

我的應用將在當前位置顯示標記。 GPS設置警報對話框將顯示GPS是否被禁用。啓用GPS並返回到應用程序後,地圖不會刷新。所以我必須強制停止我的應用程序並再次打開。如何刷新地圖活動?啓用GPS設置後刷新地圖以顯示當前位置

GoogleMap googleMap; 

GPSTracker gps; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 
    try { 
     ViewMap(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private void ViewMap() { 
    if(googleMap == null) { 
     SupportMapFragment supportMapFragment = 
       (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap); 
     googleMap = supportMapFragment.getMap(); 

     gps = new GPSTracker(MainActivity.this); 

     if(gps.canGetLocation()) { 

      double latitude = gps.getLatitude(); 
      double longitude = gps.getLongitude(); 

      LatLng latLng = new LatLng(latitude,longitude); 

      TextView locationTv = (TextView) findViewById(R.id.latlongLocation); 
      googleMap.addMarker(new MarkerOptions().position(latLng)); 
      googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
      googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 
      locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude); 
     } 
     else { 
      gps.showSettingsAlert(); 
     } 
    } 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    ViewMap(); 
} 
+1

覆蓋上的位置變化的方法。它會給更新的lat long值。 – Vid

回答

0

一旦你點擊打開你的GPS後刷新按鈕,然後獲取當前的經緯度長值和

MarkerOptions markerOption = new MarkerOptions().position(new LatLng(Double.valueOf(current lat, current long).title("Your title if required"); 

Marker marker = mGoogleMap.addMarker(markerOption); 
marker.showInfoWindow(); 

CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(current lat, current long).zoom(12).build(); 

mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
+0

對不起,我沒有刷新按鈕。我想我的地圖刷新自己。 – Dortianna

+0

然後從onLocationChange()覆蓋方法調用刷新方法併發送當前經緯度值。 – Vid

相關問題