priya priya - 9 months ago 77
Android Question

getMap to getMapAsync covert error

I never try Google Map. This is a client project where they use

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


I have to improve their design to support the latest version of android that's way

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


it shows me an error so I try this link.

It say It can be fixed by updating the Google Play Services dependency to 6.1.+. .

Then I use

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


to support latest version of android.

Then it shows me error
getMap()
so I convert it to
getMapAsync()
but what are duing is
private static GoogleMap googlemap;
then assign it like this:

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


When i convert it to

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


It show me error. What to do

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();
}
});

}


They are calling googlemap and also adding marks. how i can implement it

@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;
.....}

Answer Source

This is a client project where they use compile project(':google-play-services_lib')

So, you replace that with this (notice the -maps)

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

Then it shows me error getMap() so I convert it to getMapAsync()

Which is correct, but

You really should not have a static map variable. If there are static variables all over the app, something is wrong in the design.

When i convert it to googlemap = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this); It show me error

Well, yes that'll error

1) getActivity() is not necessary. You already are in an Activity class

2) getSupportFragmentManager() will only be available if you extends AppCompatActivity (or FragmentActivity) in the class definition

3) getMapAsync(this) will not work unless you implements OnMapReadyCallback on the class definition.

If you do all that, then you will have something like

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);

}