谷歌地图Android的API V2:GoogleMap的为空为空、地图、Android、API

2023-09-05 04:48:25 作者:涐会陪着你 不离不弃﹌

我想使用的MapView类的GoogleMap显示探索,没有运气,因为大多数codeS的例子是使用MapFragment,我不想要的。

我使用谷歌地图Android的API V2。

起初,只是为了测试与这里从谷歌的例子,我成功让典型的法线贴图来显示。

 公共类POnlineMapView延伸活动{

@覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.online_map_activity);
    }
}
 

在code以上的作品完美这表明,一切都已经设置正确。

我现在想使用的MapView类来操作显示设置,如中心点,但似乎我得到一个空对象每次我尝试获取的GoogleMap对象。为什么会这样?

 公共类POnlineMapView延伸活动{

    私人MapView类myMapView;
    私人GoogleMap的地图;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        myMapView =新的图形页面(getApplicationContext());
        叠B = getIntent()getExtras()。
        双经度= b.getDouble(经度);
        双纬度= b.getDouble(纬);

        的setContentView(R.layout.online_map_activity);
        地图= myMapView.getMap();

        CameraUpdate中心= CameraUpdateFactory.newLatLng(新经纬度(纬度,经度));
        CameraUpdate变焦= CameraUpdateFactory.zoomTo(17);

        map.moveCamera(中心); //这给出了一个NullPointerException异常,可能是由于myMapView.getMap()方法?
        map.animateCamera(变焦);
    }
}
 

解决方案

谢谢,你终于给了我一个线索来初始化地图编程,我怎么这么笨监督吧:)

嗯,有时候没有选择使用MapFragment(或SupportMapFragment一样),即当你要使用的地图从片段!这其实是一个很常见的情况有标签(动作条),该模式是利用碎片工作时。所以,你有你自己的每个选项卡,在该片段要夸大你的布局,其中包含了MapFragment片段,片段的地图等瞧! - 好运气

滴滴国际化项目 Android 端演进

现在,根据MapView的规格实在是没办法说,使用的MapView不鼓励或去precated,为什么我不应该使用它?我不想在维护嵌套的片段黑客攻击,而无需从SDK的支持(甚至是最近的支撑LIB V13似乎有不支持的错误,并从布局嵌套的片段),因此,使用图形页面变成我到吻。

下面是我的CustomMapFragment我与布局通胀(允许复杂的布局)及嵌入式地图使用,欢迎您使用它。您可能还需要延长的支持-lib中,而不是SDK中的片段。

在onCreateView()方法膨胀的布局(只提供你自己的),并期望布局有ViewGroup中(RelativeLayout的,LinearLayout中,等等)在ID为mapViewHolder里的MapView会附着在布局创作。活动必须实现CustomMapFragment.Handler接口,否则抛出ClassCastException。

 进口android.app.Activity;
进口android.app.Fragment;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.Toast;

进口com.google.android.gms.common.GooglePlayServicesNotAvailableException;
进口com.google.android.gms.maps.GoogleMap;
进口com.google.android.gms.maps.MapView;
进口com.google.android.gms.maps.MapsInitializer;
进口ř;

公共类AppMapFragment扩展片段{
  / *
   *与活动互动
   * /
  公共静态接口处理器{
    无效onMa presume(GoogleMap的地图);
  }

  私人处理程序处理程序;
  私人图形页面图形页面;

  @覆盖
  公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    尝试 {
      //初始化明确地,因为我们正在与图形页面(不MapFragment)
      MapsInitializer.initialize(getActivity());
      this.mapView =新的图形页面(getActivity());
      this.mapView.onCreate(savedInstanceState);
    }赶上(GooglePlayServicesNotAvailableException E){
      Toast.makeText(getActivity(),请安装谷歌Play商店,然后重试一次,Toast.LENGTH_LONG).show();
      getActivity()完成()。
    }
  }

  @覆盖
  公共无效onResume(){
    super.onResume();
    this.mapView.onResume();
    this.handler.onMa presume(this.mapView.getMap());
  }

  @覆盖
  公共无效的onPause(){
    super.onPause();
    this.mapView.onPause();
  }

  @覆盖
  公共无效的onDestroy(){
    super.onDestroy();
    this.mapView.onDestroy();
  }

  @覆盖
  公共无效的onSaveInstanceState(包outState){
    super.onSaveInstanceState(outState);
    this.mapView.onSaveInstanceState(outState);
  }

  @覆盖
  公共无效onLowMemory(){
    super.onLowMemory();
    this.mapView.onLowMemory();
  }

  @覆盖
  公共无效onAttach(活动活动){
    super.onAttach(活动);
    尝试 {
      this.handler =(处理器)的活动;
    }赶上(ClassCastException异常E){
      抛出新的ClassCastException异常(你的行为已实现接口+ this.handler.getClass()的getName());
    }
  }

  公共AppMapFragment(){
  }

  @覆盖
  公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
    查看rootView = inflater.inflate(R.layout.fragment_results_map,集装箱,假);

    ((ViewGroup中)rootView.findViewById(R.id.mapViewHolder))addView(this.mapView)。

    返回rootView;
  }
}
 

I'm trying to explore using the MapView class for GoogleMap display, with no luck, as most codes examples are using MapFragment which I do not want.

I am using Google Maps Android API v2.

At first, just for testing with here from Google's example, I managed to get the typical normal map to display.

public class POnlineMapView extends Activity {

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

The code above works perfectly which show that everything has been set up properly.

I am now trying to use the MapView class to manipulate the display settings such as the center point, but it seems like I am obtaining a null object everytime I try to obtain the GoogleMap object. Why is this so?

public class POnlineMapView extends Activity {

    private MapView myMapView;
    private GoogleMap map;

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

        myMapView = new MapView(getApplicationContext());
        Bundle b = getIntent().getExtras();
        double longitude = b.getDouble("longitude");
        double latitude = b.getDouble("latitude");

        setContentView(R.layout.online_map_activity);
        map = myMapView.getMap();

        CameraUpdate center= CameraUpdateFactory.newLatLng(new LatLng(latitude,longitude));
        CameraUpdate zoom=CameraUpdateFactory.zoomTo(17);

        map.moveCamera(center); //this gives a NullPointerException, probably due to the myMapView.getMap() method?
        map.animateCamera(zoom);    
    }
}

解决方案

Thanks, you finally gave me a clue to initialize the Map programatically, how stupid I oversaw it :)

Well, sometimes there is no option to use MapFragment (or SupportMapFragment alike), i.e. when you want to use the map from a fragment! This is actually a very common case when working with tabs (ActionBar) where the pattern is to use fragments. So you've got your own fragment per tab and in that fragment for a map you want to inflate your layout which contains a fragment for the MapFragment, et voila - good luck!

Now, according to the MapView specs it is no way said that using of the MapView is discouraged or deprecated, so why should I not use it? I did not want hacks in maintaining nested fragments without having support from SDK ( even the recent support lib v13 seems to have bugs and nested fragments from layout are not supported ), so using MapView turned for me into KISS.

Below is my CustomMapFragment I use with layout inflation (allowing complex layout) and embedded map, you're welcome to use it. You may also want to extend the Fragment from support-lib rather than SDK.

The onCreateView() method inflates the layout (just provide you're own) and expects the layout to have viewgroup (relativelayout,linearlayout,etc) with id "mapViewHolder" where the mapView will be attached to upon layout creation. The activity has to implement CustomMapFragment.Handler interface, otherwise ClassCastException will be thrown.

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import R;

public class AppMapFragment extends Fragment {
  /*
   * to interact with activity
   */
  public static interface Handler {
    void onMapResume(GoogleMap map);
  }

  private Handler handler;
  private MapView mapView;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
      // initialize explicitely, since we're working with MapView (not MapFragment)
      MapsInitializer.initialize(getActivity());
      this.mapView = new MapView(getActivity());
      this.mapView.onCreate(savedInstanceState);
    } catch (GooglePlayServicesNotAvailableException e) {
      Toast.makeText(getActivity(), "Please install Google Play Store and retry again!", Toast.LENGTH_LONG).show();
      getActivity().finish();
    }
  }

  @Override
  public void onResume() {
    super.onResume();
    this.mapView.onResume();
    this.handler.onMapResume(this.mapView.getMap());
  }

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

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

  @Override
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    this.mapView.onSaveInstanceState(outState);
  }

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

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
      this.handler = (Handler) activity;
    } catch (ClassCastException e) {
      throw new ClassCastException("Your activity has to implement the interface " + this.handler.getClass().getName());
    }
  }

  public AppMapFragment() {
  }

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

    ((ViewGroup) rootView.findViewById(R.id.mapViewHolder)).addView(this.mapView);

    return rootView;
  }
}