I am doing a google map project where I have to fetch the data (title and snippet description) from Array List (which is retrieved from server) and display it in title and snippet dynamically. Since the description in the snippet is lengthy, the whole description is not displayed. The following is my code.
I am able to get a title and a snippet when I tap the marker. What I need is, the snippet should show the lengthy description which comes from server. At present what happens is, there is one title line and one snippet line. The description is shown half in the snippet. If I am not clear please let me know. Need to solve this.
@SuppressLint("NewApi")
public class GoogleActivity extends FragmentActivity implements LocationListener {
private LocationManager locationManager;
private static final long MIN_TIME = 700;
private static final float MIN_DISTANCE = 800;
private Location mLocation;
// Google Map
private GoogleMap googleMap;
LatLng myPosition;
// All static variables
static final String URL = "http://webersspot.accountsupport.com/gmaptrial/onedb/phpsqlajax_genxml.php";
// XML node keys
static final String KEY_PID = "pro"; // parent node
static final String KEY_NAME = "Name";
static final String KEY_DESCRIPTION = "Description";
static final String KEY_LAT = "Latitude";
static final String KEY_LONG = "Longitude";
ArrayList<HashMap<String, String>> storeMapData = new ArrayList<HashMap<String, String>>();
private ShareActionProvider mShareActionProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//open the map
openTheMap();
/*
// Get Location Manager and check for GPS & Network location services
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
!lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
// Build the alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Location Services Not Active");
builder.setMessage("Please enable Location Services and GPS");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
// Show location settings when the user acknowledges the alert dialog
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
Dialog alertDialog = builder.create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.show();
}
*/
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this); //You can also use LocationManager.GPS_PROVIDER and LocationManager.PASSIVE_PROVIDER
new LongOperation().execute("");
new MapOperation().execute(googleMap);
}
/* open the map */
private void openTheMap() {
try {
if(googleMap == null) {
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = mapFragment.getMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); // Hybrid for satellite with place name
googleMap.setMyLocationEnabled(true); // enable user location button.
googleMap.setInfoWindowAdapter(null) ;
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setAllGesturesEnabled(true);
googleMap.setTrafficEnabled(true); // enable road
zoomMap();
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/* zoom current location */
private void zoomMap() {
int zoomScale = 12;
double currentLat = mLocation.getLatitude();
double currentLon = mLocation.getLongitude();
googleMap.moveCamera(CameraUpdateFactory
.newLatLngZoom(new LatLng(currentLat, currentLon), zoomScale));
}
public List<HashMap<String, String>> prepareData(){
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
//List<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XmlParser parser = new XmlParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_PID);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
System.out.println("OOOOOOOOOOOOOOOOOOO ::: "+e.getAttribute(KEY_NAME));
// adding each child node to HashMap key => value
map.put(KEY_NAME, e.getAttribute(KEY_NAME).toString());
map.put(KEY_DESCRIPTION ,e.getAttribute(KEY_DESCRIPTION).toString());
map.put(KEY_LAT, e.getAttribute(KEY_LAT).toString());
map.put(KEY_LONG ,e.getAttribute(KEY_LONG).toString());
// adding HashList to ArrayList
menuItems.add(map);
storeMapData = menuItems;
}
return menuItems;
}
public void onMapReady(final GoogleMap map) {
ArrayList<HashMap<String, String>> processData = storeMapData;
System.out.println( "kjkasdc "+processData);
for (int i=0; i< processData.size(); i++){
final double lat = Double.parseDouble(processData.get(i).get(KEY_LAT));
System.out.println("MAP LAT ::::::::::::::::::::::::: "+lat);
final double lon = Double.parseDouble(processData.get(i).get(KEY_LONG));
System.out.println("MAP LON ::::::::::::::::::::::::: "+lon);
final String address = processData.get(i).get(KEY_DESCRIPTION);
System.out.println("MAP ADDRESS ::::::::::::::::::::::::: "+address);
final String name = processData.get(i).get(KEY_NAME);
System.out.println("MAP ADDRESS ::::::::::::::::::::::::: "+name);
runOnUiThread(new Runnable() {
@Override
public void run() {
map.addMarker(new MarkerOptions().position(new LatLng(lat, lon)).title(name).snippet(address).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
}
});
}
}
@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Inflating the current activity's menu with res/menu/items.xml */
getMenuInflater().inflate(R.menu.share_menu, menu);
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_item_share).getActionProvider();
/** Setting a share intent */
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return super.onCreateOptionsMenu(menu);
}
/** Returns a share intent */
private Intent getDefaultShareIntent(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT,"Download");
intent.putExtra(Intent.EXTRA_TEXT,"Download Hill Top Beauty Parlour App - Maroli from Google Play Store: https://play.google.com/store/apps/details?id=beauty.parlour.maroli");
return intent;
}
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
prepareData();
return "Executed";
}
@Override
protected void onPostExecute(String result) {
System.out.println("Executed");
}
@Override
protected void onPreExecute() {
System.out.println("Execution started");
}
@Override
protected void onProgressUpdate(Void... values) {
System.out.println(" -- -- -- "+values);
}
}
private class MapOperation extends AsyncTask<GoogleMap, Void, String> {
@Override
protected String doInBackground(GoogleMap... params) {
GoogleMap map = params[0];
onMapReady(map);
return "Executed";
}
@Override
protected void onPostExecute(String result) {
System.out.println(result);
}
@Override
protected void onPreExecute() {
System.out.println("Execution started");
}
@Override
protected void onProgressUpdate(Void... values) {
System.out.println(" -- -- -- "+values);
}
}
class MyInfoWindowAdapter implements InfoWindowAdapter{
private final View myContentsView;
MyInfoWindowAdapter(){
myContentsView = getLayoutInflater().inflate(R.layout.custom_info_contents, null);
}
@Override
public View getInfoContents(Marker marker) {
TextView tvTitle = ((TextView)myContentsView.findViewById(R.id.title));
tvTitle.setText(marker.getTitle());
TextView tvaddress = ((TextView)myContentsView.findViewById(R.id.snippet));
tvaddress.setText(marker.getTitle());
return myContentsView;
}
@Override
public View getInfoWindow(Marker marker) {
// TODO Auto-generated method stub
return null;
}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
googleMap.animateCamera(cameraUpdate);
locationManager.removeUpdates(this);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
At present I am able to get a title and a snippet when I tap the marker. What I need is, the snippet should show the lengthy description which comes from server. At present what happens is, there is one title line and one snippet line. The description is shown half in the snippet. If I am not clear please let me know. Need to solve this.