Sunday, January 27, 2013

Create MapFragment and GoogleMap using Java code

In previous exercises, MapFragment and SupportMapFragment are defined in XML of layout file. We can create it using Java code, without XML.

package com.example.androidmapsv2;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 final int RQS_GooglePlayServices = 1;
 private GoogleMap myMap;
 MapFragment myMapFragment;
 private static final String TAG_MYMAPFRAGMENT = "TAG_MyMapFragment";

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

  FragmentManager myFragmentManager = getFragmentManager();
  myMapFragment = 
    (MapFragment)myFragmentManager.findFragmentByTag(TAG_MYMAPFRAGMENT);
  
  if(myMapFragment == null){
   myMapFragment = MapFragment.newInstance();
   FragmentTransaction fragmentTransaction = myFragmentManager.beginTransaction();
   fragmentTransaction.add(android.R.id.content, myMapFragment, TAG_MYMAPFRAGMENT);
   fragmentTransaction.commit();
  }

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  
  switch (item.getItemId()) {
  case R.id.menu_legalnotices:
   String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
     getApplicationContext());
   AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
   LicenseDialog.setTitle("Legal Notices");
   LicenseDialog.setMessage(LicenseInfo);
   LicenseDialog.show();
   return true; 
  }
  return super.onOptionsItemSelected(item);
 }

 @Override
 protected void onResume() {
  super.onResume();
  
  int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
  if (resultCode == ConnectionResult.SUCCESS){
   Toast.makeText(getApplicationContext(), 
     "isGooglePlayServicesAvailable SUCCESS", 
     Toast.LENGTH_LONG).show();
   
   if(myMap == null){
    myMap = myMapFragment.getMap();
    if(myMap != null){
     myMap.setMyLocationEnabled(true);
     myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    }else{
     Toast.makeText(getApplicationContext(), 
       "cannot getMap!", 
       Toast.LENGTH_LONG).show();
    }
   }
   
  }else{
   GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices); 
  }
 }

}

Create MapFragment and GoogleMap using Java code


download filesDownload the files.

The series:
A simple example using Google Maps Android API v2, step by step.

1 comment:

oooO said...

very useful, thank u!