简体   繁体   English

Google Maps for Android v2-导入单独的Java文件

[英]Google Maps for Android v2 - Importing separate java file

For my school project we have to work with the Google Maps API v2 for Android (with Eclipse ADT). 对于我的学校项目,我们必须使用Android版Google Maps API v2(使用Eclipse ADT)。

We are creating an app that keeps track of your location during Running or Cycling exercises. 我们正在创建一个应用程序,可在跑步或骑自行车锻炼期间跟踪您的位置。 This means that the Fragment of the Map only has to be 50% off the screen. 这意味着地图片段仅需离开屏幕50%。 Because we would like to keep our code looking nice and clean, I wanted to create a separate .java file which stores the code for the Google Maps Fragment. 因为我们想保持代码的美观和整洁,所以我想创建一个单独的.java文件来存储Google Maps Fragment的代码。

The .java class for our "WORKOUT PAGE" called Workout.java has code for a stopwatch and some other info, and is already an FragmentActivity. 我们的“锻炼页面”的.java类称为Workout.java,具有用于秒表和一些其他信息的代码,并且已经是FragmentActivity。

How would I start off creating the GoogleMaps.java file (with or without an activity?) and how would I import this into Workout.java? 我将如何开始创建GoogleMaps.java文件(有或没有活动?),以及如何将其导入Workout.java?

Thanks! 谢谢!

Edit: Could I use something like this in Workout.java? 编辑:我可以在Workout.java中使用类似的东西吗?

GoogleMaps map = new GoogleMaps();

How would I be able to import this into my Fragment in Workout.java later on? 以后如何将其导入到Workout.java中的Fragment中?

You can extend SupportMapFragment and put your map related logic there (ex: marker management) 您可以扩展SupportMapFragment并将地图相关的逻辑放在此处(例如:标记管理)

public class CustomMapFragment extends SupportMapFragment {

private GoogleMap googleMap;
private List<Marker> markers = new ArrayList<Marker>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);
    googleMap = getMap();
    ViewUtils.initializeMargin(getActivity(), view);
    return view;
}

private void addMarkerToMap(LatLng latLng) {
    Marker marker = googleMap.addMarker(new MarkerOptions().position(latLng)
             .title("title")
             .snippet("snippet"));
    markers.add(marker);

}

/**
 * Adds a list of markers to the map.
 */
public void addMarkersToMap(List<LatLng> latLngs) {
    for (LatLng latLng : latLngs) {
        addMarkerToMap(latLng);
    }
}

/**
 * Clears all markers from the map.
 */
public void clearMarkers() {
    googleMap.clear();
    markers.clear();        
}

} }

You can then add that custom MapFragment in your layout (along with other fragments) 然后,您可以在布局中添加该自定义MapFragment(以及其他片段)

<fragment
    android:id="@+id/workout"
    android:name="com.foobar.WorkoutFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<fragment
    android:id="@+id/map"
    android:name="com.foobar.CustomMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

That way you seperate the pure map related code from the rest of your codebase. 这样,您就可以将纯地图相关的代码与其余代码库分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM