0

我的问题是这个问题的副本:将地址地理编码为坐标
,唯一的区别是我需要在 Monotouch (C#) 中而不是 Objective-C 中进行。

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

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

编辑: CompletionHandler 被调用,但我不知道如何让新的地图应用程序显示。(我对 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;没有建立。我无法理解如何获取视图对象。

4

1 回答 1

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

    }
于 2012-11-29T13:27:28.890 回答