callisto
callisto

Reputation: 5083

GeoCoding an address using MonoTouch

My question is a duplicate of this one: geocoding address into coordinates
with the only difference being that I need to do it in Monotouch (C#) not Objective-C.

I've tried this so far with no success:

string sw = searchWhere.Text;  
CLGeocoder clg = new CLGeocoder();  
clg.GeocodeAddress(sw, HandleCLGeocodeCompletionHandler);

EDIT: The CompletionHandler is called, but I dont know how to get the new Map app to show. (I am very new to iOS dev.)

    MKMapView MapIt = new MKMapView();
    void HandleCLGeocodeCompletionHandler (CLPlacemark[] placemarks, NSError error)
    {

        List<ObjAnnotation> oal = new List<ObjAnnotation>();

        if ( oal.Count > 0 )
        {
            MapIt.RemoveAnnotations(oal.ToArray()); 
        }
        oal.Clear();
        for(int i = 0; i < placemarks.Length; i++)
        {
            var loc = placemarks[i].Location.Coordinate;
            oal.Add(new ObjAnnotation(new CLLocationCoordinate2D(loc.Latitude, loc.Longitude),
                                      placemarks[i].Name, string.Empty));
        }
        MapIt.AddAnnotationObjects(oal.ToArray());
        CustomerDetailTab cdt = CustomerDetailTab;
        UIView view = cdt.View;
        MapIt.AddSubview (view);
    }

The line CustomerDetailTab cdt = CustomerDetailTab; does not build though. I am having trouble understanding how to get the view object.

Upvotes: 0

Views: 2329

Answers (1)

callisto
callisto

Reputation: 5083

Got it working thusly:

partial void btnAddr1Touch (MonoTouch.Foundation.NSObject sender)
        {

        short version =  Convert.ToInt16(UIDevice.CurrentDevice.SystemVersion.Split('.')[0] );
        string lmapLocation = Globals.SelectedCustomer.DelAddr01 + " " + Globals.SelectedCustomer.DelAddr02 + " " + Globals.SelectedCustomer.DelAddr03 + " " + Globals.SelectedCustomer.DelAddr04 + " " + Globals.SelectedCustomer.DelAddr05;
        mapLocation= Globals.SelectedCustomer.DelAddr01 + Environment.NewLine + Globals.SelectedCustomer.DelAddr02 + Environment.NewLine + Globals.SelectedCustomer.DelAddr03 + Environment.NewLine + Globals.SelectedCustomer.DelAddr04 + Environment.NewLine + Globals.SelectedCustomer.DelAddr05;


        if(mapLocation.Trim().Length == 0)return;

        if (version == 5) 
        {
            NSUrl url = new NSUrl("http://maps.google.com/maps?q=" + lmapLocation);
            UIApplication.SharedApplication.OpenUrl(url);
        }               
        else 
        {   
            ActivityThread.Start ("Loading Location");
            string sw = mapLocation;
            CLGeocoder clg = new CLGeocoder();
            clg.GeocodeAddress(sw, HandleCLGeocodeCompletionHandler);

        }
    }

void HandleCLGeocodeCompletionHandler (CLPlacemark[] placemarks, NSError error)
        {
            try 
            {
                CLLocationCoordinate2D coordinate = placemarks [0].Location.Coordinate;

            viewController = new MapViewController (coordinate, Globals.SelectedCustomer.Name, mapLocation);

            ActivityThread.Stop ();
            this.NavigationController.PushViewController (viewController, true);
        } 
        catch (Exception ex) 
        {
            ActivityThread.Stop ();
            var erroralert = new UIAlertView ("Location could not be found", "", null, "Ok", null);
            erroralert.Show ();
        }

    }

Upvotes: 1

Related Questions