2015-05-08 75 views
1

我正在構建一個應用程序來跟蹤用戶培訓會議,即運行。Android谷歌地圖V2無法改變我的地圖內的片段

應用程序應該記錄用戶的位置,然後從結果中創建折線。

我已經使用android studio中的導航抽屜活動來構建我的應用程序。不過,我認爲這已經把我的地圖片段放入一個片段中。

我的問題是,我似乎無法爲地圖設置默認值。例如,如果我嘗試將setUpMap中的地圖類型設置爲衛星,則更改將不會生效。這不僅限於設置默認值,我也沒有獲取位置更新。

任何幫助,將不勝感激

這裏是我的地圖是如何調用:

package com.example.craig.runtrackerv3; 

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 


public class menu1_fragment extends Fragment { 

    View rootview; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     rootview = inflater.inflate(R.layout.activity_maps, container, false); 
     return rootview; 
    } 
} 

這裏是地圖對象:

package com.example.craig.runtrackerv3; 

import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 

import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class MapsActivity extends FragmentActivity { 

    private GoogleMap mMap; // Might be null if Google Play services APK is not available. 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     setUpMapIfNeeded(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     setUpMapIfNeeded(); 
    } 

    /** 
    * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly 
    * installed) and the map has not already been instantiated.. This will ensure that we only ever 
    * call {@link #setUpMap()} once when {@link #mMap} is not null. 
    * <p/> 
    * If it isn't installed {@link SupportMapFragment} (and 
    * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to 
    * install/update the Google Play services APK on their device. 
    * <p/> 
    * A user can return to this FragmentActivity after following the prompt and correctly 
    * installing/updating/enabling the Google Play services. Since the FragmentActivity may not 
    * have been completely destroyed during this process (it is likely that it would only be 
    * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this 
    * method in {@link #onResume()} to guarantee that it will be called. 
    */ 
    private void setUpMapIfNeeded() { 
     // Do a null check to confirm that we have not already instantiated the map. 
     if (mMap == null) { 
      // Try to obtain the map from the SupportMapFragment. 
      mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); 
      // Check if we were successful in obtaining the map. 
      if (mMap != null) { 
       setUpMap(); 
      } 
     } 
    } 

    /** 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, we 
    * just add a marker near Africa. 
    * <p/> 
    * This should only be called once and when we are sure that {@link #mMap} is not null. 
    */ 
    private void setUpMap() { 
     LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 

     Criteria criteria = new Criteria(); 
     criteria.setAccuracy(Criteria.ACCURACY_FINE); 

     String provider = locationManager.getBestProvider(criteria, true); 

     LocationListener locationListener = new LocationListener() { 

      @Override 
      public void onLocationChanged(Location location) { 
       showCurrentLocation(location); 
      } 
      @Override 
      public void onStatusChanged(String s, int i, Bundle bundle) { 
      } 
      @Override 
      public void onProviderEnabled(String s) { 
      } 
      @Override 
      public void onProviderDisabled(String s) { 
      } 
     }; 

     locationManager.requestLocationUpdates(provider, 2000, 0, locationListener); 

     // Getting initial Location 
     Location location = locationManager.getLastKnownLocation(provider); 
     // Show the initial location 
     if(location != null) 
     { 
      showCurrentLocation(location); 
     } 

     mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
    } 
    private void showCurrentLocation(Location location){ 

     mMap.clear(); 

     LatLng currentPosition = new LatLng(location.getLatitude(),location.getLongitude()); 

     mMap.addMarker(new MarkerOptions() 
       .position(currentPosition) 
       .snippet("Lat: " + location.getLatitude() + ", Lng: " + location.getLongitude()) 
         //.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_peterleow)) 
       .flat(true) 
       .title("I'm here!")); 

     // Zoom in, animating the camera. 
     mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, 18)); 
    } 
} 

這裏是地圖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.example.craig.runtrackerv3.MapsActivity" 
    android:name="com.google.android.gms.maps.SupportMapFragment" /> 

我的suspi錫安是我需要某種回調,但我不知道如何實現它。

任何幫助將不勝感激。

回答

0

你只是不初始化地圖正確,它緊密連接到活動的生命週期

GoogleMap mMap; 
    MapView map = (MapView) findViewById(R.id.map); 
    map.onCreate(mActivity.getStateBundle()); 
    map.onResume(); 
    map.getMapAsync(new OnMapReadyCallback() { 
     @Override 
     public void onMapReady(GoogleMap googleMap) { 
      mMap = googleMap; 
      initializeMap(googleMap, device); 
      } 
     }); 

,當你所有的調用來完成

map.onPause(); 
    map.onDestroy(); 
+0

這帶來了錯誤的載荷:不能解析符號deviceLocationItem – Craig

+0

這只是我的佈局,我已經更新的答案。 –

+0

我像這樣添加它: 公共類MapsActivity擴展FragmentActivity { //私有GoogleMap mMap; //如果Google Play服務APK不可用,則可能爲空。 GoogleMap mMap; MapView map =(MapView)findViewById(R.id.map); map.onCreate(mActivity.getStateBundle()); map.onResume(); map.getMapAsync(新OnMapReadyCallback(){ @Override 公共無效onMapReady(使用GoogleMap使用GoogleMap){ MMAP = GoogleMap的; initializeMap(GoogleMap的,設備); } }); 但即時獲取無法解析符號onCreate – Craig

0

您需要使用地圖碎片,而不是地圖活動。

public class MapaFragment extends Fragment implements OnMapClickListener { 

    MapView mapView; 
    GoogleMap map; 

    ..... 

    @Override 
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.fragment_map, container, false); 
     MapsInitializer.initialize(getActivity()); 
     mapView = (MapView) v.findViewById(R.id.mapaPrincipal); 
     mapView.onCreate(savedInstanceState); 
     map = mapView.getMap(); 
     if(map != null) { 
      map.setOnMapClickListener(this); 
     } 
     return v; 
    } 
}