2014-03-04 33 views
0

那么,下面是我的例子,我在Android中有一个主要的活动,我创建了一个工具栏,并且有一个叫做位置的子函数,我将调用一些位置函数这里。 它被编码在一个片段中,因为我需要在单击工具栏中的图标后进入子函数时创建一个片段。如何把谷歌地图功能放到一个片段中

我已经得到如何显示一个活动内的谷歌地图。但是我没有在片段中显示它。 我能做的只是在片段内调用onActivityCreated。但是,虽然程序工作正常,但布局将省略工具栏(我不擅长英语,如果你不明白我在说什么,只是问我的细节:))

所以呢任何人都可以帮助我弄清楚如何调用一些谷歌位置服务,例如查找png和lat,或者在片段中显示谷歌地图?我的片段代码如下:

LocationFragment.java:

public class LocationFragment extends MapFragment { 

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

回答

1

片段类将是:

public class MyFragment extends Fragment { 

    private MapView mapView; 
    private GoogleMap map; 
    private Bundle bundle; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.fragment_map, container, false); 

     // initialize it to avoid a NPE 
     try { 
      MapsInitializer.initialize(getActivity()); 
     } catch (GooglePlayServicesNotAvailableException e) { } 

     mapView = (MapView) v.findViewById(R.id.map); 
     mapView.onCreate(bundle); 

     // check if the map is null 
     if (map == null) { 
      map = ((MapView) v.findViewById(R.id.map)).getMap(); 
     } 

     // add a marker 
     map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Here!")); 

     return v; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     bundle = savedInstanceState; 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     mapView.onResume(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     mapView.onPause(); 
    } 

    @Override 
    public void onDestroy() { 
     mapView.onDestroy(); 
     super.onDestroy(); 
    } 

    @Override 
    public void onLowMemory() { 
     super.onLowMemory(); 
     mapView.onLowMemory(); 
    } 
} 

最后,你的布局fragment_map

<com.google.android.gms.maps.MapView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/map" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 

他我们去在片段内显示你的地图!请参阅a little post以及the reference来自Google。

对于用户的位置,需要一点阅读才能知道更多关于它的信息:Location Strategies(仔细阅读,一切都在那里)。你需要一个Location Manager和一个Location Listener
首先,你需要这样实现:

// The implement 
implements LocationListener { [...] } 

有了这个监听器,你就能够拥有并通过GPS或网络更新设备的位置。不要忘了测试设备的服务的状态,这样的事情:

// Getting Google Play availability status 
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); 

下面这段代码是这个例子中的一个片段内的当前位置:

public class LocationFragment extends Fragment implements LocationListener { 

    private Marker marker; 
    private LocationManager locationManager; 

    static final LatLng NEWYORK = new LatLng(40.75, -74.03); 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     super.onCreateView(inflater, container, savedInstanceState); 
     View v = inflater.inflate(R.layout.location_layout, container, false); 

     // ... 
     if (map == null) { 
      map = ((MapView) v.findViewById(R.id.map)).getMap(); 
     } 
     // ... 
     if (map != null) initMap(); 

     locationManager = (LocationManager) getActivity().getApplicationContext().getSystemService(Context.LOCATION_SERVICE); 
     if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
      getGPS(); 
     } else { 
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10, this);  
     } 
     // ... 

     return v; 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     getGPS(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     outGPS(); 
    } 

    public void getGPS() { 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); 
    } 

    public void outGPS() { 
     locationManager.removeUpdates(this); 
    } 

    @Override 
    public void onLocationChanged(final Location location) { 
     // get the latitude and the longitude 
     final StringBuilder msg = new StringBuilder("lat : "); 
     msg.append(location.getLatitude()); 
     msg.append("; lng : "); 
     msg.append(location.getLongitude()); 
     // display it on a toast 
     Toast.makeText(getActivity().getApplicationContext(), msg.toString(), Toast.LENGTH_SHORT).show(); 

     // final latlng with these above to update the position on a marker 
     final LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
     gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 1)); 
     marker.setPosition(latLng); 
     // ... 
    } 

    @Override 
    public void onProviderDisabled(final String provider) { 
     if("gps".equals(provider)) { 
      desabonnementGPS(); 
     } 
    } 

    @Override 
    public void onProviderEnabled(final String provider) { 
     if("gps".equals(provider)) { 
      abonnementGPS(); 
     } 
    } 

    @Override 
    public void onStatusChanged(final String provider, final int status, final Bundle extras) { } 

    private void initMap() { 
     gMap.addMarker(new MarkerOptions().title("New-York").position(NEWYORK)); 
     marker = gMap.addMarker(new MarkerOptions().title("I am here!").position(new LatLng(0, 0))); 
    } 
} 

然后创建经理,然后使用onLocationChanged方法更新当前位置。有一些很好的教程:a tutorial by Vogella(这可能是帮助)和another tutorial(也可以帮助你)。

您对this blog不少小费。非常简单,非常全面。

注意2:不要忘记在您的清单中添加权限。

我希望,我回答清楚,这将是有益的。
快乐编码!

+0

嘿弗洛,谢谢你的建议。 但我想创建一个片段内的地图视图,说我已创建一个片段类,所以它应该是一个onCreateView方法,而不是的onCreate,因为它是一个片段并不是一项活动... –

+0

酷!我看到了,我根据自己的需要更新了答案。让我知道这是否有帮助。 – Fllo

+0

谢谢你!有用!! –