使用MonoTouch对地址进行GeoCoding

时间:2012-10-23 08:36:01

标签: ios xamarin.ios ios6 geocoding

我的问题与此问题重复:geocoding address into coordinates
唯一的区别是我需要在Monotouch(C#)而不是Objective-C中进行。

到目前为止,我已经尝试过这个但没有成功:

string sw = searchWhere.Text;  
CLGeocoder clg = new CLGeocoder();  
clg.GeocodeAddress(sw, HandleCLGeocodeCompletionHandler);
编辑:调用CompletionHandler,但我不知道如何显示新的Map应用程序。 (我是iOS开发人员的新手。)

    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);
    }

CustomerDetailTab cdt = CustomerDetailTab;行不会构建。我无法理解如何获取视图对象。

1 个答案:

答案 0 :(得分:1)

让它如此运作:

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 ();
        }

    }