当前位置: 编程技术>移动开发
本页文章导读:
▪对C的扩张 对C的扩展
第二章对C的扩展
一、构建Hello Objective-C
建立项目后在.m结尾文件内
代码如下:
#import <foundation>
int main(int argc, const char *argv[])
{
NSlog(@"Hello, Objective-C");
return(0);
}.........
▪ TextView展示链接 TextView显示链接
TextView的内容如果包含一个URL,将autoLink属性设置为web,该控件将找到并突出显示该URL
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是百.........
▪ ItemizedOverlay的低级使用 ItemizedOverlay的初级使用
实现一个ItemizedOverlay其实很简单,首先一个装载item的list,用于读取并绘制每一个item,创建一个添加方法 add 剩下的事情留给框架处理吧引用package map.chaos.com;import java.u.........
[1]对C的扩张
来源: 互联网 发布时间: 2014-02-18
对C的扩展
第二章对C的扩展 一、构建Hello Objective-C 建立项目后在.m结尾文件内 代码如下: #import <foundation> int main(int argc, const char *argv[]) { NSlog(@"Hello, Objective-C"); return(0); } 代码解读: #import 使用头文件来包含元素声明,元素可以包括结构体、符号常量、函数原型 #import可以保证头文件只被包含一次,在C语言中一般使用#ifdef命令实现, Objective-C中也可使用#include实现不过一般都用#import NSlog()和@"字符串" NSlog()用于向控制台输出,C语言中使用printf()向控制台输出 @"字符串"表示引用的字符串将作为Cocoa的NSString来处理 必须向NSlog()传递@"字符串",否则将会报警 NSlog()格式化字符串 %d表示数字,%@表示字符串,%s表示非Cocoa规格的字符串 NSLog (@"are %d and %d different? %@", 5, 5, YES); NSString的功能: 告知其长度 自身一其他字符串比较 将自身转换为整型值或浮点值 BOOL类型 它具有YES(1)和NO(0)值 Objective-C中提供了BOOL类型和C的bool类并存,并且年长10余年 在编写Cocoa代码中必须要使用BOOL类型 main(int argc, const char *argv[]) argv中保存用户提供的参数,argv[0]保存程序名称 argc提供参数的数组长度,为1时没有参数 调出控制台的快捷键: Start+Shift+R 或者在菜单Run中选Console BOOL类型示例代码 #import <foundation> // returns NO if the two integers have the same // value, YES otherwise BOOL areIntsDifferent (int thing1, int thing2) { if (thing1 == thing2) { return (NO); } else { return (YES); } } // areIntsDifferent // given a YES value, return the human-readable // string "YES". Otherwise return "NO" NSString *boolString (BOOL yesNo) { if (yesNo == NO) { return (@"NO"); } else { return (@"YES"); } } // boolString int main (int argc, const char *argv[]) { BOOL areTheyDifferent; areTheyDifferent = areIntsDifferent (5, 5); NSLog (@"are %d and %d different? %@", 5, 5, boolString(areTheyDifferent)); areTheyDifferent = areIntsDifferent (23, 42); NSLog (@"are %d and %d different? %@", 23, 42, boolString(areTheyDifferent)); return (0); } // main</foundation></foundation>
第二章对C的扩展 一、构建Hello Objective-C 建立项目后在.m结尾文件内 代码如下: #import <foundation> int main(int argc, const char *argv[]) { NSlog(@"Hello, Objective-C"); return(0); } 代码解读: #import 使用头文件来包含元素声明,元素可以包括结构体、符号常量、函数原型 #import可以保证头文件只被包含一次,在C语言中一般使用#ifdef命令实现, Objective-C中也可使用#include实现不过一般都用#import NSlog()和@"字符串" NSlog()用于向控制台输出,C语言中使用printf()向控制台输出 @"字符串"表示引用的字符串将作为Cocoa的NSString来处理 必须向NSlog()传递@"字符串",否则将会报警 NSlog()格式化字符串 %d表示数字,%@表示字符串,%s表示非Cocoa规格的字符串 NSLog (@"are %d and %d different? %@", 5, 5, YES); NSString的功能: 告知其长度 自身一其他字符串比较 将自身转换为整型值或浮点值 BOOL类型 它具有YES(1)和NO(0)值 Objective-C中提供了BOOL类型和C的bool类并存,并且年长10余年 在编写Cocoa代码中必须要使用BOOL类型 main(int argc, const char *argv[]) argv中保存用户提供的参数,argv[0]保存程序名称 argc提供参数的数组长度,为1时没有参数 调出控制台的快捷键: Start+Shift+R 或者在菜单Run中选Console BOOL类型示例代码 #import <foundation> // returns NO if the two integers have the same // value, YES otherwise BOOL areIntsDifferent (int thing1, int thing2) { if (thing1 == thing2) { return (NO); } else { return (YES); } } // areIntsDifferent // given a YES value, return the human-readable // string "YES". Otherwise return "NO" NSString *boolString (BOOL yesNo) { if (yesNo == NO) { return (@"NO"); } else { return (@"YES"); } } // boolString int main (int argc, const char *argv[]) { BOOL areTheyDifferent; areTheyDifferent = areIntsDifferent (5, 5); NSLog (@"are %d and %d different? %@", 5, 5, boolString(areTheyDifferent)); areTheyDifferent = areIntsDifferent (23, 42); NSLog (@"are %d and %d different? %@", 23, 42, boolString(areTheyDifferent)); return (0); } // main</foundation></foundation>
[2] TextView展示链接
来源: 互联网 发布时间: 2014-02-18
TextView显示链接
TextView的内容如果包含一个URL,将autoLink属性设置为web,该控件将找到并突出显示该URL
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="这是百度的网址:http://www.baidu.com/" android:autoLink="web"/>
也可以通过Linkify类实现
TextView mTextView = (TextView)findViewById(R.id.mTextView); Linkify.addLinks(mTextView, Linkify.WEB_URLS);
[3] ItemizedOverlay的低级使用
来源: 互联网 发布时间: 2014-02-18
ItemizedOverlay的初级使用
实现一个ItemizedOverlay其实很简单,首先一个装载item的list,用于读取并绘制每一个item,创建一个添加方法 add 剩下的事情留给框架处理吧
package map.chaos.com;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class MyMap extends MapActivity {
MapView mapview;
MapController controller;
GeoPoint[] g=new GeoPoint[5];
LocationManager locationManager;
Drawable[] drawables;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
drawables=new Drawable[]{getResources().getDrawable(R.drawable.home),
getResources().getDrawable(R.drawable.tg001),getResources().getDrawable(R.drawable.tg002),
getResources().getDrawable(R.drawable.tg003),getResources().getDrawable(R.drawable.tg004)};
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//设置mapview
mapview = (MapView) findViewById(R.id.mapview);
mapview.setTraffic(false);
mapview.setSatellite(false);
mapview.setStreetView(false);
mapview.setEnabled(true);
mapview.setClickable(true);
mapview.setBuiltInZoomControls(true);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER,
60000, 0, new LocationListener() {
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
// controller.animateTo(locationToGeopoint(location));
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
});
Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
List<Overlay> list = mapview.getOverlays();
//初始化GeoPoint
g[0]=locationToGeopoint(location);
g[1]=new GeoPoint(30523109,114389224);
g[2]=new GeoPoint(30623059,115389344);
//添加了3个MarkMyMap对象 显示3个不同的图标
MarkMyMap home = new MarkMyMap(drawables[0], this);
OverlayItem item_home = new OverlayItem(locationToGeopoint(location),"IAMHOME", "Home");
home.addOverlay(item_home);
MarkMyMap tg1=new MarkMyMap(drawables[1],this);
OverlayItem item_tg1=new OverlayItem(g[1],"tg1","tg1");
tg1.addOverlay(item_tg1);
MarkMyMap tg2=new MarkMyMap(drawables[2],this);
OverlayItem item_tg2=new OverlayItem(g[2],"tg2","tg2");
tg2.addOverlay(item_tg2);
// 添加到Overlay的list当中
list.add(home);
list.add(tg1);
list.add(tg2);
controller = mapview.getController();
controller.setZoom(17);
controller.animateTo(locationToGeopoint(location));
}
// location转GeoPoint
public GeoPoint locationToGeopoint(Location location) {
// TODO Auto-generated method stub
int gX = (int) (location.getLatitude() * 1E6);
int gY = (int) (location.getLongitude() * 1E6);
GeoPoint gp = new GeoPoint(gX, gY);
return gp;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
// 计算亮点距离
public String getDistance(GeoPoint gp){
float latitude_A=(float) (g[0].getLatitudeE6()/1E6);
float longitude_A=(float) (g[0].getLongitudeE6()/1E6);
float latitude_B=(float) (gp.getLatitudeE6()/1E6);
float longitude_B=(float) (gp.getLongitudeE6()/1E6);
float a=latitude_A-latitude_B;
float b=longitude_A-longitude_B;
float distance=(float) (2*6378.137*Math.asin(
Math.sin(a/2)*Math.sin(a/2)+Math.cos(latitude_A)*Math.cos(latitude_B)*Math.sin(b/2)*Math.sin(b/2)
));
return "与我相距 "+Math.round(distance*1000)+" 米";
}
// 处理地图标记
class MarkMyMap extends ItemizedOverlay<OverlayItem> {
ArrayList<OverlayItem> itemsOverlay = new ArrayList<OverlayItem>();
Context context;
GeoPoint tg;
public MarkMyMap(Drawable defaultMarker) {
//这里一定要加上boundCenterBottom 否则图片根本显示不出来 而不是显示效果不佳
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public MarkMyMap(Drawable defaultMaker, Context context) {
this(defaultMaker);//调用上一个构造器
this.context = context;
}
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
//GeoPoint和location转换使用1E6 精度不会丢失很严重
System.out.println("itemsOverlay.get(i).getMarker(i)====>"+ itemsOverlay.get(i).getPoint());
return itemsOverlay.get(i);
}
@Override
public int size() {
// TODO Auto-generated method stub
return itemsOverlay.size();
}
//mapview里使用
@Override
protected boolean onTap(int index) {
// TODO Auto-generated method stub
//如果同一个对象 添加了很多item 这里可以显示被点击的item ID
System.out.println("index is "+index);
Toast.makeText(context, getDistance(tg), Toast.LENGTH_SHORT).show();
return true;
}
public void addOverlay(OverlayItem item) {
itemsOverlay.add(item);
System.out.println("itemsOverlay.size()=====" + itemsOverlay.size());
tg=item.getPoint();
this.populate();//通知overlay添加数据
}
}
}
实现一个ItemizedOverlay其实很简单,首先一个装载item的list,用于读取并绘制每一个item,创建一个添加方法 add 剩下的事情留给框架处理吧
引用
package map.chaos.com;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class MyMap extends MapActivity {
MapView mapview;
MapController controller;
GeoPoint[] g=new GeoPoint[5];
LocationManager locationManager;
Drawable[] drawables;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
drawables=new Drawable[]{getResources().getDrawable(R.drawable.home),
getResources().getDrawable(R.drawable.tg001),getResources().getDrawable(R.drawable.tg002),
getResources().getDrawable(R.drawable.tg003),getResources().getDrawable(R.drawable.tg004)};
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//设置mapview
mapview = (MapView) findViewById(R.id.mapview);
mapview.setTraffic(false);
mapview.setSatellite(false);
mapview.setStreetView(false);
mapview.setEnabled(true);
mapview.setClickable(true);
mapview.setBuiltInZoomControls(true);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER,
60000, 0, new LocationListener() {
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
// controller.animateTo(locationToGeopoint(location));
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
});
Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
List<Overlay> list = mapview.getOverlays();
//初始化GeoPoint
g[0]=locationToGeopoint(location);
g[1]=new GeoPoint(30523109,114389224);
g[2]=new GeoPoint(30623059,115389344);
//添加了3个MarkMyMap对象 显示3个不同的图标
MarkMyMap home = new MarkMyMap(drawables[0], this);
OverlayItem item_home = new OverlayItem(locationToGeopoint(location),"IAMHOME", "Home");
home.addOverlay(item_home);
MarkMyMap tg1=new MarkMyMap(drawables[1],this);
OverlayItem item_tg1=new OverlayItem(g[1],"tg1","tg1");
tg1.addOverlay(item_tg1);
MarkMyMap tg2=new MarkMyMap(drawables[2],this);
OverlayItem item_tg2=new OverlayItem(g[2],"tg2","tg2");
tg2.addOverlay(item_tg2);
// 添加到Overlay的list当中
list.add(home);
list.add(tg1);
list.add(tg2);
controller = mapview.getController();
controller.setZoom(17);
controller.animateTo(locationToGeopoint(location));
}
// location转GeoPoint
public GeoPoint locationToGeopoint(Location location) {
// TODO Auto-generated method stub
int gX = (int) (location.getLatitude() * 1E6);
int gY = (int) (location.getLongitude() * 1E6);
GeoPoint gp = new GeoPoint(gX, gY);
return gp;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
// 计算亮点距离
public String getDistance(GeoPoint gp){
float latitude_A=(float) (g[0].getLatitudeE6()/1E6);
float longitude_A=(float) (g[0].getLongitudeE6()/1E6);
float latitude_B=(float) (gp.getLatitudeE6()/1E6);
float longitude_B=(float) (gp.getLongitudeE6()/1E6);
float a=latitude_A-latitude_B;
float b=longitude_A-longitude_B;
float distance=(float) (2*6378.137*Math.asin(
Math.sin(a/2)*Math.sin(a/2)+Math.cos(latitude_A)*Math.cos(latitude_B)*Math.sin(b/2)*Math.sin(b/2)
));
return "与我相距 "+Math.round(distance*1000)+" 米";
}
// 处理地图标记
class MarkMyMap extends ItemizedOverlay<OverlayItem> {
ArrayList<OverlayItem> itemsOverlay = new ArrayList<OverlayItem>();
Context context;
GeoPoint tg;
public MarkMyMap(Drawable defaultMarker) {
//这里一定要加上boundCenterBottom 否则图片根本显示不出来 而不是显示效果不佳
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public MarkMyMap(Drawable defaultMaker, Context context) {
this(defaultMaker);//调用上一个构造器
this.context = context;
}
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
//GeoPoint和location转换使用1E6 精度不会丢失很严重
System.out.println("itemsOverlay.get(i).getMarker(i)====>"+ itemsOverlay.get(i).getPoint());
return itemsOverlay.get(i);
}
@Override
public int size() {
// TODO Auto-generated method stub
return itemsOverlay.size();
}
//mapview里使用
@Override
protected boolean onTap(int index) {
// TODO Auto-generated method stub
//如果同一个对象 添加了很多item 这里可以显示被点击的item ID
System.out.println("index is "+index);
Toast.makeText(context, getDistance(tg), Toast.LENGTH_SHORT).show();
return true;
}
public void addOverlay(OverlayItem item) {
itemsOverlay.add(item);
System.out.println("itemsOverlay.size()=====" + itemsOverlay.size());
tg=item.getPoint();
this.populate();//通知overlay添加数据
}
}
}
最新技术文章: