iPhone SDK में MapKit में एक मार्ग आकर्षित

वोट
53

मैं नक्शे पर दो स्थानों के बीच एक मार्ग आकर्षित करने के लिए चाहते हैं। एक टूर गाइड की तरह। पर्यटक किसी अन्य स्थान क्लिक करता है, मैं एक मार्ग आकर्षित करने के लिए सक्षम होना चाहते हैं; साथ ही साथ, वर्तमान स्थान से दूरी के बारे में सूचित करें।

मैं इंटरनेट पर साइटों जो बताने के लिए नक्शे पर पोलीलाइंस आकर्षित करने के लिए के बारे में पता कर रहा हूँ। लेकिन, उदाहरण के सबसे विभिन्न निर्देशांक के साथ एक प्रीलोडेड csv फ़ाइल थी।

वहाँ के रूप में स्थान गतिशील चयन किया जाता है, गूगल या किसी अन्य प्रदाता से निर्देशांक प्राप्त करने के लिए एक वैकल्पिक तरीका है।

नहीं तो, मैं कैसे मध्यवर्ती निर्देशांक के लिए जानकारी प्राप्त करूं?

IOS 6 इस समस्या के लिए कोई सीधा रास्ता प्रदान करता है?

14/05/2010 को 14:37
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


9 जवाब

वोट
61

निम्नलिखित viewDidLoadहोगा (1) दो स्थानों, (2) सभी पिछले एनोटेशन बाहर निकालना, और (3) कॉल उपयोगकर्ता परिभाषित सहायक कार्य (मार्ग अंक मिलता है और मार्ग आकर्षित करने के लिए) की स्थापना की।

-(void)viewDidLoad
{
    [super viewDidLoad];

    // Origin Location.
    CLLocationCoordinate2D loc1;
    loc1.latitude = 29.0167;
    loc1.longitude = 77.3833;
    Annotation *origin = [[Annotation alloc] initWithTitle:@"loc1" subTitle:@"Home1" andCoordinate:loc1];
    [objMapView addAnnotation:origin];

    // Destination Location.
    CLLocationCoordinate2D loc2;
    loc2.latitude = 19.076000;
    loc2.longitude = 72.877670;
    Annotation *destination = [[Annotation alloc] initWithTitle:@"loc2" subTitle:@"Home2" andCoordinate:loc2];
    [objMapView addAnnotation:destination];

    if(arrRoutePoints) // Remove all annotations
        [objMapView removeAnnotations:[objMapView annotations]];

    arrRoutePoints = [self getRoutePointFrom:origin to:destination];
    [self drawRoute];
    [self centerMap];
}

निम्नलिखित है MKMapViewDelegateविधि, जिससे ओवरले ड्रॉ (आईओएस 4.0 और बाद में)।

/* MKMapViewDelegate Meth0d -- for viewForOverlay*/
- (MKOverlayView*)mapView:(MKMapView*)theMapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKPolylineView *view = [[MKPolylineView alloc] initWithPolyline:objPolyline];
    view.fillColor = [UIColor blackColor];
    view.strokeColor = [UIColor blackColor];
    view.lineWidth = 4;
    return view;
}

निम्नलिखित समारोह दोनों स्थानों और सभी मार्ग अंक प्राप्त करने के लिए यूआरएल तैयार करेंगे। और बेशक, stringWithURL कॉल करेंगे।

/* This will get the route coordinates from the Google API. */
- (NSArray*)getRoutePointFrom:(Annotation *)origin to:(Annotation *)destination
{
    NSString* saddr = [NSString stringWithFormat:@"%f,%f", origin.coordinate.latitude, origin.coordinate.longitude];
    NSString* daddr = [NSString stringWithFormat:@"%f,%f", destination.coordinate.latitude, destination.coordinate.longitude];

    NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@", saddr, daddr];
    NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];

    NSError *error;
    NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSUTF8StringEncoding error:&error];
    NSString* encodedPoints = [apiResponse stringByMatching:@"points:\\\"([^\\\"]*)\\\"" capture:1L];

    return [self decodePolyLine:[encodedPoints mutableCopy]];
}

निम्नलिखित कोड असली जादू (प्रतिक्रिया हम एपीआई से मिल गया के लिए डिकोडर) है। जब तक मैं जानता हूँ कि मैं क्या कर रहा हूँ मुझे लगता है कि कोड को संशोधित नहीं होगा :)

- (NSMutableArray *)decodePolyLine:(NSMutableString *)encodedString
{
    [encodedString replaceOccurrencesOfString:@"\\\\" withString:@"\\"
                                  options:NSLiteralSearch
                                    range:NSMakeRange(0, [encodedString length])];
    NSInteger len = [encodedString length];
    NSInteger index = 0;
    NSMutableArray *array = [[NSMutableArray alloc] init];
    NSInteger lat=0;
    NSInteger lng=0;
    while (index < len) {
        NSInteger b;
        NSInteger shift = 0;
        NSInteger result = 0;
        do {
            b = [encodedString characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lat += dlat;
        shift = 0;
        result = 0;
        do {
            b = [encodedString characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
       } while (b >= 0x20);
        NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lng += dlng;
        NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5];
        NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5];
        printf("\n[%f,", [latitude doubleValue]);
        printf("%f]", [longitude doubleValue]);
        CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
        [array addObject:loc];
    }
    return array;
}

यह फ़ंक्शन एक मार्ग आकर्षित करेगा और एक उपरिशायी जोड़ देगा।

- (void)drawRoute
{
    int numPoints = [arrRoutePoints count];
    if (numPoints > 1)
    {
        CLLocationCoordinate2D* coords = malloc(numPoints * sizeof(CLLocationCoordinate2D));
        for (int i = 0; i < numPoints; i++)
        {
            CLLocation* current = [arrRoutePoints objectAtIndex:i];
            coords[i] = current.coordinate;
        }

        self.objPolyline = [MKPolyline polylineWithCoordinates:coords count:numPoints];
        free(coords);

        [objMapView addOverlay:objPolyline];
        [objMapView setNeedsDisplay];
    }
}

निम्नलिखित कोड नक्शा संरेखित केंद्रित हो जाएगा।

- (void)centerMap
{
    MKCoordinateRegion region;

    CLLocationDegrees maxLat = -90;
    CLLocationDegrees maxLon = -180;
    CLLocationDegrees minLat = 90;
    CLLocationDegrees minLon = 180;

    for(int idx = 0; idx < arrRoutePoints.count; idx++)
    {
        CLLocation* currentLocation = [arrRoutePoints objectAtIndex:idx];

        if(currentLocation.coordinate.latitude > maxLat)
            maxLat = currentLocation.coordinate.latitude;
        if(currentLocation.coordinate.latitude < minLat)
            minLat = currentLocation.coordinate.latitude;
        if(currentLocation.coordinate.longitude > maxLon)
            maxLon = currentLocation.coordinate.longitude;
        if(currentLocation.coordinate.longitude < minLon)
            minLon = currentLocation.coordinate.longitude;
    }

    region.center.latitude     = (maxLat + minLat) / 2;
    region.center.longitude    = (maxLon + minLon) / 2;
    region.span.latitudeDelta  = maxLat - minLat;
    region.span.longitudeDelta = maxLon - minLon;

    [objMapView setRegion:region animated:YES];
}

मुझे आशा है कि यह किसी को मदद मिलेगी।

12/08/2012 को 18:21
का स्रोत उपयोगकर्ता

वोट
24

यह पेचीदा है। ऐसा करने के लिए MapKit के साथ कोई तरीका नहीं है: यह काफी आसान है जब आप निर्देशांक जानना लाइनों आकर्षित करने के लिए है, लेकिन MapKit आप सड़कों या अन्य रूटिंग जानकारी तक पहुंच ही नहीं होगा। मैं आप अपने डेटा प्राप्त करने के लिए एक बाहरी एपीआई कॉल करने की आवश्यकता कहेंगे।

मैं cloudmade.com एपीआई के साथ खेल रहा है। वेक्टर धारा सर्वर लौटना चाहिए कि तुम क्या जरूरत है, और फिर आप आकर्षित कर सकते हैं कि आपके नक्शे पर। हालांकि, गूगल के नक्शे और के बीच फ़र्क OSM वे MapKit के लिए एक समान है: नक्शे cloudmade द्वारा प्रयोग किया जाता आप cloudmade नक्शे सभी तरह उपयोग करना चाहते हैं कर सकते हैं।

पुनश्च: अन्य मानचित्रण प्रदाताओं - गूगल, बिंग, आदि भी बराबर डाटा फ़ीड उपलब्ध करा सकता है। मैं अभी हाल ही में OSM / Cloudmade को देखकर किया गया है।

पी पी एस: इस में से कोई भी तुच्छ नौसिखिया चीज़ है! शुभकामनाएँ!

14/05/2010 को 14:59
का स्रोत उपयोगकर्ता

वोट
12

Andiih की यह अधिकार मिल गया। MapKit आप ऐसा नहीं दूँगा। दुर्भाग्य से, Google आप क्या आप या तो करना चाहते हैं नहीं दूँगा।

जब एप्पल MapKit और सभी की घोषणा की है, वे भी स्पष्ट रूप से कहा है कि किसी भी नौवहन अनुप्रयोगों BYOM होगा: अपने स्वयं के नक्शे ले आओ, इसलिए किसी भी नेविगेशन आवेदन मैपिंग टूल का अपने स्वयं के सेट का उपयोग करता।

गूगल की सेवा की शर्तों यहां तक ​​कि उनके नक्शे के शीर्ष पर मार्गों प्रदर्शित करने से आप को सीमित:

http://code.google.com/intl/de/apis/maps/iphone/terms.html

लाइसेंस प्रतिबंध:

10.9 किसी भी उत्पाद, प्रणालियों, या के लिए या के सिलसिले में अनुप्रयोगों के साथ सेवा या सामग्री का उपयोग करें:

(क) रीयल टाइम नेविगेशन या सहित मार्ग निर्देशन, लेकिन नहीं मोड़-दर-मोड़ मार्ग निर्देशन, जो उपयोगकर्ता की सेंसर-सक्षम उपकरण की स्थिति में सिंक्रोनाइज़ सीमित;

(ख) किसी भी सिस्टम या वाहन व्यवहार के स्वचालित या स्वतंत्र नियंत्रण के कार्य; या

(ग) प्रेषण, बेड़ा प्रबंधन, व्यापार संपत्ति ट्रैकिंग, या इसी तरह उद्यम अनुप्रयोगों (गूगल मैप्स एपीआई इस तरह की कारों, बसों या अन्य वाहनों के रूप में परिसंपत्तियों (ट्रैक करने के लिए इस्तेमाल किया जा सकता) जब तक ट्रैकिंग आवेदन के रूप में बिना जनता के लिए उपलब्ध कराया गया है प्रभारी। उदाहरण के लिए, आप एक नि: शुल्क, सार्वजनिक मैप्स एपीआई कार्यान्वयन कि वास्तविक समय सार्वजनिक परिवहन या अन्य परिवहन की स्थिति की जानकारी प्रदर्शित करता है दे सकते हैं।

दुःख की बात यह भी शामिल है कि आप क्या करना चाहते हैं क्या। उम्मीद है कि एक दिन MapKit इस तरह की सुविधाओं की अनुमति के लिए ... हालांकि संभावना नहीं विस्तार किया जाएगा।

सौभाग्य।

14/05/2010 को 19:54
का स्रोत उपयोगकर्ता

वोट
5

आप पर एक नज़र है करने के लिए चाहते हो सकता है https://github.com/leviathan/nvpolyline v.4.0 करने से पहले यह समाधान विशेष रूप से iPhone OS संस्करण पर लक्षित है

हालांकि यह भी v.4.0 आशा में इस्तेमाल किया जा सकता यह मदद करता है।

01/07/2010 को 15:22
का स्रोत उपयोगकर्ता

वोट
4

प्रयास करें MapKit-मार्ग-निर्देश प्राप्त ( GitHub )।

05/01/2011 को 08:06
का स्रोत उपयोगकर्ता

वोट
3

प्राप्त करने और नक्शे पर एक मार्ग ड्राइंग iOS 7 एपीआई के साथ सुपर आसान है:

MKDirectionsRequest *directionsRequest = [[MKDirectionsRequest alloc] init];

// Set the origin of the route to be current user location
[directionsRequest setSource:[MKMapItem mapItemForCurrentLocation]];

// Set the destination point of the route
CLLocationCoordinate2D destinationCoordinate = CLLocationCoordinate2DMake(34.0872, 76.235);
MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoordinate addressDictionary:nil];
[directionsRequest setDestination:[[MKMapItem alloc] initWithPlacemark:destinationPlacemark]];

MKDirections *directions = [[MKDirections alloc] initWithRequest:directionsRequest];

// Requesting route information from Apple Map services
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
    if (error) {
        NSLog(@"Cannot calculate directions: %@",[error localizedDescription]);
    } else {
        // Displaying the route on the map
        MKRoute *route = [response.routes firstObject];
        [mapView addOverlay:route.polyline];
    }
}];
14/02/2015 को 16:46
का स्रोत उपयोगकर्ता

वोट
3

बस स्पष्ट करने के लिए, ऐसा लगता है कि वहाँ चीजों की एक जोड़ी चर्चा की जा रही है। एक एक तरह से एक मार्ग के कोने पाने के लिए है और अन्य उन कोने का उपयोग कर मानचित्र पर एक उपरिशायी आकर्षित करने के लिए है। मैं MapQuest एपीआई पता है, तो मैं नीचे उन लोगों के लिए कुछ लिंक है - गूगल और बिंग समकक्ष मुझे लगता है कि है।

1) एक मार्ग के कोने हो रही है
आप एक मार्ग ओवरले आकर्षित करने के लिए एक मार्ग के नए निर्देशांक के लिए देख रहे हैं, तो आप एक मार्ग वेब सेवा के लिए वेब सेवा कॉल का उपयोग कर सकते - मैं तुम्हें करने के लिए यहाँ जावास्क्रिप्ट का उपयोग कर रहे संभालने कर रहा हूँ नक्शा प्रदर्शित करते हैं। आप मूल कोड का उपयोग कर रहे हैं, तो आप अभी भी एक वेब सेवा हिट कर सकते हैं या आप एक देशी कॉल उपयोग कर सकते हैं (जो है, MapQuest iPhone SDK उस में एक देशी मार्ग कॉल है)।

दिशाओं सेवाओं के अधिकांश मार्ग की "shapepoints" वापस ताकि आप आकर्षित कर सकते हैं चाहिए।

यहाँ कुछ MapQuest- दिशा-निर्देश वेब सेवा का उपयोग कर shapepoints पाने के लिए उदाहरण हैं (आकार वापसी वस्तु को देखने के) - http://www.mapquestapi.com/directions/

2) एक उपरिशायी आकर्षित
बार जब आप अपने कोने है, तो आप उन्हें आकर्षित करने के लिए की जरूरत है। मुझे लगता है कि सबसे जावास्क्रिप्ट मानचित्र एपीआई किसी प्रकार का ओवरले वर्ग होगा। MapQuest एक यहाँ: http://developer.mapquest.com/web/documentation/sdk/javascript/v7.0/overlays#line

3) एक कॉल के साथ कर रहा
MapQuest भी मार्ग कॉल करने और आप के लिए लाइन आकर्षित करने के लिए कुछ सुविधा कार्यों - मैं दो से अधिक लिंक पोस्ट नहीं कर सकता! तो ऊपर के लिंक पर जाएं और "मार्ग" बाईं तरफ नेविगेशन पट्टी में देखने के लिए।

12/03/2012 को 17:09
का स्रोत उपयोगकर्ता

वोट
3

MapQuest एक SDK कि MapKit के लिए एक ड्रॉप में प्रतिस्थापन है है। यह वर्तमान में बीटा में है, लेकिन सक्रिय विकास के अंतर्गत।

यह अनुमति देता है ओवरले, मार्ग और जियोकोडिंग।

MapQuest आईओएस मैप्स एपीआई

05/03/2012 को 00:45
का स्रोत उपयोगकर्ता

वोट
2

इस सवाल का अद्यतन करने के लिए, वहाँ iOS7 के बाद से बाहरी apk करने की कोई जरूरत नहीं है।

यहाँ एक बहुत ही सरल और प्रभावी समाधान:

http://technet.weblineindia.com/mobile/draw-route-between-2-points-on-map-with-ios7-mapkit-api/2/

मैं जानता हूँ कि प्रश्न iOS 6 के बारे में था, लेकिन मुझे विश्वास है कि इस समाधान कई लोगों के लिए उपयोगी होगा।

केवल एक चीज है इस समाधान में लापता शुरुआत और समाप्त होने के पिन प्रदर्शित करने के लिए निम्नलिखित प्रतिनिधि विधि लागू कर रहा है

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
25/02/2015 को 08:30
का स्रोत उपयोगकर्ता

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more