Skip to content

Commit 5a7a7c5

Browse files
author
Hirbod
committed
Merge pull request #503 from trancee/master
Implement maxWidth for Marker InfoWindow
2 parents 14bb6f8 + ef62c60 commit 5a7a7c5

5 files changed

Lines changed: 162 additions & 57 deletions

File tree

src/android/plugin/google/maps/GoogleMaps.java

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,7 +1693,17 @@ public void onClick(View view) {
16931693
return;
16941694
}
16951695
}
1696-
1696+
1697+
public static boolean isNumeric(String str)
1698+
{
1699+
for (char c : str.toCharArray()) {
1700+
if (!Character.isDigit(c)) {
1701+
return false;
1702+
}
1703+
}
1704+
return true;
1705+
}
1706+
16971707
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
16981708
@Override
16991709
public View getInfoContents(Marker marker) {
@@ -1725,6 +1735,55 @@ public View getInfoContents(Marker marker) {
17251735
windowLayer.setOrientation(LinearLayout.VERTICAL);
17261736
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
17271737
layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER;
1738+
1739+
int maxWidth = 0;
1740+
1741+
if (styles != null) {
1742+
try {
1743+
int width = 0;
1744+
String widthString = styles.getString("width");
1745+
1746+
if (widthString.endsWith("%")) {
1747+
double widthDouble = Double.parseDouble(widthString.replace ("%", ""));
1748+
1749+
width = (int)((double)mapView.getWidth() * (widthDouble / 100));
1750+
} else if (isNumeric(widthString)) {
1751+
double widthDouble = Double.parseDouble(widthString);
1752+
1753+
if (widthDouble <= 1.0) { // for percentage values (e.g. 0.5 = 50%).
1754+
width = (int)((double)mapView.getWidth() * (widthDouble));
1755+
} else {
1756+
width = (int)widthDouble;
1757+
}
1758+
}
1759+
1760+
if (width > 0) {
1761+
layoutParams.width = width;
1762+
}
1763+
} catch (Exception e) {}
1764+
1765+
try {
1766+
String widthString = styles.getString("maxWidth");
1767+
1768+
if (widthString.endsWith("%")) {
1769+
double widthDouble = Double.parseDouble(widthString.replace ("%", ""));
1770+
1771+
maxWidth = (int)((double)mapView.getWidth() * (widthDouble / 100));
1772+
1773+
// make sure to take padding into account.
1774+
maxWidth -= (windowLayer.getPaddingLeft() + windowLayer.getPaddingRight());
1775+
} else if (isNumeric(widthString)) {
1776+
double widthDouble = Double.parseDouble(widthString);
1777+
1778+
if (widthDouble <= 1.0) { // for percentage values (e.g. 0.5 = 50%).
1779+
maxWidth = (int)((double)mapView.getWidth() * (widthDouble));
1780+
} else {
1781+
maxWidth = (int)widthDouble;
1782+
}
1783+
}
1784+
} catch (Exception e) {}
1785+
}
1786+
17281787
windowLayer.setLayoutParams(layoutParams);
17291788

17301789
//----------------------------------------
@@ -1762,6 +1821,11 @@ public View getInfoContents(Marker marker) {
17621821
image = PluginUtil.scaleBitmapForDevice(image);
17631822
ImageView imageView = new ImageView(this.cordova.getActivity());
17641823
imageView.setImageBitmap(image);
1824+
1825+
if (maxWidth > 0) {
1826+
imageView.setMaxWidth(maxWidth);
1827+
}
1828+
17651829
windowLayer.addView(imageView);
17661830
} else {
17671831
TextView textView = new TextView(this.cordova.getActivity());
@@ -1798,7 +1862,11 @@ public View getInfoContents(Marker marker) {
17981862
} catch (JSONException e) {}
17991863
}
18001864
textView.setTypeface(Typeface.DEFAULT, fontStyle);
1801-
1865+
1866+
if (maxWidth > 0) {
1867+
textView.setMaxWidth(maxWidth);
1868+
}
1869+
18021870
windowLayer.addView(textView);
18031871
}
18041872
}
@@ -1813,8 +1881,13 @@ public View getInfoContents(Marker marker) {
18131881
textView2.setTextAlignment(textAlignment);
18141882
}
18151883

1884+
if (maxWidth > 0) {
1885+
textView2.setMaxWidth(maxWidth);
1886+
}
1887+
18161888
windowLayer.addView(textView2);
18171889
}
1890+
18181891
return windowLayer;
18191892
}
18201893

src/ios/GoogleMaps/GoogleMapsViewController.m

Lines changed: 87 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,50 @@ -(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker*)marker
372372
rightImg = [self loadImageFromGoogleMap:@"bubble_right@2x"];
373373
float scale = leftImg.scale;
374374
int sizeEdgeWidth = 10;
375-
375+
376+
int width = 0;
377+
378+
if (styles && [styles objectForKey:@"width"]) {
379+
NSString *widthString = [styles valueForKey:@"width"];
380+
381+
if ([widthString hasSuffix:@"%"]) {
382+
double widthDouble = [[widthString stringByReplacingOccurrencesOfString:@"%" withString:@""] doubleValue];
383+
384+
width = (int)((double)mapView.frame.size.width * (widthDouble / 100));
385+
} else if ([widthString isNumeric:widthString]) {
386+
double widthDouble = [widthString doubleValue];
387+
388+
if (widthDouble <= 1.0) {
389+
width = (int)((double)mapView.frame.size.width * (widthDouble));
390+
} else {
391+
width = (int)widthDouble;
392+
}
393+
}
394+
}
395+
396+
int maxWidth = 0;
397+
398+
if (styles && [styles objectForKey:@"maxWidth"]) {
399+
NSString *widthString = [styles valueForKey:@"maxWidth"];
400+
401+
if ([widthString hasSuffix:@"%"]) {
402+
double widthDouble = [[widthString stringByReplacingOccurrencesOfString:@"%" withString:@""] doubleValue];
403+
404+
maxWidth = (int)((double)mapView.frame.size.width * (widthDouble / 100));
405+
406+
// make sure to take padding into account.
407+
maxWidth -= sizeEdgeWidth;
408+
} else if ([widthString isNumeric:widthString]) {
409+
double widthDouble = [widthString doubleValue];
410+
411+
if (widthDouble <= 1.0) {
412+
maxWidth = (int)((double)mapView.frame.size.width * (widthDouble));
413+
} else {
414+
maxWidth = (int)widthDouble;
415+
}
416+
}
417+
}
418+
376419
//-------------------------------------
377420
// Calculate the size for the contents
378421
//-------------------------------------
@@ -382,16 +425,14 @@ -(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker*)marker
382425
isTextMode = false;
383426
NSArray *tmp = [title componentsSeparatedByString:@","];
384427
NSData *decodedData;
385-
#if !defined(__IPHONE_8_0)
386-
decodedData = [[NSData alloc] initWithBase64Encoding:(NSString *)tmp[1]];
387-
#else
428+
#ifdef __IPHONE_7_0
388429
if ([PluginUtil isIOS7_OR_OVER]) {
389-
decodedData = [NSData dataFromBase64String:tmp[1]];
430+
decodedData = [[NSData alloc] initWithBase64Encoding:(NSString *)tmp[1]];
390431
} else {
391-
#if !defined(__IPHONE_7_0)
392-
decodedData = [[NSData alloc] initWithBase64Encoding:(NSString *)tmp[1]];
393-
#endif
432+
decodedData = [NSData dataFromBase64String:tmp[1]];
394433
}
434+
#else
435+
decodedData = [NSData dataFromBase64String:tmp[1]];
395436
#endif
396437

397438
base64Image = [[UIImage alloc] initWithData:decodedData];
@@ -412,15 +453,15 @@ -(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker*)marker
412453
}
413454
}
414455
if (isBold == TRUE && isItalic == TRUE) {
415-
#ifdef __IPHONE_7_0
456+
if ([PluginUtil isIOS7_OR_OVER] == true) {
416457
// ref: http://stackoverflow.com/questions/4713236/how-do-i-set-bold-and-italic-on-uilabel-of-iphone-ipad#21777132
417458
titleFont = [UIFont systemFontOfSize:17.0f];
418459
UIFontDescriptor *fontDescriptor = [titleFont.fontDescriptor
419460
fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold | UIFontDescriptorTraitItalic];
420461
titleFont = [UIFont fontWithDescriptor:fontDescriptor size:0];
421-
#else
462+
} else {
422463
titleFont = [UIFont fontWithName:@"Helvetica-BoldOblique" size:17.0];
423-
#endif
464+
}
424465
} else if (isBold == TRUE && isItalic == FALSE) {
425466
titleFont = [UIFont boldSystemFontOfSize:17.0f];
426467
} else if (isBold == TRUE && isItalic == FALSE) {
@@ -430,33 +471,14 @@ -(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker*)marker
430471
}
431472

432473
// Calculate the size for the title strings
433-
CGSize tmpSize = CGSizeMake(mapView.frame.size.width - 13, mapView.frame.size.height - 13);
434-
#if !defined(__IPHONE_8_0)
435-
textSize = [title sizeWithFont:titleFont constrainedToSize: tmpSize];
436-
#else
437-
NSDictionary *attr = @{ NSFontAttributeName: titleFont};
438-
textSize = [title boundingRectWithSize:tmpSize
439-
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine
440-
attributes:attr
441-
context:nil].size;
442-
#endif
474+
textSize = [title sizeWithFont:titleFont constrainedToSize: CGSizeMake(mapView.frame.size.width - 13, mapView.frame.size.height - 13)];
443475
rectSize = CGSizeMake(textSize.width + 10, textSize.height + 22);
444476

445477
// Calculate the size for the snippet strings
446478
if (snippet) {
447479
snippetFont = [UIFont systemFontOfSize:12.0f];
448480
snippet = [snippet stringByReplacingOccurrencesOfString:@"\n" withString:@""];
449-
450-
451-
#if !defined(__IPHONE_8_0)
452-
snippetSize = [snippet sizeWithFont:snippetFont constrainedToSize: tmpSize];
453-
#else
454-
NSDictionary *attr = @{ NSFontAttributeName: snippetFont};
455-
snippetSize = [snippet boundingRectWithSize:tmpSize
456-
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine
457-
attributes:attr
458-
context:nil].size;
459-
#endif
481+
snippetSize = [snippet sizeWithFont:snippetFont constrainedToSize: CGSizeMake(mapView.frame.size.width - 13, mapView.frame.size.height - 13)];
460482
rectSize.height += snippetSize.height + 4;
461483
if (rectSize.width < snippetSize.width + leftImg.size.width) {
462484
rectSize.width = snippetSize.width + leftImg.size.width;
@@ -468,6 +490,14 @@ -(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker*)marker
468490
} else {
469491
rectSize.width += sizeEdgeWidth;
470492
}
493+
494+
if (width > 0) {
495+
rectSize.width = width;
496+
}
497+
if (maxWidth > 0 &&
498+
maxWidth < rectSize.width) {
499+
rectSize.width = maxWidth;
500+
}
471501

472502
//-------------------------------------
473503
// Draw the the info window
@@ -594,7 +624,7 @@ -(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker*)marker
594624
}
595625

596626
CGRect textRect = CGRectMake(5, 5 , rectSize.width - 10, textSize.height );
597-
#if defined(__IPHONE_7_0)
627+
if ([PluginUtil isIOS7_OR_OVER] == true) {
598628
// iOS7 and above
599629
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
600630
style.lineBreakMode = NSLineBreakByWordWrapping;
@@ -607,41 +637,43 @@ -(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker*)marker
607637
};
608638
[title drawInRect:textRect
609639
withAttributes:attributes];
610-
#else
640+
641+
642+
} else {
611643
// iOS6
612644
[titleColor set];
613645
[title drawInRect:textRect
614646
withFont:titleFont
615647
lineBreakMode:NSLineBreakByWordWrapping
616648
alignment:textAlignment];
617-
#endif
649+
}
618650
//CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 0.5);
619651
//CGContextStrokeRect(context, textRect);
620652
}
621653

622654
//Draw the snippet
623655
if (snippet) {
624656
CGRect textRect = CGRectMake(5, textSize.height + 10 , rectSize.width - 10, snippetSize.height );
625-
#if defined(__IPHONE_7_0)
626-
// iOS7 and above
627-
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
628-
style.lineBreakMode = NSLineBreakByWordWrapping;
629-
style.alignment = textAlignment;
630-
631-
NSDictionary *attributes = @{
632-
NSForegroundColorAttributeName : [UIColor grayColor],
633-
NSFontAttributeName : snippetFont,
634-
NSParagraphStyleAttributeName : style
635-
};
636-
[snippet drawInRect:textRect withAttributes:attributes];
637-
#else
638-
// iOS6
639-
[[UIColor grayColor] set];
640-
[snippet drawInRect:textRect
641-
withFont:snippetFont
642-
lineBreakMode:NSLineBreakByWordWrapping
643-
alignment:textAlignment];
644-
#endif
657+
if ([PluginUtil isIOS7_OR_OVER] == true) {
658+
// iOS7 and above
659+
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
660+
style.lineBreakMode = NSLineBreakByWordWrapping;
661+
style.alignment = textAlignment;
662+
663+
NSDictionary *attributes = @{
664+
NSForegroundColorAttributeName : [UIColor grayColor],
665+
NSFontAttributeName : snippetFont,
666+
NSParagraphStyleAttributeName : style
667+
};
668+
[snippet drawInRect:textRect withAttributes:attributes];
669+
} else {
670+
// iOS6
671+
[[UIColor grayColor] set];
672+
[snippet drawInRect:textRect
673+
withFont:snippetFont
674+
lineBreakMode:NSLineBreakByWordWrapping
675+
alignment:textAlignment];
676+
}
645677
}
646678
} else {
647679
//Draw the content image

src/ios/GoogleMaps/NSData-Base64/NSData+Base64.h

100755100644
File mode changed.

src/ios/GoogleMaps/NSData-Base64/NSData+Base64.m

100755100644
File mode changed.

src/ios/GoogleMaps/NSData-Base64/NSData+Base64.podspec

100755100644
File mode changed.

0 commit comments

Comments
 (0)