Showing posts with label GoogleMapAPI. Show all posts
Showing posts with label GoogleMapAPI. Show all posts

Saturday, June 7, 2014

Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment

I was using Google map API version2 with for one of my application.In layout I was using "com.google.android.gms.maps.MapFragment".

 <RelativeLayout
                        android:id="@+id/relative_disc_nyc_map"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent" >

                        <fragment
                            android:id="@+id/frg_disc_nyc_map"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            class="com.google.android.gms.maps.MapFragment" />
             </RelativeLayout>

And while executing I was using the above layout and adding it into fragment.....

public class DiscoverNYCActivity extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_discover_nyc, container, false);
        lbl_poi_title = (TextView) rootView.findViewById(R.id.lbl_poi_title);
        lbl_rate_count = (TextView) rootView.findViewById(R.id.lbl_rate_count);
return rootView;
}

But when I was travesing to the sam e screen twice.I was giving me below error

06-07 14:41:41.496: E/AndroidRuntime(21037): FATAL EXCEPTION: main
06-07 14:41:41.496: E/AndroidRuntime(21037): android.view.InflateException: Binary XML file line #120: Error inflating class fragment
06-07 14:41:41.496: E/AndroidRuntime(21037): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
06-07 14:41:41.496: E/AndroidRuntime(21037): at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
06-07 14:41:41.496: E/AndroidRuntime(21037): at com.wf.wholesale.nymta.DiscoverNYCActivity.onCreateView(DiscoverNYCActivity.java:101)
06-07 14:41:41.496: E/AndroidRuntime(21037): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
06-07 14:41:41.496: E/AndroidRuntime(21037): Caused by: java.lang.IllegalArgumentException: Binary XML file line #120: Duplicate id 0x7f090058, tag null, or parent id 0x7f090057 with another fragment for com.google.android.gms.maps.MapFragment
06-07 14:41:41.496: E/AndroidRuntime(21037): at android.app.Activity.onCreateView(Activity.java:4971)
06-07 14:41:41.496: E/AndroidRuntime(21037): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
06-07 14:41:41.496: E/AndroidRuntime(21037): ... 24 more


The problem is that what I was trying was wrong. You shouldn't be inflating fragments inside other fragments. From Android's documentation:

Note: You cannot inflate a layout into a fragment when that layout includes a <fragment>. Nested fragments are only supported when added to a fragment dynamically.

I have resolved the error with below changes.....

public class DiscoverNYCActivity extends Fragment {
View rootView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 if (rootView != null) {
           ViewGroup parent = (ViewGroup) rootView.getParent();
           if (parent != null)
               parent.removeView(rootView);
       }
       try {
        rootView = inflater.inflate(R.layout.activity_discover_nyc, container, false);
       } catch (InflateException e) {
            map is already there, just return view as it is 
       }
        lbl_poi_title = (TextView) rootView.findViewById(R.id.lbl_poi_title);
        lbl_rate_count = (TextView) rootView.findViewById(R.id.lbl_rate_count);
return rootView;
}

Friday, November 22, 2013

How to get SHA certificate in Windows ?

C:\Program Files (x86)\Java\jdk1.6.0_06\bin>keytool -v -list -keystore C:\Users\sshukla\.android\debug.keystore -alias androiddebugkey -storepass android -keypass android
Alias name: androiddebugkey
Creation date: Apr 18, 2013
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=Android Debug, O=Android, C=US
Issuer: CN=Android Debug, O=Android, C=US
Serial number: 64bbfdff
Valid from: Thu Apr 18 14:56:16 IST 2013 until: Sat Apr 11 14:56:16 IST 2043
Certificate fingerprints:
         MD5:  75:73:CC:22:4C:C0:EB:04:6A:C5:63:89:57:66:DC:DB
         SHA1: 8A:C7:3A:96:2E:4E:D2:F0:C6:B0:AB:1D:A2:FE:DA:6E:E2:71:8F:CD
         Signature algorithm name: SHA256withRSA
         Version: 3

Extensions:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: EF 85 F0 F3 11 65 7A D3   C7 66 10 21 BE 85 DB 69  .....ez..f.!...i
0010: 8B 9A 7A 07                                        ..z.
]
]


C:\Program Files (x86)\Java\jdk1.6.0_06\bin>

Monday, July 9, 2012

Google Map API key genaration in ubuntu for Android?


shailesh@shailesh-desktop:~$  cd /
shailesh@shailesh-desktop:/$ cd usr/
shailesh@shailesh-desktop:/usr$ cd lib/
shailesh@shailesh-desktop:/usr/lib$ cd jvm/
shailesh@shailesh-desktop:/usr/lib/jvm$ cd java-6-openjdk/
shailesh@shailesh-desktop:/usr/lib/jvm/java-6-openjdk$ cd bin/

then we want to get the keystore.To get the android default debug keystore:

eclipse->windows->preferences->Android ->Build, then copy the  default debug keystore.

eg: /home/shailesh/.android/debug.keystore \

shailesh@shailesh-desktop:/usr/lib/jvm/java-6-openjdk/bin$ keytool -list -alias androiddebugkey \-keystore /home/shailesh/.android/debug.keystore \
> -storepass android -keypass android
androiddebugkey, 11 Jun, 2012, PrivateKeyEntry,
Certificate fingerprint (MD5): 65:13:5E:81:B6:F4:99:12:8C:0C:4A:DF:60:3B:48:B8

shailesh@shailesh-desktop:/usr/lib/jvm/java-6-openjdk/bin$


Then take the following link: https://developers.google.com/android/maps-api-signup
Then agree the teams and conditions to generate key.

Your key is:
0vU_TfSqyKwFGQzFx08B4SLzVDTy62tVto_0CYA

This key is good for all apps signed with your certificate whose fingerprint is:
65:13:5E:81:B6:F4:99:12:8C:0C:4A:DF:60:3B:48:B8

Sunday, June 10, 2012

keytool -genkey error: Keystore file does not exist(Ubuntu) in Android ?



Below command will generate the KeyStore..........
keytool -list -alias [KEYNAME] -keystore [STORE FILENAME] -storepass [STORE PASSWORD] -keypass [KEY PASSWORD]

shailesh@shailesh-desktop:~$ cd .android/

shailesh@shailesh-desktop:~/.android$ keytool -list -alias androiddebugkey -keystore debug.keystore -storepass android -keypass android
keytool error: java.lang.Exception: Keystore file does not exist: debug.keystore

SOLUTION

Generating a key pair (and a new keystore) has to be done as a separate operation to creating a self-signed certificate for that key.
i.e.
keytool -genkey -alias myKey -keystore store.jks
keytool -selfcert -alias myKey -keystore store.jks


shailesh@shailesh-desktop:~/.android$ keytool -genkey -alias debug -keystore store.jks
Enter keystore password:

Above command will ask for the password and other information and hence the key is generated.


Tuesday, February 14, 2012

How to Draw an image as a map overlay in android?

This draws an image centered around the given geographical point in a map. Add as many of this as you want to your map.

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class DrawableMapOverlay extends Overlay {

  private static final double MAX_TAP_DISTANCE_KM = 3;
  // Rough approximation - one degree = 50 nautical miles
  private static final double MAX_TAP_DISTANCE_DEGREES = MAX_TAP_DISTANCE_KM * 0.5399568 * 50;
  private final GeoPoint geoPoint;
  private final Context context;
  private final int drawable;

  /**
   * @param context the context in which to display the overlay
   * @param geoPoint the geographical point where the overlay is located
   * @param drawable the ID of the desired drawable
   */
  public CamerasMapOverlay(Context context, GeoPoint geoPoint, int drawable) {
    this.context = context;
    this.geoPoint = geoPoint;
    this.drawable = drawable;
  }

  @Override
  public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
    super.draw(canvas, mapView, shadow);

    // Convert geo coordinates to screen pixels
    Point screenPoint = new Point();
    mapView.getProjection().toPixels(geoPoint, screenPoint);

    // Read the image
    Bitmap markerImage = BitmapFactory.decodeResource(context.getResources(), drawable);

    // Draw it, centered around the given coordinates
    canvas.drawBitmap(markerImage,
        screenPoint.x - markerImage.getWidth() / 2,
        screenPoint.y - markerImage.getHeight() / 2, null);
    return true;
  }

  @Override
  public boolean onTap(GeoPoint p, MapView mapView) {
    // Handle tapping on the overlay here
    return true;
  }
}



In the activity which has your MapView:

    MapView map = (MapView) findViewById(R.id.mapview);
    MapController mapController = map.getController();
    List<Overlay> overlays = map.getOverlays();
    overlays.add(new DrawableMapOverlay(this, point, R.drawable.my_drawable);
    map.invalidate();

Saturday, February 11, 2012

How to Transform a Map to a JSON object in android?

JSON is a convenient format for transporting info across the wire.

public static JSONObject getJSON(Map map) {
Iterator iter = map.entrySet().iterator();
JSONObject holder = new JSONObject();


while (iter.hasNext()) {
Map.Entry pairs = (Map.Entry) iter.next();
String key = (String) pairs.getKey();
Map m = (Map) pairs.getValue();
JSONObject data = new JSONObject();

try {
Iterator iter2 = m.entrySet().iterator();
while (iter2.hasNext()) {
Map.Entry pairs2 = (Map.Entry) iter2.next();
data.put((String) pairs2.getKey(), (String) pairs2.getValue());
}
holder.put(key, data);
} catch (JSONException e) {
Log.e("Transforming", "There was an error packaging JSON",e);
}
}

return holder;
}

How to Open Map via intent in Android?

Show point on google map via an intent.

String uri = "geo:"+ latitude + "," + longitude;
startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)));


// You can also choose to place a point like so:
String uri = "geo:"+ latitude + "," + longitude + "?q=my+street+address";
startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)));

Friday, February 10, 2012

How to get the last known GPS location in android?

First we are going to fetch a list of location providers with lm.getProviders(true) (likely ['network','gps']). Then we're going to loop over the array backwards: 
private double[] getGPS() {
 LocationManager lm = (LocationManager) getSystemService(
  Context.LOCATION_SERVICE);
 List<String> providers = lm.getProviders(true);

 Location l = null;
 
 for (int i=providers.size()-1; i>=0; i--) {
  l = lm.getLastKnownLocation(providers.get(i));
  if (l != null) break;
 }
 
 double[] gps = new double[2];
 if (l != null) {
  gps[0] = l.getLatitude();
  gps[1] = l.getLongitude();
 }

 return gps;
}

How to calculate the distance between two GPS coordinates in android?

A simple function that takes two GPS coordinates as input and outputs the distance between them in meter. 
private double gps2m(float lat_a, float lng_a, float lat_b, float lng_b) {
  float pk = (float) (180/3.14169);

  float a1 = lat_a / pk;
  float a2 = lng_a / pk;
  float b1 = lat_b / pk;
  float b2 = lng_b / pk;

  float t1 = FloatMath.cos(a1)*FloatMath.cos(a2)*
     FloatMath.cos(b1)*FloatMath.cos(b2);
  float t2 = FloatMath.cos(a1)*FloatMath.sin(a2)*
     FloatMath.cos(b1)*FloatMath.sin(b2);
  float t3 = FloatMath.sin(a1)*FloatMath.sin(b1);
  double tt = Math.acos(t1 + t2 + t3);
 
  return 6366000*tt;
}

Monday, February 6, 2012

How to make custom overlay on android?


Using the standard Google Maps markers on a map in your application might not be the thing you want. Instead, you would like to personalize it a bit using a custom icon, for example. But what if you want to draw that overlay from scratch, containing some dynamic information, instead of using a drawable or bitmap?

 It actually is quite simple, you just need to override the draw method in the Overlay class and the onTap method if you want to do some actions resulting a tap.
I commented the code. The way I’ve selected the different points to draw on is the result of trying, moving it a bit to the left, to the top, etc. so you shouldn’t focus on that.

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Typeface;
import android.util.Log;
import be.emich.villo.R;
import be.emich.villo.Villo;
import be.emich.villo.messages.StationMessage;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class CustomOverlay extends Overlay {
 protected Bitmap[] icons;
 protected GeoPoint point;
 protected StationMessage msg;

 protected static Bitmap GREENBIKE=null;
 protected static Bitmap REDBIKE=null;
 protected static Bitmap GREENPARKING=null;
 protected static Bitmap REDPARKING=null;
 protected static Bitmap MASTERCARD=null;

 protected static final int offsetX=18;
 protected static final int offsetY=20;

 protected int mapWidth;
 protected int mapHeight;

 protected Context ctx;

 /**
  * Constructor of the overlay. Takes in a stationmessage,
  * a POJO containing all info about the overlay to draw.
  * The context, needed to create the images and show a popup
  * when the overlay is clicked. The height and width are needed
  * so we know if drawing the overlay is necessary or not.
  * If the overlay is outside of the map, we don't minde because
  * we can't see it.
  */
 public CustomOverlay(Context ctx,StationMessage msg,int mapWidth,int mapHeight){
  this.point=new GeoPoint((int)(msg.getLatitude()*1000000),(int)(msg.getLongitude()*1000000));
  this.msg=msg;

  //If the bitmaps are not loaded yet, we load them and put them in
  //a static context so another instance of this overlay doesn't have
  //to load it.
  if(GREENBIKE==null)GREENBIKE=BitmapFactory.decodeResource(ctx.getResources(), R.drawable.greenbikesmall);
  if(REDBIKE==null)REDBIKE=BitmapFactory.decodeResource(ctx.getResources(), R.drawable.redbikesmall);
  if(GREENPARKING==null)GREENPARKING=BitmapFactory.decodeResource(ctx.getResources(), R.drawable.parkinggreen);
  if(REDPARKING==null)REDPARKING=BitmapFactory.decodeResource(ctx.getResources(), R.drawable.parkingred);
  if(MASTERCARD==null)MASTERCARD=BitmapFactory.decodeResource(ctx.getResources(), R.drawable.mastercard);

  //If ticket equals 1 we have to draw an extra icon.
  if(msg.getTicket()==1){icons = new Bitmap[3];icons[2]=MASTERCARD;}
  else icons = new Bitmap[2];

  //We fill up the other icons here.
  icons[0]=msg.getBikes()!=0?GREENBIKE:REDBIKE;
  icons[1]=msg.getParking()!=0?GREENPARKING:REDPARKING;

  this.mapWidth=mapWidth;
  this.mapHeight=mapWidth;
  this.ctx=ctx;
 }

 @Override
 public void draw(Canvas canvas, MapView mapView, boolean shadow) {
  //Translate point to x y coordinates on the screen
  Point pt = mapView.getProjection().toPixels(point, null);

  //Is the overlay outside the viewing area? If yes, do not draw it
  if(pt.x<-90 || pt.x>mapWidth || pt.y<-50 || pt.y>mapHeight+50)return;

  //Define brush 1
  Paint p1 = new Paint();
  p1.setColor(Color.BLACK);

  //Define brush 2
  Paint p2 = new Paint();
  p2.setColor(Color.WHITE);

  //Define path
  Path path = new Path();
  //Corner top left
  path.moveTo(offsetX+pt.x-20,offsetY+pt.y-60);
  //Corner top right
  path.lineTo(offsetX+pt.x+2+(18*(getCases()-1)),offsetY+pt.y-60);
  //Corner bottom right
  path.lineTo(offsetX+pt.x+2+(18*(getCases()-1)),offsetY+pt.y-30);
  //Corner triangle right
  path.lineTo(offsetX+pt.x-10,offsetY+pt.y-30);
  //Bottom triangle middle
  path.lineTo(offsetX+pt.x-20,offsetY+pt.y-20);
  //Corner bottom left
  path.lineTo(offsetX+pt.x-20,offsetY+pt.y-30);
  //Corner top left
  path.lineTo(offsetX+pt.x-20,offsetY+pt.y-60);

  //Paint polygon (made with path) and fill it using brush 2
  p2.setStyle(Paint.Style.FILL);
  canvas.drawPath(path,p2);

  //Yellow station name rect using brush 1
  p1.setColor(Color.rgb(255, 193, 40));
  p1.setStyle(Paint.Style.FILL);

  //Draw yellow box
  Path path2=new Path();
  //Corner top left
  path2.moveTo(offsetX+pt.x-20,offsetY+pt.y-60);
  //Corner top right
  path2.lineTo(offsetX+pt.x+2+(18*(getCases()-1)),offsetY+pt.y-60);
  //Corner bottom right
  path2.lineTo(offsetX+pt.x+2+(18*(getCases()-1)),offsetY+pt.y-49);
  //Corner bottom left
  path2.lineTo(offsetX+pt.x-20,offsetY+pt.y-49);
  //Corner top left
  path2.lineTo(offsetX+pt.x-20,offsetY+pt.y-60);
  canvas.drawPath(path2, p1);

  //Paint stroke above using brush 1
  p1.setColor(Color.BLACK);
  p1.setAntiAlias(false);
  p1.setStyle(Paint.Style.STROKE);
  canvas.drawPath(path,p1);

  //Draw icons inside map
  for(int i = 0;i<icons.length;i++){
   canvas.drawBitmap(icons[i],
     offsetX+pt.x+(36*(i-1))+18,
     offsetY+pt.y-48,
     p1);
  }

  p1.setAntiAlias(true);
  p1.setTypeface(Typeface.DEFAULT);
  p1.setTextSize(10);

  //Draw station occupancy
  canvas.drawText(
    Integer.toString(msg.getBikes()),
    offsetX+pt.x+2,
    offsetY+pt.y-36,
    p1);
  canvas.drawText(
    Integer.toString(msg.getParking()),
    offsetX+pt.x+2+36,
    offsetY+pt.y-36,
    p1);

  p1.setAntiAlias(true);
  p1.setTextSize(9);
  p1.setColor(Color.rgb(14,95,160));
  p1.setTypeface(Typeface.DEFAULT_BOLD);

  //Draw station name above yellow box
  canvas.drawText(
    msg.getName(),
    offsetX+2+pt.x-20,
    offsetY+pt.y-51,
    p1);
  super.draw(canvas, mapView, shadow);
 }

 /* Utility function to get the number of used "cases"
 ** this is necessary to know how large the overlay will be
 */
 protected int getCases(){
  return icons.length+2;
 }

 /*
  * This function will detect if an overlay
  * has been tapped or not and return true if so
  */
 @Override
 public boolean onTap(GeoPoint p, MapView mapView) {
  Point pointTap = mapView.getProjection().toPixels(p, null);
  Point pointMap = mapView.getProjection().toPixels(point, null);
  if(
    pointTap.x-pointMap.x>=0
    && pointTap.x-pointMap.x<=80
    && pointMap.y-pointTap.y>=0
    && pointMap.y-pointTap.y<=50
  )
  {
   Log.v("onTap",msg.getName());
   ((Villo)ctx).showDialogForMessage(msg);
   return true;
  }

  return super.onTap(p, mapView);

 }

}