MApView无法使用google api v2在Android中显示

时间:2013-06-12 11:35:34

标签: android-intent android-webview google-maps-android-api-2

我已经使用mapView api2在android for google map中创建了一个应用程序,我已经尝试了以下code.map完全runnig但是我想在点击代码片段时使用它,然后在secondActivity的webview中加载特定的“url”。我的代码如下:

Main.java

package com.example.mapviewdemo;

import java.security.PublicKey;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.webkit.WebView;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends FragmentActivity {
    private GoogleMap map;
    WebView wv;
private String a ;
private String b ;
//public String ah="http://en.wikipedia.org/wiki/Ahmedabad";
//public String br="http://en.wikipedia.org/wiki/Baroda";
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        map = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();
        setUpMap();
        if (map != null) {
         Marker Ahmedabad = map.addMarker(new MarkerOptions().position(
                    new LatLng(Double.parseDouble("23.03957"), Double
                            .parseDouble("72.56600"))).title("Ahmedabad"));
            Marker Baroda = map.addMarker(new MarkerOptions()
                    .position(
                            new LatLng(Double.parseDouble("22.30731"), Double
                                    .parseDouble("73.18110")))
                    .title("Baroda")
                    .snippet("Baroda")
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.pin_blue)));

            // Move the camera instantly to hamburg with a zoom of 15.
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(
                    new LatLng(Double.parseDouble("23.03957"), Double
                            .parseDouble("72.56600")), 15));

            // Zoom in, animating the camera.
            map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
             a =Ahmedabad.getTitle();
             b = Baroda.getTitle();
        }
    }
    private void setUpMap() {

        UiSettings setting = map.getUiSettings();
        setting.setMyLocationButtonEnabled(true);
        map.setMyLocationEnabled(true);
        map.setTrafficEnabled(true);

        map.setOnMarkerClickListener(new OnMarkerClickListener() {

            @Override
            public boolean onMarkerClick(Marker arg0) {
                // TODO Auto-generated method stub
                if(arg0.getTitle().equals(a)){
                Toast.makeText(getApplicationContext(),"Ahmedabad", 0).show();
                }
                else
                {
                    Toast.makeText(getApplicationContext(),"Baroda", 0).show();
                }

                return false;
            }
        });
        map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

            @Override
            public void onInfoWindowClick(Marker arg0) {
                // TODO Auto-generated method stub
                if(arg0.getTitle().equals(a)){
                Intent i =new  Intent(getApplicationContext(),SecondActivity.class);
                i.putExtra("ahm", a);
                startActivity(i);
                }
                else{
                Intent i =new Intent(getApplicationContext(),SecondActivity.class);
                i.putExtra("brd", b);
                startActivity(i);
                }
            //Toast.makeText(getApplicationContext(), "Check",0).show();    
            }
        });

    }

}

second.java

package com.example.mapviewdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;

public class SecondActivity extends Activity {
WebView v;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    v=(WebView)findViewById(R.id.webView1);
    Bundle b =getIntent().getExtras();
    b.getString("ahm");
    b.getString("brd");


    }



}

main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <fragment
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />



</RelativeLayout>

second.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SecondActivity" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

使用谷歌地图android api v2时,信息窗口任何部分的点击都被视为信息点击事件,你无法明确地说明是否点击了片段或任何其他部分。您的点击监听器代码似乎是正确的。当您检测到单击了哪个标记的信息时,您可以执行以下两种方法中的任何一种:

1,通过传递所需的url作为参数直接调用webview:

 Uri uri = Uri.parse("http://www.yourlocationspecificuri.com");
 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
 startActivity(intent);

2,按照您现在的操作调用您的第二个活动,然后在阅读了解被点击的标记的意图后,使用所需的网址启动网页视图:

v.loadUrl("http://www.yourlocationspecificuri.com");