Post Top Ad

July 22, 2014

Android Google Maps Chapter 2

This is the second chapter about creating a android google map with google map api v2




In the previous chapter i have explained about main layout file and the manifest file to load a map inside the emulator. Now this chapter will explain about the main activity of the map.

public class MainActivity extends FragmentActivity {
   
    private static final int GPS_ERRORDIALOG_REQUEST = 9001;
    private GoogleMap mMap;
    private static final double SEATTLE_LAT = 6.939378, // if you want to enable satellite views
            SEATTLE_LNG = 79.840336;
        //    SYDNEY_LAT =  ;
        //    SYDNEY_LNG = 151.20699,
        //    NEWYORK_LAT = 40.714353,
        //    NEWYORK_LNG = -74.005973;
    private static final float DEFAULTZOOM = 7; // to set the zoom level

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        if (servicesOK()) {
            setContentView(R.layout.activity_main);
       
            if (initMap()) {
                Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show();
            //    gotoLocation(SEATTLE_LAT, SEATTLE_LNG,DEFAULTZOOM);
                mMap.setMyLocationEnabled(true); 
            }
            else {
                Toast.makeText(this, "Map not available!", Toast.LENGTH_SHORT).show();
            }
        }
        else {
            setContentView(R.layout.activity_main);
        }
             mMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
//        if(mMap!=null)
//            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
       
    }

    private void hideSoftKeyboard(View v) {
        InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
   
    public void geoLocate(View v){ //Geocoder may return IOException
        hideSoftKeyboard(v);

        EditText et = (EditText) findViewById(R.id.editText1);
        String location = et.getText().toString();

        try {
            Geocoder gc = new Geocoder(this); //    when you search you will get back a java list
            List<Address> list = gc.getFromLocationName(location, 1); // Address is Android thing
            //getFromLocationName is a method of Geocoder class
            // i only want a single  item
            Address add = list.get(0); // give me the first and only item in the list
            String locality = add.getLocality();
           
            Toast.makeText(this, locality, Toast.LENGTH_LONG).show(); // To check whether Geocoder  is working or not
            //Toast.makeText(this, locality, Toast.LENGTH_LONG).show();

/*    double lat = add.getLatitude();
            double lng = add.getLongitude();

            gotoLocation(lat, lng, DEFAULTZOOM);
*/
        } catch (IOException e) {

            Log.d("EXCEPTION:", "Failed to do something: " + e.getMessage());
            Log.d("EXCEPTION: " + method,  message);
        }
    }
   
    private void gotoLocation(double lat, double lng,float zoom) {
        LatLng ll = new LatLng(lat, lng);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
        mMap.moveCamera(update);
       
    }
   
    public boolean servicesOK() {
        int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
       
        if (isAvailable == ConnectionResult.SUCCESS) {
            return true;
        }
        else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST);
            dialog.show();
        }
        else {
            Toast.makeText(this, "Can't connect to Google Play services", Toast.LENGTH_SHORT).show();
        }
        return false;
    }
   
    private boolean initMap() {
        if (mMap == null) {
            SupportMapFragment mapFrag =
                    (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            mMap = mapFrag.getMap();
        }
        return (mMap != null);
    }
   

    private void messageBox(String method, String message)
    {
        Log.d("EXCEPTION: " + method,  message);

        AlertDialog.Builder messageBox = new AlertDialog.Builder(this);
        messageBox.setTitle(method);
        messageBox.setMessage(message);
        messageBox.setCancelable(false);
        messageBox.setNeutralButton("OK", null);
        messageBox.show();
    }

// if you want to load different map versions according to user selection

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.mapTypeNone:
            mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
            break;
        case R.id.mapTypeNormal:
            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            break;
        case R.id.mapTypeSatellite:
            mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
            break;
        case R.id.mapTypeHybrid:
            mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            break;
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }
}

Most probably when you run this code you will not see any output in the default android emulator and it will ask to update google play services. The simple answer for this problem is to try and run your map using a mobile phone. otherwise you can use install the google play services into your android emulator which shoud be done in a simple work around.
July 21, 2014

How to change the name of an iOS app?

Once, I began an iPhone project with a dummy development code name, and then I wanted to change the name of the project since its nearly finished. But I was not sure how to do this with Xcode, trying the obvious of changing the application's name in the pinfo, causes the signing process to go wrong  and my app didn't launch giving me a Launcher error.

I thought of making a  new project and copy paste everything over, but it seemed so primitive.Finally i found the solution after trying different ways.

Here are the steps to follow.
  1. Go to Targets in Xcode
  2. "Get Info" on your project's target (your current dummy development name)
  3. Search for "Product Name" under "Packaging". Change the value of that what you want the new program name is going to be.

My Instagram