Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: getVisibleRegion #37

Closed
fschmidt opened this issue May 8, 2014 · 3 comments
Closed

Enhancement: getVisibleRegion #37

fschmidt opened this issue May 8, 2014 · 3 comments
Milestone

Comments

@fschmidt
Copy link

fschmidt commented May 8, 2014

Hey there again. We needed a function to get the lat lng boundaries the map is currently showing and figured others may need it too so I'm sharing the code with you:

_ANDROID_*

@SuppressWarnings("unused")
   private void getVisibleRegion(final JSONArray args, final CallbackContext callbackContext) {
      try {
         final VisibleRegion visibleRegion = map.getProjection().getVisibleRegion();
         final LatLngBounds latLngBounds = visibleRegion.latLngBounds;
         final JSONObject result = new JSONObject();
         final JSONObject northeast = new JSONObject();
         final JSONObject southwest = new JSONObject();
         northeast.put("lat", latLngBounds.northeast.latitude);
         northeast.put("lng", latLngBounds.northeast.longitude);
         southwest.put("lat", latLngBounds.southwest.latitude);
         southwest.put("lng", latLngBounds.southwest.longitude);
         result.put("northeast", northeast);
         result.put("southwest", southwest);
         callbackContext.success(result);
      } catch (final JSONException e) {
         // TODO Handle exception
         throw new RuntimeException("Unhandeled Exception!", e);
      }
   }

_END_ANDROID_*

_iOS_*

- (void)getVisibleRegion:(CDVInvokedUrlCommand*)command {
    GMSVisibleRegion visibleRegion = self.mapCtrl.map.projection.visibleRegion;
    GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithRegion:visibleRegion];
    NSMutableDictionary *json = [NSMutableDictionary dictionary];
    NSMutableDictionary *northeast = [NSMutableDictionary dictionary];
    [northeast setObject:[NSNumber numberWithFloat:bounds.northEast.latitude] forKey:@"lat"];
    [northeast setObject:[NSNumber numberWithFloat:bounds.northEast.longitude] forKey:@"lng"];
    [json setObject:northeast forKey:@"northeast"];
    NSMutableDictionary *southwest = [NSMutableDictionary dictionary];
    [southwest setObject:[NSNumber numberWithFloat:bounds.southWest.latitude] forKey:@"lat"];
    [southwest setObject:[NSNumber numberWithFloat:bounds.southWest.longitude] forKey:@"lng"];
    [json setObject:southwest forKey:@"southwest"];
    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:json];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

_END_iOS_*
***_JAVASCRIPT_

getVisibleRegion = function(successCallback, errorCallback) {
    cordova.exec(successCallback, self.errorHandler, PLUGIN_NAME, 'exec', ['Map.getVisibleRegion']);
 };

***_END_JAVASCRIPT_

@fschmidt
Copy link
Author

fschmidt commented May 8, 2014

Let me know if you need something if you decide to implement this.

@wf9a5m75
Copy link
Member

wf9a5m75 commented May 8, 2014

Thank you. I will check.

wf9a5m75 added a commit that referenced this issue May 8, 2014
@wf9a5m75 wf9a5m75 added this to the v1.0.9 milestone May 8, 2014
@wf9a5m75 wf9a5m75 added the v1.0.9 label May 8, 2014
@wf9a5m75
Copy link
Member

wf9a5m75 commented May 9, 2014

Close with #33

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants