Saturday 9 November 2013

Show map with infowindow example

1_map_activity.xml
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
   
     android:id="@+id/map"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 class="com.google.android.gms.maps.SupportMapFragment"/>

              android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_margin="@dimen/main_layout_margin"
         android:layout_alignParentBottom="true"
         android:layout_alignParentLeft="true"
         android:gravity="center"
         android:orientation="horizontal" >

         

   

2)MapActivity.java
=============
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.rws.apputil.Constants;
import com.rws.travelsafe.R;

public class MapActivity extends FragmentActivity implements OnClickListener{
private Button btnCall,btnRefresh;
private GoogleMap mMap;
private Marker CurrentLocationMarker;
private View infoWindow;
private String mStrLatitude,mStrLongitude,mStrPhoneNumber,mStrName;
private double mDblLatitude,mDblLongitude;

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

btnCall = (Button)findViewById(R.id.btnCall);
btnCall.setOnClickListener(this);
btnRefresh = (Button)findViewById(R.id.btnRefresh);
btnRefresh.setOnClickListener(this);

SharedPreferences mSPreferences = getSharedPreferences(Constants.SHAREDPREFERENCE.SHAREDPREFERENCES, MODE_WORLD_READABLE);
mStrName = mSPreferences.getString(Constants.CONNECT.NAME, "");
mStrLatitude = mSPreferences.getString(Constants.CONNECT.LATITUDE, "");
mStrLongitude = mSPreferences.getString(Constants.CONNECT.LONGITUDE, "");
mStrPhoneNumber = mSPreferences.getString(Constants.CONNECT.PHONE_NO, "");

mDblLatitude = Double.parseDouble(mStrLatitude);
mDblLongitude = Double.parseDouble(mStrLongitude);

System.out.println("mDblLatitude==="+mDblLatitude);
System.out.println("mDblLongitude==="+mDblLongitude);

// SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting reference to Google Map
// map = fm.getMap();
// map.addMarker(new MarkerOptions()
//                .position(new LatLng(mDblLatitude, mDblLongitude))
//                .title(mStrName +"'s"+ "\n"+ "Current Location")
//                .snippet(mStrName +"'s"+ "\n"+ "Current Location")
//                .icon(BitmapDescriptorFactory.fromResource(R.drawable.locator)));
  // infoWindow=getLayoutInflater().inflate(R.layout.custom_map, null);
 
   mMap=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
   CurrentLocationMarker=mMap.addMarker(new MarkerOptions().position(new LatLng(mDblLatitude,mDblLongitude)).title(mStrName+"'s"+ "\n"+ "Current Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_transperent)));
   CameraPosition camPos = new CameraPosition.Builder().target(new LatLng(mDblLatitude,mDblLongitude)).zoom(10).build();
   CameraUpdate camUpdate = CameraUpdateFactory.newCameraPosition(camPos);
 
   mMap.moveCamera(camUpdate);
   mMap.setInfoWindowAdapter(new InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker arg0){
                ContextThemeWrapper cw = new ContextThemeWrapper(getApplicationContext(), R.style.Transparent);
                // AlertDialog.Builder b = new AlertDialog.Builder(cw);
                LayoutInflater inflater = (LayoutInflater) cw.getSystemService(LAYOUT_INFLATER_SERVICE);
                View mConverView = inflater.inflate(R.layout.custom_map,null);
                TextView txt_title = (TextView)mConverView.findViewById(R.id.txtTitle);
                txt_title.setText(mStrName+"'s"+ "\n"+ "Current Location");
               // render(arg0, mConverView);
                return mConverView;
            }

            @Override
            public View getInfoContents(Marker arg0){
                return null;
            }
        });
   CurrentLocationMarker.showInfoWindow();
}

@Override
public void onClick(View v){
switch (v.getId()) {
case R.id.btnCall:
try {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:"+mStrPhoneNumber));
                startActivity(callIntent);
            } catch (ActivityNotFoundException activityException){
                Log.e("Calling a Phone Number", "Call failed", activityException);
            }
break;
case R.id.btnRefresh:
Intent mIntent = new Intent(this,MapActivity.class);
startActivity(mIntent);
finish();
break;
default:
break;
}
}
}
3)manifest permission
==============
         android:name="com.rws.travelsafe.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

   
   
   
   
   

   
   
   
   
   

No comments:

Post a Comment