It broken down.
Here is my code and logcat.Please help me to solve the problems.Thanks!
[/size]
Using java Syntax Highlighting
- package net.learn2develop.GoogleMaps;
- import java.io.IOException;
- import java.util.List;
- import java.util.Locale;
- import com.google.android.maps.GeoPoint;
- 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 android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.content.SharedPreferences;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Point;
- import android.location.Address;
- import android.location.Geocoder;
- import android.os.Bundle;
- import com.google.android.maps.MapView.LayoutParams;
- import android.telephony.SmsMessage;
- import android.view.KeyEvent;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.LinearLayout;
- import android.widget.Toast;
- public class MapsActivity extends MapActivity {
- MapView mapView;
- MapController mc;
- GeoPoint p;
- public static final String PREF_FILE_NAME = "PrefFile";
- String[] coordinates = new String[2];
- String[] location = new String[2];
- String temp;
- Bundle bundle;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mapView = (MapView) findViewById(R.id.mapView);
- LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
- View zoomView = mapView.getZoomControls();
- zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
- mapView.displayZoomControls(true);
- // mapView.setSatellite(true); /**显示卫星视图
- // mapView.setStreetView(true); /**显示街道视图
- mc = mapView.getController();
- bundle = getIntent().getExtras();
- try {
- if (bundle == null) {
- temp = "40.849165,123.328739";
- } else {
- temp = bundle.getString("location");
- }
- location = temp.split(",");
- } catch (NumberFormatException e) {
- e.toString();
- }
- for (int i = 0; i < location.length; i++) {
- coordinates[i] = location[i];
- }
- double lat = Double.parseDouble(coordinates[0]);
- double lng = Double.parseDouble(coordinates[1]);
- p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
- mc.animateTo(p);
- mc.setZoom(17);
- MapOverlay mapOverlay = new MapOverlay();
- List<Overlay> listOfOverlays = mapView.getOverlays();
- listOfOverlays.clear();
- listOfOverlays.add(mapOverlay);
- mapView.invalidate();
- }
- class MapOverlay extends com.google.android.maps.Overlay {
- @Override
- public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
- long when) {
- super.draw(canvas, mapView, shadow);
- // ---translate the GeoPoint to screen pixels---
- Point screenPts = new Point();
- mapView.getProjection().toPixels(p, screenPts);
- // ---add the marker---
- Bitmap bmp = BitmapFactory.decodeResource(getResources(),
- R.drawable.icon);
- canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
- return true;
- }
- public boolean onTouchEvent(MotionEvent event, MapView mapView) {
- // ---when user lifts his finger---
- if (event.getAction() == 1) {
- GeoPoint p = mapView.getProjection().fromPixels(
- (int) event.getX(), (int) event.getY());
- /** 通过获取坐标来拿到地理名称加上坐标显示 **/
- Geocoder geoCoder = new Geocoder(getBaseContext(),
- Locale.getDefault());
- try {
- List<Address> addresses = geoCoder.getFromLocation(
- p.getLatitudeE6() / 1E6, p.getLongitudeE6() / 1E6,
- 1);
- String add = "";
- if (addresses.size() > 0) {
- for (int i = 0; i < addresses.get(0)
- .getMaxAddressLineIndex(); i++)
- add += addresses.get(0).getAddressLine(i) + "\n"
- + p.getLatitudeE6() / 1E6 + ","
- + p.getLongitudeE6() / 1E6 + "\n";
- }
- Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT)
- .show();
- } catch (IOException e) {
- e.printStackTrace();
- }
- /**
- * 通过已知的地名找出位置 Geocoder geoCoder = new
- * Geocoder(getBaseContext(), Locale.getDefault()); try {
- * List<Address> addresses = geoCoder.getFromLocationName(
- * "empire state building", 5); String add = ""; if
- * (addresses.size() > 0) { p = new GeoPoint( (int)
- * (addresses.get(0).getLatitude() * 1E6), (int)
- * (addresses.get(0).getLongitude() * 1E6)); mc.animateTo(p);
- * mapView.invalidate(); } } catch (IOException e) {
- * e.printStackTrace(); }
- */
- return true;
- } else
- return false;
- }
- }
- /** Called when the activity is first created. */
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- MapController mc = mapView.getController();
- switch (keyCode) {
- case KeyEvent.KEYCODE_3:
- mc.zoomIn();
- break;
- case KeyEvent.KEYCODE_1:
- mc.zoomOut();
- break;
- }
- return super.onKeyDown(keyCode, event);
- }
- @Override
- protected boolean isRouteDisplayed() {
- return false;
- }
- }
Parsed in 0.052 seconds, using GeSHi 1.0.8.4
Using java Syntax Highlighting
- package net.learn2develop.GoogleMaps;
- import java.io.Serializable;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.telephony.gsm.SmsMessage;
- import android.util.Log;
- import android.widget.Toast;
- public class SMSReceiver extends BroadcastReceiver implements Serializable {
- @Override
- public void onReceive(Context context, Intent intent) {
- if ("android.provider.Telephony.SMS_RECEIVED"
- .equals(intent.getAction())) {
- StringBuilder sb = new StringBuilder();
- // 接收由SMS传过来的数据
- Bundle bundle = intent.getExtras();
- // 判断是否有数据
- if (bundle != null) {
- // 通过pdus可以获得接收到的所有短信消息
- Object[] objArray = (Object[]) bundle.get("pdus");
- /* 构建短信对象array,并依据收到的对象长度来创建array的大小 */
- SmsMessage[] messages = new SmsMessage[objArray.length];
- for (int i = 0; i < objArray.length; i++) {
- messages[i] = SmsMessage
- .createFromPdu((byte[]) objArray[i]);
- }
- /* 将送来的短信合并自定义信息于StringBuilder当中 */
- for (SmsMessage currentMessage : messages) {
- sb.append(currentMessage.getDisplayMessageBody());
- }
- }
- Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
- Intent mainIntent = new Intent(context, MapsActivity.class);
- mainIntent.putExtra("location", sb.toString());
- mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(mainIntent);
- }
- }
- }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
Using java Syntax Highlighting
- manifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="net.learn2develop.GoogleMaps" android:versionCode="1"
- android:versionName="1.0.0">
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
- <uses-permission android:name="android.permission.READ_SMS"></uses-permission>
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".MapsActivity" android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <receiver android:name="SMSReceiver">
- <intent-filter>
- <action android:name="android.provider.Telephony.SMS_RECEIVED" />
- </intent-filter>
- </receiver>
- <uses-library android:name="com.google.android.maps" />
- </application>
- </manifest>
- main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent" android:layout_height="fill_parent">
- <com.google.android.maps.MapView
- android:id="@+id/mapView" android:layout_width="fill_parent"
- android:layout_height="fill_parent" android:enabled="true"
- android:clickable="true" android:apiKey="0XNxIh2B3i4Pkvl_csKw1UrAoEsywGp_5ngeJLg" />
- <LinearLayout android:id="@+id/zoom" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:layout_alignParentBottom="true"
- android:layout_centerHorizontal="true" />
- </RelativeLayout>
Parsed in 0.040 seconds, using GeSHi 1.0.8.4

