logo
down
shadow

Drawing directions from google map api


Drawing directions from google map api

By : Abraham Setiawan
Date : October 19 2020, 08:10 AM
Any of those help I suspect, your api key is not authorized to use this service. You can verify by printing json
code :
print(json)
{
    "error_message" = "This API project is not authorized to use this API.";
    routes =     (
    );
    status = "REQUEST_DENIED";
}


Share : facebook icon twitter icon
Drawing MKMapView Overlay like Google Maps Directions

Drawing MKMapView Overlay like Google Maps Directions


By : Sean Dempsey
Date : March 29 2020, 07:55 AM
Hope this helps I think that @ChrisMiles is correct in that the segments are probably being drawn individually. (I initially thought that this might have been doable using CGPatternRef but you don't have any access to the CTM or path endpoints inside the pattern drawing callback.)
With this in mind, here is an exceedingly crude, back-of-the-envelope example of how you might begin such an effort (filling the segments individually). Note that:
code :
-  (CGGradientRef)lineGradient
{
    static CGGradientRef gradient = NULL;
    if (gradient == NULL) {
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGColorRef white = [[UIColor colorWithWhite:1.f
                                              alpha:0.7f] CGColor];
        CGColorRef blue = [[UIColor colorWithRed:0.1f
                                           green:0.2f
                                            blue:1.f
                                           alpha:0.7f] CGColor];
        CGColorRef lightBlue = [[UIColor colorWithRed:0.4f
                                                green:0.6f
                                                 blue:1.f
                                                alpha:0.7f] CGColor];
        CFMutableArrayRef colors = CFArrayCreateMutable(kCFAllocatorDefault, 
                                                        8,
                                                        NULL);
        CFArrayAppendValue(colors, blue);
        CFArrayAppendValue(colors, blue);
        CFArrayAppendValue(colors, white);
        CFArrayAppendValue(colors, white);
        CFArrayAppendValue(colors, lightBlue);
        CFArrayAppendValue(colors, lightBlue);
        CFArrayAppendValue(colors, blue);
        CFArrayAppendValue(colors, blue);
        CGFloat locations[8] = {0.f, 0.08f, 0.14f, 0.21f, 0.29f, 0.86f, 0.93f, 1.f};
        gradient = CGGradientCreateWithColors(colorSpace,
                                              colors,
                                              locations);
        CFRelease(colors);
        CGColorSpaceRelease(colorSpace);
    }
    return gradient;
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);

    CGContextSetAllowsAntialiasing(context, YES);
    CGContextSetShouldAntialias(context, YES);

    // Fill background color
    [[UIColor whiteColor] setFill];
    UIRectFill(rect);

    // Build a path
    CGFloat strokeWidth = 10.f;
    CGContextSetLineWidth(context, strokeWidth);

    CGGradientRef gradient = [self lineGradient];

    CGPoint points[9] = {
        CGPointMake(10.f, 25.f),
        CGPointMake(100.f, 100.f),
        CGPointMake(100.f, 150.f),
        CGPointMake(22.f, 300.f),
        CGPointMake(230.f, 400.f),
        CGPointMake(230.f, 200.f),
        CGPointMake(300.f, 200.f),
        CGPointMake(310.f, 160.f),
        CGPointMake(280.f, 100.f)
    };


    for (NSUInteger i = 1; i < 9; i++) {
        CGPoint start = points[i - 1];
        CGPoint end = points[i];
        CGFloat dy = end.y - start.y;
        CGFloat dx = end.x - start.x;
        CGFloat xOffset, yOffset;
        // Remember that, unlike Cartesian geometry, origin is in *upper* left!
        if (dx == 0) {
            // Vertical to start, gradient is horizontal
            xOffset = 0.5 * strokeWidth;
            yOffset = 0.f;
            if (dy < 0) {
                xOffset *= -1;
            }
        }
        else if (dy == 0) {
            // Horizontal to start, gradient is vertical
            xOffset = 0.f;
            yOffset = 0.5 * strokeWidth;
        }
        else {
            // Sloped
            CGFloat gradientSlope = - dx / dy;
            xOffset = 0.5 * strokeWidth / sqrt(1 + gradientSlope * gradientSlope);
            yOffset = 0.5 * strokeWidth / sqrt(1 + 1 / (gradientSlope * gradientSlope));
            if (dx < 0 && dy > 0) {
                yOffset *= -1;
            }
            else if (dx > 0 && dy < 0) {
                xOffset *= -1;
            }
            else if (dx < 0 && dy < 0) {
                yOffset *= -1;
                xOffset *= -1;
            }
            else {
            }
        }
        CGAffineTransform startTransform = CGAffineTransformMakeTranslation(-xOffset, yOffset);
        CGAffineTransform endTransform = CGAffineTransformMakeTranslation(xOffset, -yOffset);
        CGPoint gradientStart = CGPointApplyAffineTransform(start, startTransform);
        CGPoint gradientEnd = CGPointApplyAffineTransform(start, endTransform);

        CGContextSaveGState(context);
        CGContextMoveToPoint(context, start.x, start.y);
        CGContextAddLineToPoint(context, end.x, end.y);
        CGContextReplacePathWithStrokedPath(context);
        CGContextClip(context);
        CGContextDrawLinearGradient(context, 
                                    gradient, 
                                    gradientStart, 
                                    gradientEnd, 
                                    kCGGradientDrawsAfterEndLocation | kCGGradientDrawsBeforeStartLocation);
        CGContextRestoreGState(context);
    }

    CGContextRestoreGState(context);
}
Google maps (v3) get geolocate address into directions - gives status OK, but no directions (target not found)

Google maps (v3) get geolocate address into directions - gives status OK, but no directions (target not found)


By : miniTotal
Date : March 29 2020, 07:55 AM
To fix this issue You don't do anything in the DirectionsService callback routine other than output the message "Directions was not successful for the following reason: " + status:
code :
      directionsService.route(request, function(response, status) {
         alert("Directions was not successful for the following reason: " + status);
      });
directionsService.route(request, function(response, status) {
  if (status == google.maps.DirectionsStatus.OK) {
    directionsDisplay.setDirections(response);
  } else  alert("Directions was not successful for the following reason: " + status);
});
Drawing roadmap with more than 8 waypoints using Google directions API Issue

Drawing roadmap with more than 8 waypoints using Google directions API Issue


By : Leandro Castilho
Date : March 29 2020, 07:55 AM
around this issue Finaly fixed it.Now it will work with any number of point and now status = ZERO_RESULTS will not be a problem.
The problem was for some points I was getting status=ZERO_RESULT.After that it stopped executing and due to that I was not getting the map.So to fix that problem I came with the solution of skipping the particular point whenever status is not equal to OK.Then proceed with the rest of the point. To do that i took two parameter one is for storing the last index and other is path. Because when I will not get OK status then I will not be able to get the lastIndx and path from the status so when ever i am getting the problem then it will go to the else part and there I did like this.
code :
1. lastIndx = lastIndx+1 ,
2. gDirRequest(service, waypoints, userFunction,lastIndx , path) 
Google Maps setting directions of directionsRenderer object not drawing route

Google Maps setting directions of directionsRenderer object not drawing route


By : user1493626
Date : March 29 2020, 07:55 AM
this will help To set the map attribute of the DirectionsRenderer, use the .setMap method. You are currently setting the map property of the DirectionsRenderer, which is not documented.
code :
  var routeDisplay = new function() {
    var self = this;
    // Variables
    self.directionsService;
    self.directionsRenderer;
    self.map;
    self.origin;
    self.destination;

    // Functions
    self.setup = function() {
      self.directionsService = new google.maps.DirectionsService();
      self.directionsRenderer = new google.maps.DirectionsRenderer({
        preserveViewport: true,
        suppressMarkers: true
      });
    }

    self.setMap = function(map) {
      self.map = map;
      self.directionsRenderer.setMap(map);
    }

    self.setPoints = function(origin, destination) {
      self.origin = origin;
      self.destination = destination;
    }

    self.render = function() {
      if (self.directionsRenderer.getMap() == null)
        self.directionsRenderer.setMap(self.map);

      self.directionsService.route({
        origin: self.origin,
        destination: self.destination,
        travelMode: 'DRIVING'
      }, function(response, status) {
        if (status === 'OK') {
          self.directionsRenderer.setDirections(response);
        } else {
          window.alert('Directions request failed due to ' + status);
        }
      });
    }

    self.hide = function() {
      self.directionsRenderer.setMap(null);
    }

    self.show = function() {
      self.directionsRenderer.setMap(self.map);
    }

    self.toggleShow = function() {
      self.directionsRenderer.map = (self.directionsRenderer.getMap() == null) ?
        self.map : null;
    }

    self.isAlreadyRendered = function(origin, destination) {
      if (origin == self.origin && destination == self.destination) return true;
      return false;
    }
  }
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<div id="map"></div>
<script>
  var routeDisplay = new function() {
    var self = this;
    // Variables
    self.directionsService;
    self.directionsRenderer;
    self.map;
    self.origin;
    self.destination;

    // Functions
    self.setup = function() {
      self.directionsService = new google.maps.DirectionsService();
      self.directionsRenderer = new google.maps.DirectionsRenderer({
        preserveViewport: true,
        suppressMarkers: true
      });
    }

    self.setMap = function(map) {
      self.map = map;
      self.directionsRenderer.setMap(map);
    }

    self.setPoints = function(origin, destination) {
      self.origin = origin;
      self.destination = destination;
    }

    self.render = function() {
      if (self.directionsRenderer.getMap() == null)
        self.directionsRenderer.setMap(self.map);

      self.directionsService.route({
        origin: self.origin,
        destination: self.destination,
        travelMode: 'DRIVING'
      }, function(response, status) {
        if (status === 'OK') {
          self.directionsRenderer.setDirections(response);
        } else {
          window.alert('Directions request failed due to ' + status);
        }
      });
    }

    self.hide = function() {
      self.directionsRenderer.setMap(null);
    }

    self.show = function() {
      self.directionsRenderer.setMap(self.map);
    }

    self.toggleShow = function() {
      self.directionsRenderer.map = (self.directionsRenderer.getMap() == null) ?
        self.map : null;
    }

    self.isAlreadyRendered = function(origin, destination) {
      if (origin == self.origin && destination == self.destination) return true;
      return false;
    }
  }

  function initMap() {
    var myLatLng = {
      lat: 45.5325016,
      lng: -122.7973512
    };

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 15,
      center: myLatLng
    });

    var start = new google.maps.Marker({
      position: myLatLng,
      map: map,
      label: 'start'
    });

    var end = new google.maps.Marker({
      position: {
        lat: myLatLng.lat - .5,
        lng: myLatLng.lng - .5
      },
      map: map,
      label: 'end'
    });

    // Set up routeDisplay
    routeDisplay.setup();
    routeDisplay.setMap(map);

    start.addListener('click', function() {
      var a = start.position;
      var b = end.position;
      if (routeDisplay.isAlreadyRendered(a, b)) routeDisplay.toggleShow();
      else {
        routeDisplay.setPoints(a, b);
        routeDisplay.render();
      }
    });
  }
</script>

<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap">
</script>
Google api directions - drawing route not valid

Google api directions - drawing route not valid


By : Mikhail
Date : March 29 2020, 07:55 AM
I wish did fix the issue. this exception occur becoz array goes out of bound....in your decode method just use simple parameter string....their is no need of method "replaceoccuranceof string method"....just use simple string without this method........
[encoded replaceOccurrencesOfString:@"\\" withString:@"\" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
Related Posts Related Posts :
  • Managing cookies for a domain in Associated Domains
  • Array items showing error in IOS
  • Saving asynchronously downloaded files' contents to SQLITE
  • Swift 4 - animating both alpha and constraint affects other components alpha value
  • UIView animation in completion block starts with displacement
  • List all Apps and Track Internet Usage
  • Handling Back To Back HTTP Posts SWIFT 4.0
  • Azure App Service Push - registration endpoint
  • Select multiple item in a tableView
  • Apartment number / Unit number returned from google places api
  • How to align UICollectionViewCells from left to right?
  • How can I add search option on UIPickerview in ios?
  • Scenekit - physicsWorld setup to prevent kinematic nodes to intersect
  • Carthage Build Failed
  • swift 3, ios 10 - push notification firebase is not received
  • App Xcode 8 to Xcode 9 - Prevent Auto Hide Status Bar
  • How to pop back to a TableViewController?
  • Is UserDefaults thread safe in Swift? How to check it?
  • UISwitch in a table selects multiple cells when switch state on
  • iOS App with no story board (Size classes and Device type limitations)
  • AudioKit issue, mic not sensitive enough
  • send indexpath to Firebase (like button)
  • UIColor saturation brightness values different from the regular saturation luminosity values of a color
  • JSON: Extracting Data Using Alamofire and Swift 3
  • invalid_grant on OAuth2 request when obtaining access_token from SSO in App
  • ABNewPersonViewController using contact framework
  • Design iOS button scalable
  • Subclassing NSLayoutConstraint constant based on screen height
  • Migrate Realm 2.x List of Object to Realm 3.x List of String (or other primary type)
  • Using BLE RSSI value for connect-by-proximity
  • Backgroundcolor of NSTextfield
  • Firebase modifying uid in every view
  • How to rotate orientation?
  • Upload a new version of ios app to app store?
  • UIScrollView do not scroll down to its original position when keyboard disappear
  • What is the most effective way of tracking time/dates in an iOS/Swift application?
  • WatchKit get user's preferred handedness
  • Location permission dialog is shown and immediately disappears
  • Combine these regex expressions
  • How to save an array of HKQuantitySamples (heart rate) to a workout
  • RestKit mapping with parent key as attribute and key contains parenthesis
  • How to add UITextField programmatically to UIScrollView without breaking constraints?
  • How to make a view height grow depending on its child views contents
  • Some users experiencing NSInternalInconsistencyException crash
  • Mark successful siesta response as error
  • Using a modified PDFTron Tools Framework
  • How can I use universal links when I call openURL inside my own app?
  • NSArray of doubles iOS (Objective c)
  • Passing data from embedded PageViewController to parent View Controller
  • Reuse item inside a row when scrolling
  • Realm Relation how to implement
  • error: generic parameter 'T' could not be inferred in swift
  • Adding shadow to UITextView makes text expand out of UITextViewFrame
  • How to change the string in seconds to minutes in Swift3?
  • Search bar is refreshing the index of my table view cell
  • Decodable multiple DateDecodingStrategies
  • The spoiled simulator xcode 9. As a broken TV
  • How to set width of TouchableWithoutFeedback depends on Text in react-native?
  • How to show an UIActivityIndicatorView whitin UITableViewController?
  • UItextfield text in Custom UITableViewCell prints nil when i scroll up in SE
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org