getMap to getMapAsync隐蔽错误

时间:2017-03-12 02:42:13

标签: java android google-maps

我从未尝试过谷歌地图。这是一个客户端项目,他们使用

compile project(':google-play-services_lib')

我必须改进他们的设计以支持最新版本的android

compile 'com.android.support:appcompat-v7:25.1.1'

它显示了一个错误,所以我试试link

它说可以通过将Google Play服务依赖关系更新为6.1。+。来修复。

然后我用

 compile 'com.google.android.gms:play-services:10.2.0'

支持最新版本的android。

然后它显示错误getMap(),因此我将其转换为getMapAsync(),但是什么是private static GoogleMap googlemap;,然后将其分配为:

googlemap=((MapFragment)getFragmentManager().findFragmentById(R.id.places_map)).getMap();

当我将其转换为

 googlemap = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);

它告诉我错误。怎么做

public class Frnt_mapActivity extends AppCompatActivity implements OnMapReadyCallback {//implements OnMapClickListener, OnMarkerDragListener  {


    public static double Shop_lat;
    public static double Shop_lng;
    public String Shop_title;
    public String Shop_address;
    public String Shop_icons;


    ProgressDialog mDialog;
    JSONObject jsonobject3;
    JSONArray jsonarray3;
    // Google Map


    //private static GoogleMap googlemap;

    public static EditText edit_search;
    public static Button btn_search;
    ArrayList<SearchBeams> getAllimgs;
    // Location
    ArrayList<String> world_locationlist;
    ArrayList<LocationBeams> worldlocation;
    // Looking for
    ArrayList<String> looking_forlist;
    ArrayList<LookingForBeams> world_looking_for;

    public static ArrayList<SearchBeams> searchdata_list;
    public static ArrayList<NormalSearchBeams> normlSearchList;
    public static String mid;
    public static String mpost_title;
    public static String mimage;
    public static List<String> imglist;

    ArrayList<All_products_lat_long> venueList;
    Spinner mySpinner2;

    public static ArrayList<SearchBeams> GetIMG;
    public static TextView txtlocation;
    public static TextView txtlookingfor;

    public static ArrayList<All_products_lat_long> mpItm;
    String vals;


    private int i;
    private Intent intent;
    HashMap<Marker, Integer> hashMap = new HashMap<Marker, Integer>();
    public static LatLng latlong;

    @Override
    public void onMapReady(GoogleMap map) {
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    }


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

        getActionBar().setDisplayShowHomeEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        mySpinner2 = (Spinner) findViewById(R.id.spin_looking_for);
        // Spinner adapter
        txtlookingfor = (TextView) findViewById(R.id.country);
        txtlocation = (TextView) findViewById(R.id.rank);



        SupportMapFragment mapFragment=getSupportFragmentManager().findFragmentById(R.id.places_map).getMapAsync(this);
        googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        googlemap.setMyLocationEnabled(true);
        googlemap.getUiSettings().setZoomControlsEnabled(true);
        googlemap.getUiSettings().setMyLocationButtonEnabled(true);
        googlemap.getUiSettings().setCompassEnabled(true);
        googlemap.getUiSettings().setRotateGesturesEnabled(true);
        googlemap.getUiSettings().setZoomGesturesEnabled(true);
        googlemap.setMyLocationEnabled(true);
        intent=new Intent(Frnt_mapActivity.this,MainActivity.class);
        googlemap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

            @Override
            public void onInfoWindowClick(Marker arg0) {
                 i=hashMap.get(arg0);
                intent.putExtra("PRODUCT_ID", venueList.get(i).getId());
                    startActivity(intent);
                    //overridePendingTransition( R.anim.slide_right, R.anim.slide_left );
            }
        });


        new Frnt_mIcons_Activity().execute();
        new LocationList().execute();
        new LookingForList().execute();

        edit_search = (EditText)findViewById(R.id.txt_search);
        btn_search = (Button)findViewById(R.id.btn_search);
        btn_search.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String text;
                text=edit_search.getText().toString();
            //  text!=null &&
                if(text.equals("") && text.startsWith(" ")==false){

                    new GetSearch_data().execute(); 
                    /*new Get_normal_search().execute();
                    edit_search.setText("");*/
                }else

                    new Get_normal_search().execute();
                edit_search.setText("");

                    //new GetSearch_data().execute();
            }
        });

    }

他们正在调用googlemap并添加标记。我该如何实现呢

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        try{
            if(bd!=null){
                Marker marker=googlemap.addMarker(new MarkerOptions()
                        .position(new LatLng(Double.parseDouble(venue.getLatitude()), Double.parseDouble(venue.getLongitude())))
                        .title(venue.getPost_title())
                        .icon(bd));
                hashMap.put(marker, k);
            }

        }catch (Exception e){
            e.printStackTrace();
        }


    }



                switch (Integer.parseInt(venueList.get(k).getId()))
                {
                    case 5689 :
                        Marker marker=googlemap.addMarker(new MarkerOptions()
                                .position(new LatLng(la, ln))
                                .title(venueList.get(k).getPost_title())
                                .icon(BitmapDescriptorFactory.fromResource(R.drawable.fruitandveg)));
                        hashMap.put(marker, k);
                        break;
                    case 5779 :
                        Marker marker1=googlemap.addMarker(new MarkerOptions()
                                .position(new LatLng(la, ln))
                                .title(venueList.get(k).getPost_title())
                                .icon(BitmapDescriptorFactory.fromResource(R.drawable.coffee)));
                        hashMap.put(marker1, k);
                        break;
.....}

1 个答案:

答案 0 :(得分:1)

  

这是一个客户端项目,他们使用编译项目(&#39;:google-play-services_lib&#39;)

所以,你用它替换它(注意-maps

compile 'com.google.android.gms:play-services-maps:10.2.0'
  

然后它显示错误getMap()所以我将其转换为getMapAsync()

哪个是正确的,但是

你真的不应该有一个static地图变量。如果应用程序中存在static个变量,则设计中出现问题。

  

当我将其转换为googlemap = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);时显示错误

嗯,是的,错误

1)getActivity()不是必需的。您已经在Activity班级

2)只有getSupportFragmentManager()(或extends AppCompatActivity)在课程定义

中才能使用FragmentActivity 除非你getMapAsync(this)在类定义上,否则{3>} implements OnMapReadyCallback将无效。

如果你做了所有这些,那么你会有类似

的东西
import com.google.android.gms.maps.OnMapReadyCallback;

public class YourActivity extends AppCompatActivity 
    implements OnMapReadyCallback {

    private void SupportMapFragment mMapFragment;
    // private GoogleMap mMap; // Commented so you don't use in onCreate

    @Override
    public void onMapReady(GoogleMap map) {
        // this.mMap = map;
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        // TODO: call other map setup methods
    }

    // Inside onCreate
        mMapFragment = getSupportFragmentManager().findFragmentById(R.id.map);
        mMapFragment.getMapAsync(this);

        // Don't do this in onCreate
        //  googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

}