简体   繁体   English

Android 谷歌地图移动相机

[英]Android Google Maps Move Camera

I've been implementing Google Maps with Android.我一直在用 Android 实现谷歌地图。 I'm using frame layout to view the map, and it works.我正在使用框架布局来查看地图,它的工作原理。 However, I wanna use the move camera in order to focus the map to a specific location.但是,我想使用移动相机将地图聚焦到特定位置。 However, i can't implement it using my code.但是,我无法使用我的代码实现它。

MainActivity.java主活动.java

public class MainActivity extends Activity {

private MainMapFragement mapFragment;
private HashMap<Marker, EventInfo> eventMarkerMap;
EventInfo firstEventInfo;
Marker firstMarker;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_activity);
    mapFragment = new MainMapFragement();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.add(R.id.map, mapFragment);
    ft.commit();

}

@Override
protected void onStart() {
    super.onStart();
    setUpEventSpots();
}



private void setUpEventSpots() {
    // I'm going to make 2 EventInfo objects and place them on the map
    eventMarkerMap = new HashMap<Marker, EventInfo>();
    EventInfo firstEventInfo = new EventInfo(new LatLng(7.190708000000000000, 125.455340999999980000), "Right now - event", new Date(), "Party");

    Marker firstMarker= mapFragment.placeMarker(firstEventInfo);


    eventMarkerMap.put(firstMarker, firstEventInfo);


    mapFragment.getMap().setInfoWindowAdapter(new InfoWindowAdapter() {

        private final View window = getLayoutInflater().inflate(R.layout.custom_window, null);

        @Override
        public View getInfoWindow(Marker marker) {
            EventInfo eventInfo = eventMarkerMap.get(marker);

            String title = marker.getTitle();
            TextView txtTitle = ((TextView) window.findViewById(R.id.txtInfoWindowTitle));
            if (title != null) {
                // Spannable string allows us to edit the formatting of the text.
                SpannableString titleText = new SpannableString(title);
                titleText.setSpan(new ForegroundColorSpan(Color.RED), 0, titleText.length(), 0);
                txtTitle.setText(titleText);
            } else {
                txtTitle.setText("");
            }

            TextView txtType = ((TextView) window.findViewById(R.id.txtInfoWindowEventType));
           if(eventInfo.getType() != null)
                txtType.setText(eventInfo.getType());

            return window;
        }

        @Override
        public View getInfoContents(Marker marker) {
            //this method is not called if getInfoWindow(Marker) does not return null
            return null;
        }
    });

}

}

EventInfo.java事件信息.java

public class EventInfo {

private LatLng latLong;
private String name;
private Date someDate;
private String type;

public EventInfo(LatLng latLong, String name, Date someDate, String type) {
    super();
    this.latLong = latLong;
    this.name = name;
    this.someDate = someDate;
    this.type = type;
}

public LatLng getLatLong() {
    return latLong;
}
public void setLatLong(LatLng latLong) {
    this.latLong = latLong;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Date getSomeDate() {
    return someDate;
}
public void setSomeDate(Date someDate) {
    this.someDate = someDate;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

}

MapFragment.java MapFragment.java

public class MainMapFragement extends MapFragment {

public Marker placeMarker(EventInfo eventInfo) {
    Marker m  = getMap().addMarker(new MarkerOptions()
        .position(eventInfo.getLatLong())
        .title(eventInfo.getName()));

    return m;
}
}

Please, how will you implement it with this code.请问,您将如何使用此代码实现它。

This should do it: 这应该这样做:

LatLng latLng = // whatever
float zoom = // whatever
mapfragment.getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));
// in 2021, animate camera will be a better choice :-) 
LatLng latLng = // whatever
float zoom = // whatever
mapfragment.getMap().animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));

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

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